1

I'm currently working on a Project that shows a tortoise vs. hare race in real-time as an Applet. A random number is chosen, and the animals either move forward, backward, or not at all based on that number. My problem is getting the animals to be seen in real-time. It currently displays the images at their starting positions, and says who wins. Any pointers on how to get this done would be great.

my code:

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Color;
import java.awt.Font;

public class Project2 extends Applet
{
Image tortoise, hare;
Image scaledTortoise, scaledHare;
final int tortoiseYPos = 50, hareYPos = 400, SQUARE = 20, END = 1200;
int tortoiseXPos = 180, hareXPos = 180;

public void init()
{
    tortoise = getImage(getDocumentBase(), "tortoise.gif");
    hare = getImage(getDocumentBase(), "hare.gif");
    scaledTortoise = tortoise.getScaledInstance(20, 50, Image.SCALE_SMOOTH);
    scaledHare = hare.getScaledInstance(20, 50, Image.SCALE_SMOOTH);
}

public void paint(Graphics field)
{
    drawField(field);
    drawMoves(field);

    field.setFont(new Font("Times New Roman", Font.ITALIC, 72));
    //Display winner when they get to the finish line
    if(tortoiseXPos >= END)
    {
            field.drawString("Tortoise Wins!!", 650, 240);
    }
        else if(hareXPos >= END)
        {
            field.drawString("Hare Wins!!", 650, 240);
    }
}   

public void drawField(Graphics field)
{
    setBackground(Color.green);
    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(scaledTortoise, 180, tortoiseYPos, this);       
    field.drawImage(scaledHare, 180, hareYPos, this);
}

public void drawMoves(Graphics s)
{ 
    while(tortoiseXPos < END && hareXPos < END)
    {
        int move = (int)(Math.random() * 10);
        tortoiseMoves(move); hareMoves(move);
        s.drawImage(scaledTortoise, tortoiseXPos, tortoiseYPos, this);
        s.drawImage(scaledHare, hareXPos, hareYPos, this);
        delay(); delay(); delay();
    }
}
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)
    {
        hareXPos = hareXPos;
    }

    if(hareXPos < 180)
    {
        hareXPos = 180;
    }

    if(hareXPos > END)
    {
        hareXPos = END;
    }
}

public void delay()
{
    for(int i = 0; i <= 90000000; i++)
    {
    }
}
}

I should note that I am not familiar with and should not be using java swing, as I am only supposed to be using awt.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
tedclark72
  • 77
  • 1
  • 6
  • 1) Why code an applet? If it is due to the teacher specifying it, 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 use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson May 25 '15 at 05:25
  • @AndrewThompson to both your questions, my teacher wants me to use both an applet and AWT. – tedclark72 May 25 '15 at 15:27

1 Answers1

2

The problem is with your delay function. Entering a loop keeps the applet running your code and it does not get a chance to render the display between moves. If you allow the Thread that your applet is running in to sleep, then that gives it a chance to display as it runs.

public void delay()
{
    try {
        Thread.sleep(100);
    } catch (Exception e) {}
}
  • Hmmm, when I run it in my IDE, I see it step through the race. Perhaps you could try getting a basic animation going as a starting point, perhaps http://www.javatpoint.com/Animation-in-applet . Good luck. – Harry Glasgow May 25 '15 at 00:54
  • Thanks, that worked for me. Now I just need to get it to delete the previous move and I should be good – tedclark72 May 25 '15 at 15:32