Fair enough, it's a bit convoluted but here I go. This is just an example of something I'd like to do to test my delay code. It's supposed to draw a line, wait 2 seconds and then draw another line below it. What actually happens is that the window opens, waits 2 seconds then sets the background and draws all the lines simultaneously.
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import util.*;
class Lines extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
g.drawLine(0, 50, 50, 50);
Time.delay(2000);
g.drawLine(0, 100, 100, 100);
}
}
public class JustALine extends JApplet {
Lines line = new Lines();
public void init() {
Container cp = getContentPane();
line.setBackground(Color.BLUE);
cp.add(line);
}
}
now my util package contains the delay() method and looks like this:
Code:
package util;
public class Time {
/**
*Makes your program wait the specified number of milliseconds before continuing
*@param delay The number of milliseconds you want to wait for
**/
public static void delay(int delay){
boolean cont = false;
long startTime = System.currentTimeMillis() + delay;
while(cont == false){
if(startTime <= System.currentTimeMillis())
cont = true;
}
}
}
If I use System.out.println() to print a series of messages to the console with a delay in between them, my code works perfectly but it refuses to cause a sequence of delayed events to happen in an applet.
I'm really just a beginner to programming and I've been teaching myself so I might need telling what's wrong in baby terms. I do know how to use the jdk documentation to look up methods and things though.
Cheers guys