Results 1 to 3 of 3
- 06-03-2009, 04:29 PM #1Member
- Join Date
- Feb 2009
- Rep Power
How to change color of area contained by rectangle
My application is basically a sketchpad used to draw something like organization chart. Question is, whenever i hover the mouse over the nodes which is basically a rectangle drawn using the paint method, is there any way i can change the color of the portion of the sketchpad being occupied by the rectangle?
If the rectangle is a Rectangle2D, you could always create a MouseListener that iterate through your List<Rectangle2D> calling contains(x, y) to see if any rectangle contains the mouse coordinates. Otherwise, if you're not using Rectangle2D, you could create your own boolean contains(Rectangle r, Point p) and do the same thing.
- 06-04-2009, 04:48 AM #3
If you are handling painting yourself, with no components, then you will have to keep track of the mouse position and repaint portions of the display. I'm not sure how to invalidate only a portion of a container...
As far as tracking the mouse. I suggest use a javax.swing.Timer. This fires a Runnable at regular intervals. I suggest 30 milliseconds, which is around the speed motion picture frames process. The human eye sees this as continuous movement. Each interval, retrieve the mouse position and then determine what, if anything, needs to be done. This is generally more efficient than processing every mouse movement event, and you don't have to worry about things like the mouse leaving the component and your methods not receiving notification.
- By johnt in forum AWT / SwingReplies: 6Last Post: 05-14-2011, 06:48 AM
- By aRTx in forum New To JavaReplies: 1Last Post: 03-31-2009, 03:15 AM
- By Java.child in forum AWT / SwingReplies: 20Last Post: 02-12-2009, 06:51 AM
- By Java.child in forum AWT / SwingReplies: 1Last Post: 01-22-2009, 12:07 AM
- By dave700800 in forum New To JavaReplies: 1Last Post: 12-08-2007, 01:39 AM