The thing is that I'm a beginner on this of programming and I'm trying to make a program that simulates a rainfall. My problem is when I want to make an array of various raindrops fall they didn't.
Previously I've made a class for a drop, and I made the array in other class. So when I execute the program, this paint all the array of drops in different x positions but those didn't move down.
This is the code of Drop class:
public class Drop
{
private Random random = new Random();
private int x = random.nextInt(600);
private int y;
private int yspeed;
public Drop()
{
random = new Random();
}
public void fall()
{
y = y + yspeed;
}
public void draw(Graphics g)
{
g.setColor(Color.BLUE);
g.drawLine(x, y, x, y + 15);
}
}
And this is the class where it's painted:
public class Panel extends JPanel
{
private Drop[] drops = new Drop[100];
private Drop d = new Drop();
private static final long serialVersionUID = 1L;
public Panel()
{
setBackground(Color.CYAN);
for (int i = 0; i < drops.length; i++)
{
drops[i] = new Drop();
}
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
for (int i = 0; i < drops.length; i++)
{
d.fall();
drops[i].draw(g);
}
}
}
I'm a beginner on this and I'll appreciate any help you give me. Thanks.