Assaf
Assaf

Reputation: 1124

Oval on JPanel won't draw on different locations

I have a panel on which I have to draw balls, each time I press the mouse button. The balls are supposed to be drawn on the location pressed by the mouse.

The balls are drawn just fine, however they are drawn on the same location every time, so unless I "move" the window right and left, the drawn balls would not be seen.

Here's my code:

GUIBalls

public class GUIBalls {
public  JFrame frame = new JFrame("Balls");
public  JPanel panel = new JPanel();
private ArrayList<Ball> b = new ArrayList<>();
private Random rnd = new Random();

public GUIBalls(){
    setFrame();
    //moveBalls();
}
public void setFrame(){
    this.frame.setBounds(0,0,400,400);
    this.frame.add(panel);
    panel.setBounds(0,0,400,400);
    this.panel.addMouseListener(new MouseListener() {
        @Override
        public void mouseClicked(MouseEvent e) {
        }

        @Override
        public void mousePressed(MouseEvent e) {
            int x = e.getX();
            int y = e.getY();
            createBalls(x,y);
        }

        @Override
        public void mouseReleased(MouseEvent e) {
        }

        @Override
        public void mouseEntered(MouseEvent e) {
        }

        @Override
        public void mouseExited(MouseEvent e) {
        }
    });
}
public void createBalls(int x, int y){
    Ball ball = new Ball(x,y,10,10);
    this.b.add(ball);
    ball.draw(panel);
    panel.repaint();
}
}

Ball

public class Ball extends JPanel {
int x;
int y;
int z;
int w;

public Ball(int x, int y, int z, int w) {
    this.x = x;
    this.y = y;
    this.z = z;
    this.w = w;
}

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.fillOval(0, 0, z, w);

}

public void draw(JPanel panel) {
    panel.add(this);
    panel.setVisible(true);

}
}

For some odd reason, the balls are not being drawn on different locations.

Pictures of what happens are attached. enter image description here enter image description here

Any Ideas?

Upvotes: 0

Views: 48

Answers (1)

camickr
camickr

Reputation: 324118

The problem with your code right now is that you have a combination of trying to do custom painting and trying to create a custom component. You need to decide which approach you want to use.

For the basics of custom painting read the section from the Swing tutorial on Custom Painting that has a working example showing how to do this. Well, it shows how to draw a square on a panel but you get the idea.

If you want to keep adding objects to be painted, then you need to keep your objects in a ArrayList and then in the paintComponent() method you iterate through the List to paint each object. This approach is demonstrated in Custom Painting Approaches.

If you want to create a Ball as a component, then you need to make sure you override the get preferred size to return the size of the Ball. Then you always draw the oval at offset (0, 0) of the panel. You then add the Ball component to the parent panel which uses a null layout. Because you use a null layout you will position the Ball on the parent panel by using the setLocation(...) method. You will also need to use the setSize() method of the Ball component to be the preferred size.

Upvotes: 1

Related Questions