This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Drawing (mouse events) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Drawing (mouse events)" Watch "Drawing (mouse events)" New topic
Author

Drawing (mouse events)

John Lockheart
Ranch Hand

Joined: Oct 13, 2006
Posts: 115
I've created six ellipses labelled p1, p2...up to p6. I'm trying to make it so that on a mouse click (if a p object is selected), an unfilled rectangle will be drawn around it. As a way of letting someone know the object was selected. I haven't had much luck. I don't want the rectangle to be drawn until the mouse event, and i've even tried drawing it first and then having it moved when a p object is clicked...still no luck. Any help would be great.

Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Please don't post the same question to more than one forum. See here for an explanation of why I just deleted the other copy of this thread.

As for your question: you need to do all your painting in your paint() or paintComponent() method. Instead of trying to draw the rectangle here in the event handler, instead have the event handler set a member variable that represents the selection, and have the paint method draw the selection if and only if it finds that member set.

This is a general principle: event handlers should modify a data model of some kind, while your painting method should render that model to the screen.


[Jess in Action][AskingGoodQuestions]
John Lockheart
Ranch Hand

Joined: Oct 13, 2006
Posts: 115
Thanks, got it working now...Sorry about double posting. I was in a huge rush before I left for work and needed some help. Also sorry about posting in the wrong forum. I'll try to be more considerate and obey the rules.

One more question if you don't mind. Once I create a drawing, like a rectangle on a mouse event (like clicking inside a circle). How would I go about getting rid of the rectangle if a click occurred outside the circle? Would I have to redraw it and set color to background color or can I destroy it somehow?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
it would probably go something like this


[ February 04, 2007: Message edited by: Michael Dunn ]
 
GeeCON Prague 2014
 
subject: Drawing (mouse events)