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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:45:07+00:00 2026-05-27T00:45:07+00:00

I need to draw graphic element (square) dynamically in different positions of Canvas and

  • 0

I need to draw graphic element (square) dynamically in different positions of Canvas and I need to listen to mouse clicks in order to change place of my square.
How to add mouse listener to Graphics object? Do I have to use another approach?

int x = 0;
int y = 0;
 Graphics g = getGraphics(); // get Graphics context
                  g.setColor(Color.red);
          g.fillRect( x - 25, y - 15, 60, 30 );
          g.setColor(Color.black);
          g.drawRect( x - 25, y - 15, 60, 30 );
                  g.dispose();
  • 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-27T00:45:08+00:00Added an answer on May 27, 2026 at 12:45 am

    I’d probably use a JPanel as a child element of your larger component that forms the canvas. A JPanel, since it is a subclass of JComponent, allows you both to add a mouse listener via addMouseListener(), and to override its paintComponent() method.

    If you want to move the square, just reposition the JPanel.

    (for that matter, if it’s a square or rectangle, you don’t even need to override paintComponent, you could just accomplish this with appropriate calls to setBorder and setBackground.)


    Another approach would be to use a JPanel as the entire canvas, override the paintComponent to draw whatever you like, addMouseListener on the JPanel, and then manually determine whether the mouse listener events occur within the geometry of your graphic element.

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

Sidebar

Related Questions

I need to draw primitives with graphics object with different transformation matrix. I wonder
I need to draw sin(x)/x graphic into PictureBox in animation mode by timer component.
I have an application where I need to draw different graphics objects on the
When I try to draw an image to a Canvas element I get this
There is a need to visualize large quantity of 2d graphic objects (draw some
I need to draw a rectange,circle and line and then animate them(press left ->
I need to draw vertical and horizontal rectangle / bands (like in Gantt charts)
I need to draw simple alpha maps in xna which depends on current game
I need to draw a chart to show the evolution of data in real
I need to draw a lot of lines. I'm using UIBezierPath for drawing lines

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.