• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Drawing (mouse events)

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it would probably go something like this


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