gpt4 book ai didi

Java Swing简单动画

转载 作者:行者123 更新时间:2023-12-01 23:08:32 26 4
gpt4 key购买 nike

我在一个简单的动画程序上遇到了麻烦(是的,这是家庭作业)。该问题要求我们在 500X500 窗口上随机创建 100 个 Particle 类对象,并使用 Swing 计时器移动它们。这是我遇到的问题,所创建的粒子似乎不是随机的,但它们沿对角轴对齐,我无法找到逻辑错误,非常感谢您的帮助。这是一个屏幕截图,应该可以清楚地说明这一点。 http://i57.tinypic.com/qswbix.jpg

我还在帖子末尾发布了家庭作业问题,以防有人发现我的问题难以理解。谢谢您的帮助

public class ParticleFieldWithTimer extends JPanel{
private ArrayList<Particle> particle = new ArrayList<Particle>();
Timer timer;
boolean b;
public ParticleFieldWithTimer (){
this.setPreferredSize(new Dimension(500,500));


for(int i = 0; i < 100; i++) {
particle.add(new Particle());
}


timer = new Timer(40, new ActionListener()
{
@Override
public void actionPerformed(ActionEvent ae) {
for (Particle p : particle) {
p.move();
}
repaint();
}
});
timer.start();

}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setColor(Color.RED);

for (Particle p : particle) {
double temp1 = p.getX();
double temp2 = p.getX();
int tempX = (int) temp1;
int tempY = (int) temp2;
g2.fillRect(tempX, tempY, 3, 3);
}

}
public static void main(String[] args) {
final JFrame f = new JFrame("ParticleField");
final ParticleFieldWithTimer bb = new ParticleFieldWithTimer();
f.setLayout(new FlowLayout());
f.add(bb);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
try {
bb.finalize();
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

f.dispose();
}
});
f.pack();
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

这是粒子类

 public class Particle {
private int x , y ;

Random r = new Random();

public Particle () {

x = r.nextInt(500);
y = r.nextInt(500);

}
public int getX() {
return x;
}
public int getY() {
return y;
}
public void move() {

x += r.nextBoolean() ? 1 : - 1;
y += r.nextBoolean() ? 1 : - 1;
System.out.println("x : " + x+" y: " + y);
}



}

Create a class Particle that has two double fields x and y, a constructor that initializes these fields to random values between 0 and 500, methods getX and getY that return their values, and a method void move() that randomly adds or subtracts one to each of the values of x and y. (The quantities added to x and y are two separate random numbers.) Next, create a class ParticleFieldWithTimer that extends JPanel. This class should prefer to be 500 * 500 pixels in size. Its constructor should first fill an ArrayList field with 100 Particle objects, then start a Swing Timer that ticks 25 times a second. At each tick, the action listener should first call the method move for each particle, and then call repaint. The paintComponent method of ParticleFieldWithTimer should draw each particle as a 3*3 rectangle to its current coordinates.

最佳答案

paintComponent方法中,

替换

double temp2 = p.getX();

double temp2 = p.getY();

祝你好运。

关于Java Swing简单动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22392878/

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