wood burning stoves*
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 EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "repaint() problem" Watch "repaint() problem" New topic
Author

repaint() problem

Anh Nguyen
Greenhorn

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");

setSize(400,400);
show();

timer = new Timer(1000, new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
repaint();
}
});
timer.start();
}

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

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

}

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

app.addWindowListener(
new WindowAdapter()
{
public void windowClosing( WindowEvent e)
{
System.exit(0);
}
});
}
}</h6>

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

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
Greenhorn

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,
Stella
Anh Nguyen
Greenhorn

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


regards
deeksha
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
Greenhorn

Joined: Jan 03, 2002
Posts: 4
Anh,
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: http://aspose.com/file-tools
 
subject: repaint() problem
 
Similar Threads
Question about a simple GUI application
Animation in Swing
how to repaint but still keep the graphic that painted previously?
java graphic problem
how do I reset the JFrame?