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 721367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:51:55+00:00 2026-05-14T05:51:55+00:00

The issue I’m having is issue with is I’m trying to get the paintComponent

  • 0

The issue I’m having is issue with is I’m trying to get the paintComponent to draw the circle only when the mouse is clicked, dragged, then let go. However inside my paintPanel class I have to initialize the object I’ve created (ex. movedCircle myCircle = new movedCircle(0,0,0,0);) just creating the object movedCircle myCircle; gives an error until I actually fully initialize the object with a value.

What I’m looking for:
What’s considered the best practice for this issue. I don’t want to draw anything unnecessary before it is needed.

The way I know how to fix it:
boolean values inside of paintComponent so that way it doesn’t draw until somethings actually there.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class drawCircle extends JFrame{
    private JPanel myPanel = new paintPanel();

    public drawCircle(){
        add(myPanel);
    }

    private class paintPanel extends JPanel{
        private int x1, y1, x2, y2;
        movedText myText = new movedText(0,0,0,0);
        movedCircle myCircle = new movedCircle(0,0,0,0);

        public paintPanel(){
            addMouseListener(new MouseAdapter(){
                public void mousePressed(MouseEvent e){
                    x1 = e.getX();
                    y1 = e.getY();
                    myCircle = new movedCircle(x1, y1, 0, 0);
                    repaint();
                }
                public void mouseReleased(MouseEvent e){
                    x2 = e.getX();
                    y2 = e.getY();
                    myCircle = new movedCircle(x1, y1, x2, y2);
                    repaint();
                }
            });

            addMouseMotionListener(new MouseMotionAdapter(){
                public void mouseDragged(MouseEvent e){
                    x2 = e.getX();
                    y2 = e.getY();
                    myText = new movedText(x1, y1, x2, y2);
                    myCircle = new movedCircle(x1, y1, x2, y2);
                    repaint();
                }
                public void mouseMoved(MouseEvent e){
                    x1 = e.getX();
                    y1 = e.getY();
                    x2 = 0;
                    y2 = 0;

                    myText = new movedText(x1, y1, x2, y2);
                    repaint();
                }
            });
        }

        protected void paintComponent(Graphics g){
            super.paintComponent(g);
            //draw oval after mouse released
            myText.paintText(g);
            myCircle.paintCircle(g);
        }
    }

    class movedCircle{
        private int x1, y1, x2, y2;

        public movedCircle(int x1, int y1, int x2, int y2){
            this.x1 = x1;
            this.y1 = y1;
            this.x2 = x2;
            this.y2 = y2;
        }

        public void paintCircle(Graphics g){
            g.drawOval(x1, y1, x2 - x1, y2 - y1);
        }
    }
    class movedText{
        private int x1, y1, x2, y2;

        public movedText(int x1, int y1, int x2, int y2){
            this.x1 = x1;
            this.y1 = y1;
            this.x2 = x2;
            this.y2 = y2;
        }

        public void paintText(Graphics g){
            g.drawString("x1: "+x1+" y1: "+y1+" x2: "+x2+" y2: "+y2, x1, y1);
        }
    }

    class RedSquare{
        private int xPos = 50;
        private int yPos = 50;
        private int width = 20;
        private int height = 20;

        public void setX(int xPos){ 
            this.xPos = xPos;
        }

        public int getX(){
            return xPos;
        }

        public void setY(int yPos){
            this.yPos = yPos;
        }

        public int getY(){
            return yPos;
        }

        public int getWidth(){
            return width;
        } 

        public int getHeight(){
            return height;
        }

        public void paintSquare(Graphics g){
            g.setColor(Color.RED);
            g.fillRect(xPos,yPos,width,height);
            g.setColor(Color.BLACK);
            g.drawRect(xPos,yPos,width,height);  
        }
    }

    public static void main(String[] args){
        JFrame frame = new drawCircle();

        frame.setTitle("Is in ellipse? Demo");
        frame.setSize(400, 400);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}
  • 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-14T05:51:55+00:00Added an answer on May 14, 2026 at 5:51 am

    I would go with the ifs, but the question is where.

    1. you can add them to paintPanel.paintComponent()
    2. you can add them to movedCircle.paint() and do not draw anything if its coordinates are dummy (for instance <0). The same with movedText

    Alternatively, position your figures at sensible start place.

    (There is one more solution: to subclass movedCircle with nullMovedCircle that doesn’t draw anything and create that at first in your paintPanel. However, creating new class for such a little alteration of behaviour seems overkill for me.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the issue I am having: I have a large query that needs
This issue came up when I got different records counts for what I thought
The issue is there is a database with around 20k customer records and I
The issue that prompted me to ask this is a web form that was
another issue in IE 6... link The navbar link list has a border applied
Our issue is that our project has files being downloaded using wget to the
This issue is driving me mad. I have several tables defined, and CRUD stored
The issue is simple really. Instead of creating folders in Visual Studio, I create
The issue I have is as follows: My company's supplier gives us an Access
I ran into an issue with an IIS web app shutting down an idle

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.