本篇文章给大家谈谈java移动图形,以及图形移动的画法对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
1、在Java怎么让图片水平移动?2、怎么编写java程序实现图片的移动(最好有例子)3、JAVA 如何把一个图形移动到另外一个位置,代码快完成,就差关键部分4、java中如何控制图形移动
在Java怎么让图片水平移动?
java让图片水平移动,就是不断监听图片的位置,示例如下:
//给个实例
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.net.URL;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class ImageMove {
static int x,y;
public static void main(String[] args) throws Exception{
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().setLayout(null);//这个要设置成 null
//图标
Icon icon = new ImageIcon(new URL(“”));
// Icon icon = new ImageIcon(“c:/logo-zhidao.gif”);//本地图片文件
JLabel l = new JLabel(icon);
l.setSize(icon.getIconWidth(),icon.getIconHeight()); l.setBorder(BorderFactory.createLineBorder(Color.red));
f.getContentPane().add(l); f.setVisible(true);
l.addMouseListener(
new MouseAdapter(){
public void mousePressed(MouseEvent e){ x=e.getX();y=e.getY(); } });
l.addMouseMotionListener(new MouseMotionListener(){ public void mouseDragged(MouseEvent e) { JLabel l = (JLabel)e.getSource();
l.setLocation(l.getX()+e.getX()-x,l.getY()+e.getY()-y);
}
public void mouseMoved(MouseEvent e) {} }); } }
怎么编写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);
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 如何把一个图形移动到另外一个位置,代码快完成,就差关键部分
我要分,赶快采纳哦……
public void run() {
/* Begin section C. */
// Increment c from 1 to 100.
for (int c = 1; c = 1000; c++) {
// Pause this frame for 250 milliseconds before drawing the next
// frame.
try {
Thread.sleep(250);
} catch (InterruptedException i) {
System.exit(1);
}
// 我以下添加
carbodylocation.changeLocation(new Location(carbodylocation.getVertical() – c, carbodylocation.getHorizontal() + c));
windowLocation.changeLocation(new Location(windowLocation.getVertical() – c, windowLocation.getHorizontal() + c));
window2Location.changeLocation(new Location(window2Location.getVertical() – c, window2Location.getHorizontal() + c));
wheellocation.changeLocation(new Location(wheellocation.getVertical() – c, wheellocation.getHorizontal() + c));
tireLocation.changeLocation(new Location(tireLocation.getVertical() – c, tireLocation.getHorizontal() + c));
tire2Location.changeLocation(new Location(tire2Location.getVertical() – c, tire2Location.getHorizontal() + c));
wheel2Location.changeLocation(new Location(wheel2Location.getVertical() – c, wheel2Location.getHorizontal() + c));
// 以上我添加
// Draw the frame.
repaint();
}
/* End section C. */
}
java中如何控制图形移动
图像移动效果是你第一眼看到他在这,第二眼看到他在那。
所以你要做两件事,,
1,不断绘图
2,改变要绘制的对象的位置
另一种方式,,
绘制一下,改变一下位置,绘制一下,改变,,,
相关类,,
窗体JFRAME
绘制面板PANEL
重写绘制面板的PAINT方法,用一个线程以一定频率不断调用重绘方法,
java移动图形的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于图形移动的画法、java移动图形的信息别忘了在本站进行查找喔。