0

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

NJD
  • 107
  • 1
  • 1
  • 8
  • 1) Why code an applet? If it is due to spec. by teacher, please refer them to [Why CS teachers should stop teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why AWT rather than Swing? See my answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. – Andrew Thompson May 09 '14 at 07:08

1 Answers1

0

Your loop inside your run statement is nested. This means for every time you update x1pos you update x2pos from 105 to 5.

Your for loop should read (reworked as similar as I could to yours)

  for (x1pos = 5, x2pos = 105; x1pos <= 105 && x2pos >= 5 ; x1pos+=4, x2pos-=4)
  {
    repaint ();
    try { Thread.sleep(10); }
    catch (InterruptedException e) { }
  }
ug_
  • 11,267
  • 2
  • 35
  • 52