Results 1 to 3 of 3
  1. #1
    thayalan is offline Member
    Join Date
    Feb 2009
    Posts
    11
    Rep Power
    0

    Default 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?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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.

  3. #3
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    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.

Similar Threads

  1. Replies: 6
    Last Post: 05-14-2011, 06:48 AM
  2. Replies: 1
    Last Post: 03-31-2009, 03:15 AM
  3. Color Change of data
    By Java.child in forum AWT / Swing
    Replies: 20
    Last Post: 02-12-2009, 06:51 AM
  4. How to Change the color of MultiColumnListBox
    By Java.child in forum AWT / Swing
    Replies: 1
    Last Post: 01-22-2009, 12:07 AM
  5. How to change TXT color Onclick
    By dave700800 in forum New To Java
    Replies: 1
    Last Post: 12-08-2007, 01:39 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •