I have created a java applet and htm file called Checker4 which is a checker which consists of 4 fillRectangles. A top left, top right, bottom left and bottom right. The applet box is 200 width and 200 height. I also have a drawOval which is width 90 and height 90. The problem is that I need the drawOval to start at the top left box znd visit the top right, bottom right, bottom left and back to the top left rectanlge. I have managed to make the drawOval visit the topleft, top right and bottom right but cannot progress any further to the bottom left and back to the top left rectangle. My code is as follows:
/* an applet which draws a continous moving checker*/
import java.awt.*;
public class Checker4 extends TaskingApplet
{
Image offscreenImage;
Graphics offscreenGraphics;
int xpos;//y co-ordinate
int ypos;//x co-ordinate
public void init()
{
offscreenImage = createImage(this.size().width,
this.size().height);
offscreenGraphics = offscreenImage.getGraphics();
}
public void run()
{
while (true)
{
for (xpos = 5; xpos <=105; xpos = xpos + 5)
{
repaint();
try (Thread.sleep(50);}
catch (InterrupterdException e) {}
}
for (ypos = 105; ypos >5; ypos = xpos -5)
{
repaint();
try {Thread.sleep(50);}
catch (InterruptedException e) {}
}
for ( xpos = 105; xpos >0; xpos = ypos -5)
{
repaint();
try {Thread.sleep(50);}
catch (InterruptedException e) {}
}
for { ypos = 5; ypos <=105; ypos = xpos-5)
{
repaint();
try {Thread.sleep (50);}
catch (InterruptedException e) {}
}
}
}
public void update (Graphics g)
{
paint (g);
}
public void paint (Graphics g)
{
offscreenGraphics.setColor(Color.black};
offscreenGraphics.fillRect(0,0,100,100);
offscreenGraphics.setColor(Color.white);
offscreenGraphics.fillRect(100,0,100,100);
offscreenGraphics.fillRect(101,10,100,100);
offscreenGraphics.setColor(Color.black);
offscreenGraphics.fillRect(101,101,100,100);
//draw Checker
offscreenGraphics.setColor(Color.blue);
offscreenGraphics.fillOval(xpos,ypos,90,90);
//copy the image to the screen
g.drawImage(offscreenImage,0,0,this);
}
}
I would appreciate if someone would look at my code and let me know what I am doing wrong and how I should implement my program so the drawOval travels from the topleft box in a clockwise direction. Thank you for your time.
Veneta