我的Java Tortoise和Hare Applet将无法运行

时间:2013-08-28 14:57:44

标签: java applet

我正在尝试创建/模拟乌龟与野兔种族。随机数生成器用于使竞争者移动....可能的移动是:右边3个方格,右边1个方格,左边6个方格,右边9个方格,右边1个方格,左边12个方格,剩下2个方格,睡着了。该课程以50个方格位置绘制,每个玩家拥有各自的车道并从位置1开始。我的问题是applet编译,但是当我尝试在浏览器中打开html文件时不会运行。我是在正确的轨道上吗?我如何让它运行....当它运行时,正确吗?

import java.awt.*;
import java.applet.*;
public class Project2 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) {} // Agit Nihil 
                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) {} // Agit Nihil
                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) {} // Agit nihil
                else if (tortX <= (250 + (5 * WIDTH)))
                    tortX = 250;
                else
                    tortX -= (6 * WIDTH);
                tMove = "Slip"; hMove = "Fall Asleep.";
                break;
                // Cuniculus dormit, agit nihil .
        }
        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 )
{
    // Initium
    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()
{
}

}

0 个答案:

没有答案