swing Java计时器动作侦听器
可以使用您的帮助作业,包括摆动计时器、动作侦听器和多个对象。我不知道是否允许在这里发布这个问题,但我在动画方面遇到了问题,以下是我到目前为止得到的
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. Make sure that the Timer will stop when the user closes the frame
这是ParticleFieldWithTimer类
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
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) {
// change polygon data
// ...
Particle p = new Particle();
p.move();
repaint();
}
});
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
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);
}
}
这是粒子类
import java.util.Random;
public class Particle {
private double x , y ;
Random r = new Random();
public Particle () {
x = r.nextDouble()*500;
y = r.nextDouble()*500;
}
public Double getX() {
return x;
}
public Double getY() {
return y;
}
public void move() {
x = r.nextInt(2) - 1;
y = r.nextInt(2) - 1;
System.out.println(x + " " + y);
}
}
# 1 楼答案
这个
是错误的方法,它是创建一个100
Timer
的,这将影响系统的性能你也在创建一个新的
Particle
每当计时器滴答作响时,这也不是你真正想要做的,你想影响你已经创建的Particle
相反,创建你的粒子
然后创建你的
Timer
并在其中,遍历你已经创建的粒子别忘了启动计时器
或者更改
Graphics
上下文的颜色,它可能仍然设置为面板的背景我还注意到
不是在做你想做的事。它将始终使值介于-1和1之间。相反,您希望将结果添加到x/y值中
现在,这种方式使得数值以一种(基本上)一致的方式在屏幕上“拖动”
你可以试着用
但这最终让他们在原地起舞(大部分)