我正在尝试使用具有随机生成的 x 坐标的固定线长度来模拟降雨。我已经让随机 x 坐标正常工作,但我希望线条在重新绘制到窗口上后也具有不同的下降速度。我正在使用 javax swing 计时器和 java Random 生成整数以作为索引传递到我的“速度”数组中。然而速度并没有改变。它保持不变并且太快了。
public class rain extends JPanel implements ActionListener {
int i = 0;
int[] speed = {5, 10, 15, 20, 25, 30, 35, 40, 45, 50};
double[] x = {10, 202, 330, 140, 250, 160, 470, 180, 290, 510};
double y1 = 10, y2 = 20;
double down = 1;
Random random = new Random();
//Timer t = new Timer(speed[i], this);
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D l = (Graphics2D) g;
Line2D line = new Line2D.Double(x[i], y1, x[i], y2);
l.setColor(Color.blue);
l.draw(line);
Timer t = new Timer(speed[i], this);
t.start();
}
public void actionPerformed(ActionEvent e) {
if (y2 < 380) {
y1 += down;
y2 += down;
}else{
y1 = 10;
y2 = 20;
i = random.nextInt(10);
}
repaint();
}