Before I actually get to my question, let me just say that I'm not a fan of Java applets and this is for a project. I understand there are much better ways to do what I want.
Anyway, the question.
I'm creating a tortoise and hare race, where a random number between one and ten is generated. That number will then be used to determine the number of spaces that the tortoise or hare moves (i.e. if the random number is 2, then the hare will move 1 square over). I have to show the hare and tortoise moving along, but the code I'm using doesn't move the pictures anywhere, the simply remain stationary.
Here's my code:
import java.applet.Applet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
public class experiment extends Applet {
Image tortoise, hare; //create the images
final int tortoiseYPos = 50, hareYPos = 400, square = 20, end = 1256; //create x positions and y positions
int tortoiseXPos = 180, hareXPos = 180;
public void init()
{
tortoise = getImage(getDocumentBase(), "picresources/tortoise.jpg"); //get the images for the objects
hare = getImage(getDocumentBase(), "picresources/hare.jpg");
}
public void paint(Graphics field)
{
drawField(field); //draw the field
}
public void drawField(Graphics field)
{
Font f = new Font("Times New Roman", Font.BOLD, 48);
field.setFont(f);
field.drawString("Tortoise", 0, 75);
field.drawString("Hare", 0, 425);
//fill alternating black and white rectangles
field.setColor(Color.black);
int x = 180;
for(int i = 0; i < 50; i++)
{
field.fillRect(x, 50, square, 50);
field.fillRect(x, 400, square, 50);
x += (square);
}
field.drawImage(tortoise, 180, tortoiseYPos, this);
field.drawImage(hare, 180, hareYPos, this);
}
public void drawMoves (Graphics g) {
while(tortoiseXPos < end && hareXPos < end)
{
int move = (int)(Math.random() * 10);
tortoiseMoves(move);
delay();
delay();
delay();
hareMoves(move);
delay();
delay();
delay();
clearCurrent(g);
delay();
g.drawImage(tortoise, tortoiseXPos, tortoiseYPos, this);
g.drawImage(hare, hareXPos, hareYPos, this);
}
}
public void tortoiseMoves(int move)
{ //Moves for Tortoise, 180 is start, 1200 is finish
if(move <= 5)
{
tortoiseXPos += (3 * square);
}
else if(move <= 8)
{
tortoiseXPos += square;
}
else if(move <= 10)
{
tortoiseXPos -= (6 * square);
}
if(tortoiseXPos < 180)
{
tortoiseXPos = 180;
}
if(tortoiseXPos > end)
{
tortoiseXPos = end;
}
}
public void hareMoves(int move)
{ //Moves for Hare, 180 is start, 1200 is finish
if(move <= 2)
{
hareXPos += (9 * square);
}
else if(move <= 5)
{
hareXPos += (square);
}
else if(move <= 6)
{
hareXPos -= (12 * square);
}
else if(move <= 8)
{
hareXPos -= (2 * square);
}
else if(move <= 10)
{
}
if(hareXPos < 180)
{
hareXPos = 180;
}
if(hareXPos > end)
{
hareXPos = end;
}
}
public void clearCurrent( Graphics s )
{
s.clearRect(tortoiseXPos+1, tortoiseYPos+1, WIDTH-1, HEIGHT-1);
s.clearRect(hareXPos+1, hareYPos+1, WIDTH-1, HEIGHT-1);
}
public void delay()
{
try {
Thread.sleep(1000);
} catch(Exception ex) {
}
}
}
Thanks in advance!