Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

repaint() problem

 
Anh Nguyen
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>
 
Anh Nguyen
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not work :-( . But thanks anyway :-)
 
deekasha gunwant
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just use super.paint(g); as the first statement in your paint method. It SHOULD work
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic