Java竞赛小程序无效

时间:2013-01-10 22:15:28

标签: java applet

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

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 个答案:

答案 0 :(得分:3)

你的第一个问题是你实际上从未“开始”比赛......确定你的init小程序,但是没有......

你的第二个问题是control方法是阻止事件调度线程,这意味着,当你在这个方法中时,NOTHING将被绘制到屏幕上。这是因为Event Dispatching Thread还负责调度重绘请求。

第三个问题是您违反了paint联系人。你有责任调用super.paint(screen) - paint是一种复杂的方法,除非你有充分的理由这样做,否则不应该忽略它。

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

你的第五个问题是你画在顶层容器上,这是绝对不推荐的。您最好使用JPanel之类的内容并覆盖它的paintComponent方法(不要忘记调用super.paintComponent)。除了其他一切,它是双缓冲的,并且会在屏幕更新时减少轻弹。

看看......