Andrew Osborne
03-26-2001, 04:50 PM
I'm a student taking a Java course where I had to build a fake screen saver
that draws line and refreshes itself after 1 second. I accomplished this
task. But I don't like how fast the line are drawn on the screen. I was
able to slow how quickly the line are being drawn by inserting a for loop
that does nothing. There has to be a better way. Any help would be appreciated.
Please note, this was not part of the assignment. I'm just curious. I
say this because I see people post her looking for someone to complete their
assignments for them.
Code
import javax.swing.*;
import javax.swing.Timer;
import java.awt.*;
import java.awt.event.*;
public class ElevenTwentyOne extends JFrame implements ActionListener{
private Timer timer = new Timer(1000,this);
private int counter = 0;
public ElevenTwentyOne()
{
super("Screen Saver");
setBackground(Color.black);
setSize(640, 480); //Standard windows full screen
show();
timer.start();
}//end ElevenTwentyOne constructor
public void actionPerformed(ActionEvent e)
{
repaint();
}//end actionPerformed
public void paint(Graphics g)
{
int X1,
Y1,
X2,
Y2;
for(int i = 0; i < 100; i++)
{
X1 = 1 + (int) (Math.random() * 100);
Y1 = 1 + (int) (Math.random() * 100);
X2 = 1 + (int) (Math.random() * 800);
Y2 = 1 + (int) (Math.random() * 800);
g.setColor(Color.blue);
g.drawLine(X1 ,Y1,X2,Y2);
for(int b = 0; b < 10000000; b++)
{
//slow things down... find better way
}
}
g.clearRect(0,0,640,480);
}//end paint
public static void main(String arg[])
{
ElevenTwentyOne app = new ElevenTwentyOne();
app.addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}//end windowClosing
}//end WindowAdapter
);
}//end main
}//end class ElvenTwentyOne
that draws line and refreshes itself after 1 second. I accomplished this
task. But I don't like how fast the line are drawn on the screen. I was
able to slow how quickly the line are being drawn by inserting a for loop
that does nothing. There has to be a better way. Any help would be appreciated.
Please note, this was not part of the assignment. I'm just curious. I
say this because I see people post her looking for someone to complete their
assignments for them.
Code
import javax.swing.*;
import javax.swing.Timer;
import java.awt.*;
import java.awt.event.*;
public class ElevenTwentyOne extends JFrame implements ActionListener{
private Timer timer = new Timer(1000,this);
private int counter = 0;
public ElevenTwentyOne()
{
super("Screen Saver");
setBackground(Color.black);
setSize(640, 480); //Standard windows full screen
show();
timer.start();
}//end ElevenTwentyOne constructor
public void actionPerformed(ActionEvent e)
{
repaint();
}//end actionPerformed
public void paint(Graphics g)
{
int X1,
Y1,
X2,
Y2;
for(int i = 0; i < 100; i++)
{
X1 = 1 + (int) (Math.random() * 100);
Y1 = 1 + (int) (Math.random() * 100);
X2 = 1 + (int) (Math.random() * 800);
Y2 = 1 + (int) (Math.random() * 800);
g.setColor(Color.blue);
g.drawLine(X1 ,Y1,X2,Y2);
for(int b = 0; b < 10000000; b++)
{
//slow things down... find better way
}
}
g.clearRect(0,0,640,480);
}//end paint
public static void main(String arg[])
{
ElevenTwentyOne app = new ElevenTwentyOne();
app.addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}//end windowClosing
}//end WindowAdapter
);
}//end main
}//end class ElvenTwentyOne