java图片向上移动代码 java图片随着窗口放大而放大

怎么编写java程序实现图片的移动(最好有例子)import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
public class DrawTest extends JFrame {
private int x = 50;
private int y = 50;
private Image offScreenImage = null;
@Override
public void paint(Graphics g) {
Color c = g.getColor();
g.setColor(Color.BLACK);
g.fillOval(x, y, 30, 30);
g.setColor(c);
}
public void update(Graphics g) {
if (offScreenImage == null) {
offScreenImage = this.createImage(500, 500);
}
Graphics gOffScreen = offScreenImage.getGraphics();
Color c = gOffScreen.getColor();
gOffScreen.setColor(Color.GREEN);
gOffScreen.fillRect(0, 0, 500, 500);
【java图片向上移动代码 java图片随着窗口放大而放大】gOffScreen.setColor(c);
paint(gOffScreen);
g.drawImage(offScreenImage, 0, 0, null);
}
public static void main(String[] args) {
DrawTest d = new DrawTest();
}
public DrawTest() {
init();
addKeyListener(new KeyAdapter() {
public void keyPressed(final KeyEvent e) {
int code = e.getKeyCode();
switch (code) {
case KeyEvent.VK_UP:
y -= 5;
break;
case KeyEvent.VK_RIGHT:
x= 5;
break;
case KeyEvent.VK_DOWN:
y= 5;
break;
case KeyEvent.VK_LEFT:
x -= 5;
break;
}
}
});
}
public void init() {
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.setBackground(Color.GREEN);
this.setResizable(false);
this.setBounds(140, 140, 500, 500);
this.setVisible(true);
MyThread mt = new MyThread();
new Thread(mt).start();
}
class MyThread implements Runnable {
public void run() {
while (true) {
repaint();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
以上
java语言里怎么让图象移动到指定位置啊,最好用一小段代码说明.改变规制时候的X Y就行了.伪代码如下.
int x =0,y=0,;
x; y;
g.drawImage( "图片信息" , x, y,锚点);
大概就这样图片就动了.你想移动到哪加个判断就行了.
一段图片从右到左移动的JAVASCRPT代码,帮忙改成图片从上到下移动...想要图片从上到下.?,是自动移动吗?
是的话,用Marquee这个标签就行了,里面有控制方向的属性-
-,没必要写个方法
java 中移动图片,并且在按键盘的“回车”时,切换到另外一张图片?。∏朐谖蚁旅娴拇敫慕 。。⌒恍唬 。?/h2>你好,按照你的要求代码如下 , 修改java图片向上移动代码了三处
简单说明一下,就是加java图片向上移动代码了一个标识boolean , 用true/false来表示显示第一张/第二张图片
import java.awt.*;
import java.awt.event.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;
public class zhandou extends JFrame implements KeyListener {
Image roleImage, Image1;
int x, y;
public zhandou() {
super("MOVE");
Container c = getContentPane();
setSize(320, 240);
setVisible(true);
loadImage();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// setFocusable(true);
addKeyListener(this);
}
boolean key = true;// 加这样一个标识,true是第一张图片,false为第二张图片
public void paint(Graphics g) {
super.paint(g);
// drawRole(g);
if (key) {// 根据标识判断需要显示的图片
g.drawImage(roleImage, x, y, this);
} else {
g.drawImage(Image1, x, y, this);
}
}
public void loadImage() {
ImageIcon icon = new ImageIcon("tupian/草地.jpg");
roleImage = icon.getImage();
ImageIcon ic = new ImageIcon("tupian/right.gif");
Image1 = ic.getImage();
}
class Thread1 extends Thread {
}
/*
* private void drawRole(Graphics g) { g.drawImage(roleImage,100,100,this);
* }
*/
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_UP)
y = y - 5;
else if (e.getKeyCode() == KeyEvent.VK_DOWN)
y = y5;
else if (e.getKeyCode() == KeyEvent.VK_RIGHT)
x = x5;
else if (e.getKeyCode() == KeyEvent.VK_LEFT)
x = x - 5;
else if (e.getKeyCode() == KeyEvent.VK_ENTER)
key = !key;// 切换标识状态
repaint();
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
public static void main(String args[]) {
new zhandou();
}
}
关于java图片向上移动代码和java图片随着窗口放大而放大的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

    推荐阅读