This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes repaint() problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "repaint() problem" Watch "repaint() problem" New topic

repaint() problem

Anh Nguyen

Joined: Jan 24, 2001
Posts: 18
I try to make a screen saver which will draw 100 lines, refresh, then draw another 100 in every second.
I don't know why it does not refresh the screen when I call repaint() in this program . Please help me .
<h6>public class LineSaver extends JFrame
private Timer timer;

public LineSaver()
super("Screen saver with 100 of random lines");


timer = new Timer(1000, new ActionListener()
public void actionPerformed(ActionEvent e)

public void paint(Graphics g)
Graphics2D g2d = (Graphics2D) g;

for (int i=0; i<100; i++)
g2d.setColor( new Color ((int)(Math.random()*256),
g2d.setStroke( new BasicStroke((float)(Math.random()*10)));
g2d.draw( new Line2D.Double((int)(Math.random()*400),


public static void main (String [] agrs)
LineSaver app = new LineSaver();

new WindowAdapter()
public void windowClosing( WindowEvent e)

... No one can escape .......<BR><B>Heavenly Evil Knight</B>
Anh Nguyen

Joined: Jan 24, 2001
Posts: 18
Sorry, I don't know how to align the code lines in order . Look like it just skip all the tabs and spaces .
Julie Stella

Joined: Jan 12, 2001
Posts: 5
Try calling invalidate() first and then repaint(). invalidate() will the container and all parents above it as needing to be laid out and, therefore, repainted.
Hope this helps,
Anh Nguyen

Joined: Jan 24, 2001
Posts: 18
It's not work :-( . But thanks anyway :-)
deekasha gunwant
Ranch Hand

Joined: May 06, 2000
Posts: 396
hi Anh,
your program is running perfectly on my m/c . i.e. with jdk 1.3 on win NT 4.0

Grant Crofton
Ranch Hand

Joined: Nov 08, 2000
Posts: 154
when you call repaint(), it doesn't necessarily repaint the screen streight away, it just puts in a request for it to be done at the next available opportunity.
Sometimes it helps if you pass a number, eg repaint( 50 ) - this is asking for a repaint within 50ms.
Sometimes it just doesn't seem to work at all - I guess this could be because the machine is too slow. Drawing graphics tends to be fairly slow in Java anyway.
David Roseberry

Joined: Jan 03, 2002
Posts: 4
Did you ever get this to work?
Specifically, where the screen clears. I am trying the same assignment and am unable to get the screen to clear before drawing the next 100 lines.
Either I get one pass through the 'for' loop getting me a screen with 100 lines and the program ends, or when I add a call to
repaint()as the last line in method paint I get an endless loop.
But how to clear the screen escapes me.
Any luck?
Vinod Venugopal
Ranch Hand

Joined: Dec 06, 2000
Posts: 148
just use super.paint(g); as the first statement in your paint method. It SHOULD work

- Vinod<br />-------<br />SCJP2
I agree. Here's the link:
subject: repaint() problem
Similar Threads
how do I reset the JFrame?
java graphic problem
how to repaint but still keep the graphic that painted previously?
Animation in Swing
Question about a simple GUI application