我对基本java applet的第一次尝试失败了。我无法弄清楚为什么矩形不会移动

时间:2014-12-04 12:06:30

标签: java applet

public class Basics extends Applet{

int x = 0;

int y = 0;

    public void init(){
        setSize(500,500);
    }

    public void start(){
        Thread a = new Thread();
        a.start();
    }

    public void run(){
        while(true){
            x = 100;
            y = 100;                
            repaint();
            try{
                Thread.sleep(18);
            }
            catch(InterruptedException e){}
        }

    public void paint(Graphics g){
        g.setColor(Color.red);
        g.fillRect(this.x,this.y,25,25);
    }

}

不能递增x和y然后重新绘制允许方块移动

2 个答案:

答案 0 :(得分:6)

您应该增加x和y值,现在只需为其赋值。改变它:

public void run(){
        while(true){
            x += 100;
            y += 100;                
            repaint();
            try{
                Thread.sleep(18);
            }
            catch(InterruptedException e){}
        }

答案 1 :(得分:0)

在你的跑步方法中,你没有增加X和Y,你可以设置它们。

尝试

x += 100;
y += 100;

我会使用较小的值,但是从1开始。