I want to create two objects of the Ball class. I have tried the following:
public class World extends JPanel {
JFrame frame = new JFrame("GreenJ");
Actor[] actor = new Actor[100];
int n = 0;
public World() throws InterruptedException{
frame.add(this);
frame.setSize(1000, 1000);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void addObject(Actor a) {
actor[n] = a;
frame.add(actor[n]);
}
}
public class MyWorld extends World {
public MyWorld() throws InterruptedException {
addObject(new Ball(frame, 250, 750));
addObject(new Ball(frame, 750, 250));
}
}
public class Ball extends Actor{
int x;
int y;
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.fillOval(x, y, 50, 50);
}
public Ball(JFrame frame, int a, int b) throws InterruptedException{
frame.add(this);
x = a;
y = b;
}
public void main(String[]Args) {
repaint();
}
}
When I run this code I only get the first 'ball' in my frame. I have tried some other things but without success.
Thank you in advance. ElAdriano