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>