Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7526293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:47:25+00:00 2026-05-30T03:47:25+00:00

This is my second semester doing Java and so please be patient. Part of

  • 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;` 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T03:47:27+00:00Added an answer on May 30, 2026 at 3:47 am

    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
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm an University student and new to java. Last year, second semester we did
hello guys im using this second part of pagination script to show pagination; <?
I've got a Perl script that needs to execute another Perl script. This second
I've a application that must work after another application. This second application has a
Ok, I need something like this: datediff(second, date_one, date_two) < 1 dates are stored
This is a second-hand question from an OS development site, but it made me
This is my second question on Bamboo ( My First One ). My understanding
I hope it's the heat, this is the second question today and it's one
This most be the second most simple rollover effect, still I don't find any
This bit of code is taking almost a half second to execute. Could somebody

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.