File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question using Paint method

 
Clay Adkerson
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have 4 buttons with action listeners on them. When i click one it will run repaint() and will use drawString to show some text depending on the button clicked with If Else loops. That all works fine but when i start the applet i have some stuff already there but since i do repaint() it will draw all the all over again plus whatever is with the button clicked. Is there a way to make it only draw whatever that is associated with the button? If you need some code i'll post it. Dont want to right now cause it's sort of a mess.
 
Jeff Jetton
Ranch Hand
Posts: 71
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are a lot of ways to do this.

One way is to have your button call the repaint method in this form:

repaint(int x, int y, int width, int height);

The parameters you pass represent a rectanglular area you wish to update. When you do that, only the area of that rectangle gets erased and redrawn. (The code in your paint method can still draw to the whole graphics object, but java will automatically restrict its actual output to just the rectangle area.)

Another trick is to create your own Image object, and have your application do all of the initial drawing to it. Then, in your paint method, you just copy everything from your Image object to the screen's graphics object, then draw your text on top of that. That way, you don't have to reconstruct your initial image every time.

Finally, you could override the update() method. That method gets called by repaint, and it erases the screen and then calls the paint method. If you override it, and have it just call paint() without erasing anything, you won't get that "flicker" caused by the screen being erased all the time:

public void update(Graphics g)
{
paint(g);
}


Good luck,

- Jeff
 
Clay Adkerson
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, thanks for the suggestions. I'll try them out.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Swing/AWT...
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic