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;`