0

This is my second semester doing Java and so please be patient. Part of my assignment is to click a Radio button and use the Circle's draw method to draw a Circle object on a Jpanel Content pane when the mouse button is clicked. Then store each Circle object in a Arraylist so that it will stay on the pane until I unclick the radio button. I can get everything to work except for adding the Circle object to the Arraylist and keeping that circle on the screen. It will just draw one circle at a time and erase the first previous one when I click again. I don't think that I am adding the new circles created to the Arraylist, I'm just a circle. Not sure.

Here is my code for the part that is drawing the circle.

public class MyPanel extends JPanel {

  public ArrayList<Circle> circles; 

  public void paintComponent(Graphics g) {
    Circle c = new Circle(xstart, ystart);   //create a new circle
    ArrayList<Circle> circles = new ArrayList<Circle>();
    if (drawing){ 
        c.draw(g);
        circles.add(c);
        for(int k=0; k<circles.size(); k++){
            circles.get(k).draw(g);
            }
    }           // draw the circle

Code for drawing and declaring the drawing boolean in my MouseTest Constructor and tied to the radio button. Drawing true means that when the radio button is clicked then it can draw circles.

JPanel radioPanel = new JPanel(new GridLayout(2,0)); //new GridLayout(y, x)
radioPanel.add(circleButton);
radioPanel.add(trackButton);    
cp.add(radioPanel,BorderLayout.EAST);
drawing = false;

circleButton.addActionListener(new ActionListener() { 
//Set drawing to true when the button is clicked
        public void actionPerformed(ActionEvent ae) {
            drawCircles();
        }

    }); 

 public void drawCircles() {    //initialize tracking to false
    drawing = !drawing;` 
kleopatra
  • 51,061
  • 28
  • 99
  • 211
handro
  • 85
  • 6
  • 13

1 Answers1

1

You have a couple of issues. First, in your paintComponent function you are creating a local ArrayList of Circles. Each time the paintComponent is called, you are recreating this variable. Instead, just use the ArrayList of Circles that belongs to the class.

The other issue you have is that each circle is being drawn twice, once after the circle is created, the other time in the for loop. you should remove the call to have the circle draw itself, and just drawn them all in the for loop.

Finally, and this may or may not be the desired behavior, but currently you are creating a new Circle each time the paintComponent is getting called. You can potentially end up with many more circles than you want, because this function can be called a lot. You may want to reconsider at what point new circles are being created.

The following fixes the first couple of issues.

public class MyPanel extends JPanel {

  public ArrayList<Circle> circles = new ArrayList<Circle>(); 

  public void paintComponent(Graphics g) {

    Circle c = new Circle(xstart, ystart);   //create a new circle
    circles.add(c);
    if (drawing){  
        for(int k=0; k<circles.size(); k++){
            circles.get(k).draw(g);
        }
    }           // draw the circle
  }
kleopatra
  • 51,061
  • 28
  • 99
  • 211
Tony
  • 1,401
  • 9
  • 11
  • That was it. I was thinking that I had to draw the circle first but now I see that I already created a new circle. and stored that in the Arraylist and then the .draw(g) drew it. thank you very much!! – handro Feb 18 '12 at 23:03