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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:09:50+00:00 2026-06-08T03:09:50+00:00

I know how to keep my mouse (my g.draw(mouseX, mouseY) cursor) within a Ellipse2D

  • 0

I know how to keep my mouse (my g.draw(mouseX, mouseY) cursor) within a Ellipse2D / Shape…

@Override
public void mouseMoved(MouseEvent e) {
    int x = e.getX(), y = e.getY();
    if(shape.contains(x, y)) {
        mouseMoveX = e.getX();
        mouseMoveY = e.getY();
    }
}

…but this locks the movement entirely when the mouse leaves said shape (until it returns). IE it remains in the same position even though the actual cursor is moving around. I would like the mouse to be able to move around the Ellipse even though the actual cursor is out. Many of you may still be confused, sorry for that, if any more explanation is required I would be happy to oblige. Also, first question here so please let me know if I broke any rules! Thanks.

PS: Sorry for any late responses, currently on dialup internet 🙁

  • 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-08T03:09:52+00:00Added an answer on June 8, 2026 at 3:09 am

    The simplest way to do this would be to use the java.awt.Robot class, which allows you to directly control the mouse and keyboard:

    import java.awt.Robot;
    
    ...
    
    Robot robot = new Robot(<your GraphicsDevice>);
    
    ...
    
    @Override
    public void mouseMoved(MouseEvent e) {
        int x = e.getX(), y = e.getY();
        if(shape.contains(x, y)) {
            mouseMoveX = e.getX();
            mouseMoveY = e.getY();
        }
        else {
            robot.mouseMove(mouseMoveX,mouseMoveY); // Assuming these are the previous coordinates.
        }
    }
    

    Edit: Okay, try this instead:

    @Override
    public void mouseMoved(MouseEvent e) {
        int x = e.getX(), y = e.getY();
        if (shape.contains(x, y)) {
            mouseMoveX = e.getX();
            mouseMoveY = e.getY();
        }
        else {
            // get angle of rotation
            double r = Math.atan2(y-shape.getCenterY(),x-shape.getCenterX());
            mouseMoveX = (int) (shape.getWidth()/2 * Math.cos(r) + shape.getCenterX());
            mouseMoveY = (int) (shape.getHeight()/2 * Math.sin(r) + shape.getCenterY());
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know we cant keep objects directly in redis queue. I converted it to
I know it is good practice to keep your website entirely 'http://www' or http://,
I keep getting this error and I really dont know why. I have tried
Does anyone know a standard way to keep alive the http session as long
(Keep in mind this is on OSX Snow Leopard) I don't know how to
I would like to know if there is a way to keep the session
Would anyone happen to know a trick that will keep this MSBuild task from
I would like to know if a QTimer will keep on counting once it
I want to know, how to keep a method running in the background. ie.
I keep getting, an unable to load requested file error. Does anybody know how

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.