• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about repainting

 
michael bradly
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working on an applet that will draw a line from the cursor location to one of 5 random locations when the mouse is clicked.
I've managed to get the line drawn, however I am stumbling on how to make it dissappear immediately after the the mouse click event. I'm assuming repaint should be called, but what exactly do I repaint? The line? The background?
Any suggestions would be appreciated.
Regards, Michael
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I'm understanding you correctly...
You're drawing a line not after a mouse clicked event, but after a mouse pressed event, and then you want the line to disappear after a mouse clicked event (mouse clicked event = mouse pressed event + mouse released event and no mouse dragged event).
Wherever it is that you are performing your drawing, it's probably a good bet that it inherits the method repaint() from java.awt.Component (or perhaps overrides it). So, perhaps this structure would work for you:

Good Luck.
[ March 16, 2002: Message edited by: Dirk Schreckmann ]
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Concerning this part...
Originally posted by michael bradly:
but what exactly do I repaint? The line? The background?

You could set the current drawing color to the background color and then draw a line with the same coordinates as the line you want to erase. You could invoke clearRect(int, int, int, int) and clear a rectangle surrounding the line you want to erase. clearRect uses the background color to clear the specified area. The background color can be obtained using getBackground() which is probably inherited by your component from java.awt.Component.
Back on the topic of when to erase the line...
You might want to just invoke repait() from the mouse pressed (or mouse clicked) event and then let the method that is doing the drawing draw and erase the line after a short delay using:

inside the drawing method between the draw command and before the erase command.
Good Luck.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... and of course, the method setColor(Color) from class Graphics is probably available.
[ March 21, 2002: Message edited by: Dirk Schreckmann ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic