I am just a beginner and was doing some time with games and got a problem
package minigames;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class fallingball extends JPanel {
int x = 250;
int y = 0;
private void moveBall() {
y = y + 1;
}
public void paint(Graphics g){
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(Color.black);
g2d.fillOval(x, y, 30, 30);
}
public static void main(String[] args) throws InterruptedException{
JFrame f = new JFrame("Falling Ball");
fallingball game = new fallingball();
f.add(game);
f.setSize(500, 500);
f.setResizable(false);
f.setLocationRelativeTo(null);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
while (true) {
game.moveBall();
game.repaint();
Thread.sleep(10);
}
}
}
only 1 ball is generated and it gets fallen down. i wanted to generate balls falling randomly and from different x co-ordinate in the frame how can i do that