0

Say, for example, if I have a circle drawn in a JFrame, and I want to paint over it if I hover over it for three seconds.

I've got a MouseMotionListener that tells me the point of the cursor in the JFrame, but so far that's about it.

public void mouseMoved(MouseEvent e)
{
  PointerInfo a = MouseInfo.getPointerInfo();
  cursorPos = a.getLocation();
  SwingUtilities.convertPointFromScreen(cursorPos, e.getComponent());
}

I'm still quite new to action listeners. What should I add if I want to test if the mouse is not moving?

Also, I'm just curious, why is it that MOUSE_MOVED in MouseEvent is considered an int?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
KebertX
  • 304
  • 1
  • 6
  • *"find the time since"* Use a single shot Swing [`Timer`](http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html). – Andrew Thompson Dec 07 '12 at 16:00
  • Are you sure this is not better suited to a [`JToolTip`](http://docs.oracle.com/javase/tutorial/uiswing/components/tooltip.html)? – Andrew Thompson Dec 07 '12 at 16:03
  • "why is it that MOUSE_MOVED in MouseEvent is considered an int" - its a static final int; that is more commonly known as a constant, a value which does not change at any time. A constant tends to be an integer, but you bind a variable to it (MOUSE_MOVED) to give it a human readable name. – Gimby Dec 07 '12 at 16:05

1 Answers1

1

Going backwards through your questions.

Why is it that MOUSE_MOVED in MouseEvent is considered an int?

All of the mouse constants are ints. That's how enumeration was done in Java before version 1.5.

How do I find the time since the mouse last moved?

In your mouseMoved method, you save the current time somewhere in your GUI model. You then write a method in your GUI model that returns the idle time.

idleTime = System.currentTimeMillis() - savedTimeinMillis.

You divide the idle time by 1000 for seconds.

Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111