gpt4 book ai didi

java - 我对基本 Java 小程序的第一次尝试失败了。我不明白为什么矩形不动

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:44:54 26 4
gpt4 key购买 nike

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 然后重新绘制允许正方形移动

最佳答案

你应该增加你的 x 和 y 值,现在你只是给它赋值。像这样改变它:

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

关于java - 我对基本 Java 小程序的第一次尝试失败了。我不明白为什么矩形不动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27293706/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com