I am writing an applet that makes checkers run at the same speeds back and forth opposite one another. The bottom checker however is running much faster than the top one. How would i correct it so they run at the same speed back and forth. Im guessing it has something to do with the for loops but i cannot figure it out.
import java.awt.*;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Image;
public class Checkers extends java.applet.Applet implements Runnable {
Thread runner;
int x1pos;
int x2pos;
Image offscreenImg;
Graphics offscreenG;
public void init() {
offscreenImg = createImage(this.size().width, this.size().height);
offscreenG = offscreenImg.getGraphics();
}
public void start() {
if (runner == null); {
runner = new Thread(this);
runner.start();
}
}
public void stop() {
if (runner != null) {
runner.stop();
runner = null;
}
}
public void run() {
while (true) {
for (x1pos = 5; x1pos <= 105; x1pos+=4)
{
for (x2pos = 105; x2pos >= 5; x2pos-=4)
{
repaint ();
try { Thread.sleep(10); }
catch (InterruptedException e) { }
}
repaint();
try { Thread.sleep(100); }
catch (InterruptedException e) { }
}
x1pos = 5;
x2pos = 105;
}
}
public void update(Graphics g) {
paint(g);
}
public void paint(Graphics g) {
// Draw background onto the buffer area
offscreenG.setColor(Color.black);
offscreenG.fillRect(0,0,100,100);
offscreenG.setColor(Color.blue);
offscreenG.fillRect(100,0,100,100);
offscreenG.setColor(Color.blue);
offscreenG.fillRect(0,100,100,100);
offscreenG.setColor(Color.black);
offscreenG.fillRect(100,100,100,100);
// Draw checker
offscreenG.setColor(Color.red);
offscreenG.fillOval(x1pos,5,90,90);
offscreenG.setColor(Color.green);
offscreenG.fillOval(x2pos,105,90,90);
// Now, transfer the entire buffer onto the screen
g.drawImage(offscreenImg,0,0,this);
}
public void destroy() {
offscreenG.dispose();
}
}
Thank you