• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Drawing (mouse events)

 
John Lockheart
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
John Lockheart
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it would probably go something like this


[ February 04, 2007: Message edited by: Michael Dunn ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic