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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:00:12+00:00 2026-05-27T02:00:12+00:00

In the code below I have simply used a mouse listener to get the

  • 0

In the code below I have simply used a mouse listener to get the XY coordinates of the the mouse, and then call for a repaint. Within the paint method I’ve drawn a rectangle using the same XY coordinates for position. The rectangle does follow but at a distance from the mouse pointer. I’d expect the top left corner of the rectangle to touch the mouse pointer.

Am I doing something wrong?

Why is there a distance between my mouse pointer and the Rectangle object?

public void mouseMoved(MouseEvent e){
    x = e.getX();
    y = e.getY();

    repaint();
}

public class Canvas extends JPanel{
    Canvas(){}

    public void paint(Graphics g){
        Graphics2D g2 = (Graphics2D)g;
        g2.setPaint(Color.red);
        g2.fillRect(x, y, 50, 50);          
    }
}
  • 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-27T02:00:12+00:00Added an answer on May 27, 2026 at 2:00 am
    1. Don’t call your class Canvas, there is an AWT component by that name so it becomes confusing.

    2. Custom painting is done by overriding the paintComponent() method of the JPanel, not the paint() method.

    3. You don’t show where you add the MouseListener to the panel. You are probably adding it to the frame instead.

    If you need more help then post your SSCCE that demonstrates the problem.

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

Sidebar

Related Questions

I have a data that looks like this . And my code below simply
I have written a very simple WCF service, that worked fine (code below), then
I get this error, while I'm testing the code below: You have an error
I have a simple MFC program which displays the progressbar..I used the below code
I have a simple code below: import java.util.ArrayList; public class BoidList extends ArrayList {
below i have a code that runs in most of my simple programs ..
The below code is very simple. I have a jQuery autocomplete bound to an
I have below code in html. <li class=selected runat=server id=lihome><a href=/ISS/home.aspx title=Home><span>Home</span></a></li> Now I
I have below code behind in c# if (Session[cmpDictionaryTitle]!= null) { downloadLinks.Text += @<li><a
I have below code to insert a style into DOM (there is a use

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.