2

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!

javathunderman
  • 1,074
  • 2
  • 13
  • 31
  • 1
    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 Sep 01 '15 at 12:17
  • 1
    @AndrewThompson Yes, the teacher is specifying it, but I am working with some older curriculum (they refreshed the course while I was taking it). I did, by the way, read your blog post (you posted a similar comment on another question), and I agree with you. I'd much rather be using Swing here. – javathunderman Sep 01 '15 at 12:28
  • 1
    *"you posted a similar comment on another question"* It's the first thing I post on *any* applet based questions these days. ;) Glad you got the problem sorted, and my commiserations on having to use either applets or AWT. – Andrew Thompson Sep 01 '15 at 12:45
  • @AndrewThompson lol thanks! – javathunderman Sep 01 '15 at 13:10

1 Answers1

1

You are not calling your drawMoves(g) method anywhere. Jut call this method from paint(Graphics field) and it will work. That's only thing is missing.

public void paint(Graphics field)
{
    drawField(field); //draw the field
    drawMoves (field)
}

And your implementation will repaint your objects so over time you will see so many tortoise and hare.

JBaba
  • 590
  • 10
  • 30
  • Thanks! Still doesn't seem to work though. I'm getting this http://i.imgur.com/wRGrgQC.png versus what I got initially http://i.imgur.com/HiCsNmM.png – javathunderman Aug 31 '15 at 17:17
  • I used your code and by just adding this line worked for me. But used rectangle as object because i did not had images. – JBaba Aug 31 '15 at 18:06