本文目录一览:
java 画了一个圆,怎么让它上下左右移动啊?
移动圆,改变它的圆心即可,可以通过给圆心设置一个运动轨迹函数实现,实例代码为;
public class joinDemo1 extends JFrame
{
//继承
private int x=100, y=100, r=100;
//初始值
public joinDemo1()
{
super("小图形");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(800, 600);
this.setVisible(true);
Thread thread=new Thread(new Graphicss());
thread.start();
}
public void paint(Graphics g)
{
super.paint(g);
g.fillOval(x, y, r, r);
}
public static void main(String[] args)
{
new joinDemo1();
}
class Graphicss implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
for (int j = 0; j = 240; j++) {
revalidate();
// System.out.println(j);
try {
Thread.sleep(1000);// 当前线程休眠0.01秒
} catch (InterruptedException e) {
e.printStackTrace();
}
y=y+j;
repaint();
}
}
}
}
java中如何控制图形移动
图像移动效果是你第一眼看到他在这,第二眼看到他在那。
所以你要做两件事,,
1,不断绘图
2,改变要绘制的对象的位置
另一种方式,,
绘制一下,改变一下位置,绘制一下,改变,,,
相关类,,
窗体JFRAME
绘制面板PANEL
重写绘制面板的PAINT方法,用一个线程以一定频率不断调用重绘方法,
java如何实现图片拖动,放大缩小,旋转。
这个只是实现了移动,你参考以下吧 !
public class MoveImage {
static int x,y;
private static int num=0;
private static Icon icon=null;
public static void main(String[] args) throws Exception{
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().setLayout(null);//这个要设置成 null
//图片
icon = new ImageIcon("d:/test.gif");//d:/test.gif本地一张图片
JLabel l = new JLabel(icon); //创建具有指定图像的 JLabel 实例。
l.setSize(icon.getIconWidth(),icon.getIconHeight());//设置面板的宽度和高度
l.setBorder(BorderFactory.createLineBorder(Color.red));//给图片加上红色外框
f.getContentPane().add(l);
f.setSize(900,700);
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();
}
}
}
}
}
以上