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

  • Home
  • SEARCH
  • 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 8421377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:08:05+00:00 2026-06-10T03:08:05+00:00

I have Form on which i am drawing oval on mouseClick Event. This works

  • 0

I have Form on which i am drawing oval on mouseClick Event. This works Fine for me. The Circles are painted. But When i Minimize the Form and Again Maximize it the Panel gets Refreshed and Circles are removed (i.e Panel is left Blank).

Code is :
I have a JFrame on which there is a Jpanel named jPanel1, on this panel circles are drawn.

private void jPanel1MouseClicked(java.awt.event.MouseEvent evt) {
        count += 1;
        if (count <= clients) {
            drawCircle(evt.getX() - (radius / 2), evt.getY() - (radius / 2));
        }
    }

    public void drawCircle(int x, int y) {
        Graphics g = jPanel1.getGraphics();
        g.drawOval(x - radius, y - radius, 2 * radius, 2 * radius);
        g.setColor(Color.BLACK);
        g.fillOval(x - radius, y - radius, 2 * radius, 2 * radius);
    }
  • 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-06-10T03:08:06+00:00Added an answer on June 10, 2026 at 3:08 am

    In this case, not only is it important to override your paintComponent method for the JPanel, but you also need to store the information about the circles you’re going to draw. You use that stored information to paint all the circles on the screen during the paintComponent call.

    import java.awt.*;
    import java.awt.event.*;
    import java.util.ArrayList;
    
    import javax.swing.*;
    
    
    
    public class TempProject extends JPanel{
        /** Stores info about circles  */
        public ArrayList<CircleInfo> circles = new ArrayList<CircleInfo>();
    
        /** fields that were in example code */
        public int count = 0;
        public final int radius = 20;
        public final int clients = 20;
    
        public TempProject(){
    
            addMouseListener(new MouseAdapter(){
    
                @Override
                public void mouseClicked(MouseEvent evt) {
                    count += 1;
                    if (count <= clients) {
                            // Store info about the circle to draw
                        circles.add(new CircleInfo(evt.getX() - (radius / 2), evt.getY() - (radius / 2), radius));
                            // Tell swing to repaint asap
                        repaint();
                    }
                }});
        }
    
        @Override
        public void paintComponent(Graphics g ) {
                super.paintComponent(g);
    
                //Iterates through saved circles and paints them
            for(CircleInfo circle : circles){
                g.drawOval(circle.x - circle.radius, circle.y - circle.radius, 2 * circle.radius, 2 * circle.radius);
                g.setColor(Color.BLACK);
                g.fillOval(circle.x - circle.radius, circle.y - circle.radius, 2 * circle.radius, 2 * circle.radius);
            }
        }
    
        public static void main(String args[])    {
            EventQueue.invokeLater(new Runnable()
            {
                public void run()
                {
                    JFrame frame = new JFrame();
                    frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
                    frame.setContentPane(new TempProject());  
                    frame.setPreferredSize(new Dimension(400, 300));
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        /** Simple class for storing Circle info */
        public static class CircleInfo{
            int x = 0;
            int y = 0;
            int radius = 0;
    
            public CircleInfo(int x, int y, int radius){
                this.x = x; this.y = y; this.radius = radius;
            }
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following method which works fine: button21.FlatAppearance.BorderSize = 0; button21.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Transparent;
I have a form, which sets these styles in constructor: this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); this.SetStyle(ControlStyles.UserPaint, true);
I have a form (using MVC2) which has an image-upload script, but the rules
I have a MouseEnter event which currently handles some custom controls on my form.
I have a form which I want to be 'resusable' for a variety of
I have a form which has a RichTextBox docked to the left and DataGridView
I have a form which POST's its data via AJAX: $.ajax({ type: POST, url:
I have a form which has three inputs when the page loads -- 2
We have a form which uses the JQuery Validation Plugin and would like to
I have a form which consists of 2 steps. What I'd like to do

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.