Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Checking If the Mouse is Over a Certain Component

  1. #1
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Question Checking If the Mouse is Over a Certain Component

    I've run into yet another tricky problem.

    While the user's mouse is over a certain panel I have on my GUI, I want the panel to outline a circle around the user's mouse. I'm already using a Swing Timer to control the movement of existing objects drawn on the panel, and I've overridden the panel's paintComponent(Graphics g) method, so I thought I'd just add a little if-statement to that paintComponent(Graphics g) method so that it also checks to see if the mouse is over the panel.

    I'd also need to know how to get the mouse's position with respect to the JPanel that is drawing the circle around the mouse...

    However, I'm not finding any fast ways to do this... Can anyone point (heh, mouse pointer <---awful pun) me in the right direction?
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.


  2. #2
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Checking If the Mouse is Over a Certain Component

    Well, I found a decent solution. No longer need help.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Checking If the Mouse is Over a Certain Component

    Care to share your solution? A MouseMotionListener should allow you to follow the mouse movement appropriately, and you can repaint as needed.

  4. #4
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Checking If the Mouse is Over a Certain Component

    Care to share your solution?
    Yeah, I will.

    I use java.awt.MouseInfo, and then to see if the mouse is over the certain panel, I do this:
       private boolean mouseIsOverDisplayPanel()
       {
     
          if(MouseInfo.getPointerInfo().getLocation().x >= displayPanel.getLocationOnScreen().x
                  && MouseInfo.getPointerInfo().getLocation().x <= displayPanel.getLocationOnScreen().x + displayPanel.getWidth()
                  && MouseInfo.getPointerInfo().getLocation().y >= displayPanel.getLocationOnScreen().y
                  && MouseInfo.getPointerInfo().getLocation().y <= displayPanel.getLocationOnScreen().y + displayPanel.getHeight())
          {
     
             return true;
     
          }
          else
          {
     
             return false;
     
          }
       }
    Using this code, everything is show relative to the screen, and I can accurately check if the mouse is over this panel whenever I need to.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Checking If the Mouse is Over a Certain Component

    Just an alternative: Use a MouseListener and its corresponding mouseEntered/mouseExited methods to set a boolean flag - then check this flag when you paint....and/or use a MouseMotionListener to get the true location of the mouse if needed.

  6. #6
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Checking If the Mouse is Over a Certain Component

    Yes, that actually occurred to me just a couple hours ago. I'm trying to decipher which method is more useful to my program...
    Last edited by snowguy13; January 4th, 2012 at 06:03 PM.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

Similar Threads

  1. How to get Mouse Position even if it is not within our application?
    By Freaky Chris in forum Java Programming Tutorials
    Replies: 2
    Last Post: January 4th, 2012, 10:57 AM
  2. Mouse Listeners
    By newbie in forum AWT / Java Swing
    Replies: 2
    Last Post: November 27th, 2010, 11:08 PM
  3. component
    By nasi in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 4th, 2010, 07:40 PM
  4. Checking in.
    By Johannes in forum Member Introductions
    Replies: 7
    Last Post: August 13th, 2009, 06:11 AM
  5. How to get Mouse Position even if it is not within our application?
    By Freaky Chris in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: April 29th, 2009, 12:41 PM

Tags for this Thread