This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Question using Paint method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Question using Paint method" Watch "Question using Paint method" New topic
Author

Question using Paint method

Clay Adkerson
Greenhorn

Joined: Mar 16, 2005
Posts: 27
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

Joined: Mar 29, 2005
Posts: 71
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

Joined: Mar 16, 2005
Posts: 27
Okay, thanks for the suggestions. I'll try them out.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Moving to Swing/AWT...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
 
subject: Question using Paint method
 
Similar Threads
why repaint is not updating GUI?
Trying to make a button but don't know where to start
Book GUI - URLyBird
how to repaint but still keep the graphic that painted previously?
JList set element visible