0

我无法模拟两个竞争对手之间的比赛。这是您的典型比赛计划,您使用随机数生成器来确定竞争对手使用的“移动”。如下面的代码所示,赛道由 50 个矩形组成,填充的矩形显示了每个参赛者在赛道上的位置。例如,一些“动作”使参赛者向右跳 9 个方格,或向后跳 2 个方格。当我运行小程序时,只显示初始起始位置;小程序不起作用。我意识到这是很多代码,但我需要做些什么来解决我的问题?我真的被困在这一点上。任何帮助表示赞赏。我只能用AWT,不能swing。这是类的作业:/这是代码:

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()
        {
        }
    }
4

1 回答 1

3

你的第一个问题是你实际上从来没有“开始”比赛......当然是你init的小程序,但是,什么都没有......

您的第二个问题是该control方法阻止了事件调度线程,这意味着,当您使用此方法时,它们不会被绘制到屏幕上。这是因为事件调度线程也负责调度重绘请求。

你的第三个问题是你违反了paint联系。你有责任打电话super.paint(screen)——paint 是一种复杂的方法,除非你有充分的理由这样做,否则永远不要忽视它。

您的第四个问题是,您使用的Applet是 a 而不是 a JApplet。最好忽略 AWT 控件以支持 Swing 控件。Swing 更灵活,更容易扩展。

您的第五个问题是您在顶级容器上绘画,这是不推荐的。你最好使用类似的东西JPanel并覆盖它的paintComponent方法(不要忘记调用super.paintComponent)。除此之外,它是双缓冲的,并且会随着屏幕的更新而减少闪烁。

看一眼...

于 2013-01-10T22:41:59.240 回答