Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9110043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:11:02+00:00 2026-06-17T03:11:02+00:00

I’m having trouble simulating a race between two competitors. This is your typical race

  • 0

I’m having trouble simulating a race between two competitors. This is your typical race program where you use a random number generator to determine what “moves” the competitors use. As seen in my code below, the track is composed of 50 rectangles, and the filled in rectangle shows the location of each competitor on the track. Some “moves” make the competitor jump 9 squares to the right, or 2 squares back, for example. When I run the applet, only the initial starting position is displayed; the applet doesn’t work. I realize it’s a lot of code, but what do I need to do to fix my problem? I’m really stuck at this point. Any help is appreciated. I have can only use AWT, not swing. This is an assignment for class :/ Here is the code:

import java.awt.*;
import java.applet.*;


    public class Example extends Applet
    {
        Image tortoise, hare;
        int tortX = 250, hareX = 250; 
        final int tortY = 100, hareY = 300, WIDTH = 15, HEIGHT = 50;
        int turn; String turnNum;
        int move; String tMove, hMove;

        public void init()
        {
            tortoise = getImage( getDocumentBase(), "images/tortoise.gif" );
            hare = getImage( getDocumentBase(), "images/hare.gif" );
            move = 0; turn = 0;
        }


        public void control()
        {
            while (( tortX < 985 ) || ( hareX < 985 ))
            {
                move = (int)(10 * Math.random());
                switch (move)
                {
                    case 1:
                    case 2:
                        tortX += (3 * WIDTH);
                        hareX += (9 * WIDTH);
                        tMove = "Fast Plod"; hMove = "Big Hop";
                        break;
                    case 3:
                    case 4:
                    case 5:
                        tortX += (3 * WIDTH);
                        hareX += WIDTH;
                        tMove = "Fast Plod"; hMove = "Small Hop";
                        break;
                    case 6:
                        tortX += WIDTH;
                        if (hareX == 250) {} // DO NOTHING
                        else if (hareX <= (250 + (11 * WIDTH)))
                            hareX = 250;
                        else 
                            hareX -= (12 * WIDTH);
                        tMove = "Slow Plod"; hMove = "Big Slip";
                        break;
                    case 7:
                    case 8:
                        tortX += (1 * WIDTH);
                        if (hareX == 250) {} // DO NOTHING
                        else if (hareX <= (250 + (WIDTH)))
                            hareX = 250;
                        else 
                            hareX -= (2 * WIDTH);
                        tMove = "Slow Plod"; hMove = "Small Slip";
                        break;
                    case 9:
                    case 10:
                        if (tortX == 250) {} // DO NOTHING
                        else if (tortX <= (250 + (5 * WIDTH)))
                            tortX = 250;
                        else 
                            tortX -= (6 * WIDTH);
                        tMove = "Slip"; hMove = "Fall Asleep. Zzz...";
                        break;
                        // Hare falls asleep. No action. 
                }

                turn++; turnNum = (turn + "");
                repaint();
                for (int i = 1; i <= 10; i++)
                {
                    delay();
                }
            }

            tortX = 985; hareX = 985;
            repaint();
        }

        public void paint( Graphics screen )
        {
            drawRace(screen);

            if (tortX >= 985)
            {
                screen.setFont(new Font("Times New Roman", Font.ITALIC, 48));
                screen.drawString("Tortoise Wins", 650, 240);
                clearCurrent(screen); 
                fillNext(screen);
            }
            else if (hareX >= 985)
            {
                screen.setFont(new Font("Times New Roman", Font.ITALIC, 48));
                screen.drawString("Tortoise Wins", 650, 240);
                clearCurrent(screen); 
                fillNext(screen);
            }
            else
            {       
                screen.drawString(("Turn " + turnNum), 621, 55);
                screen.setFont(new Font("Times New Roman", Font.ITALIC, 12));
                screen.drawString(tMove, 59, 65); screen.drawString(hMove, 66, 255); 
                clearCurrent(screen);
                fillNext(screen);
            }

            stop();
        }

        public void clearCurrent( Graphics s )
        {
            s.clearRect(tortX+1, tortY+1, WIDTH-1, HEIGHT-1);
            s.clearRect(hareX+1, hareY+1, WIDTH-1, HEIGHT-1);

        }

        public void fillNext( Graphics s )
        {
            s.fillRect(tortX+1, tortY+1, WIDTH-1, HEIGHT-1);
            s.fillRect(hareX+1, hareY+1, WIDTH-1, HEIGHT-1);

        }

        public void drawRace( Graphics s )
        {
            // GENERATES INITIAL GRAPHICS FOR RACE
            s.drawRect(250, 100, 750, 50);
            s.drawRect(250, 300, 750, 50);
            int lineX = 265, lineYi = 100, lineYf = 150;
            for (int i = 1; i <= 98; i++)
            {
                if (lineX == 1000)
                {
                    lineX = 265; lineYi = 300; lineYf = 350;
                }
                s.drawLine(lineX, lineYi, lineX, lineYf);
                lineX += 15;
            }
            s.fillRect(tortX+1, tortY+1, WIDTH-1, HEIGHT-1);
            s.fillRect(hareX+1, hareY+1, WIDTH-1, HEIGHT-1);
            s.drawImage(tortoise, 59, 80, this);
            s.drawImage(hare, 66, 271, this);
            s.setFont(new Font("Times New Roman", Font.BOLD, 24));
            s.drawString("Race", 250, 55);
        }

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

        public void stop()
        {
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T03:11:03+00:00Added an answer on June 17, 2026 at 3:11 am

    Your first problem is you actually never “start” the race…Sure your init the applet, but then, nothing…

    Your second problem is the control method is block the Event Dispatching Thread, this means, they while you are in this method NOTHING will get painted to the screen. This is because the Event Dispatching Thread is also responsible for dispatching repaint requests.

    You third problem is your violating the paint contact. You have a responsibility to call super.paint(screen) – paint is a complex method and should never ignore it unless you have a REALLY good reason do to so.

    Your fourth problem is, you using an Applet instead of a JApplet. Better to ignore the AWT controls in favor of the Swing controls. Swing are more flexible and easier to extend.

    Your fifth problem is you painting onto a top level container, this is never recommended. You are better using something like JPanel and overriding it’s paintComponent method (don’t forget to call super.paintComponent). Apart from everything else, it’s double buffered and will reducing flicking as the screen is updated.

    Take a look at…

    • Concurrency in Swing and How to use Swing Timer to solve you EDT blocking issues…
    • Performing Custom Painting for ideas about painting in Swing
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am confused How to use looping for Json response Array in another Array.
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.