这是主要代码

import java.awt.*;

import javax.swing.*;
import java.awt.event.*;
import java.applet.Applet;

import java.applet.*;
import java.awt.image.*;
import java.util.*;
import java.net.*;
public class AlcatrazBreakout extends JApplet implements KeyListener, FocusListener  {
    int choice=0,curloc=0;
    boolean focus = false;
    boolean left=false, right=false, up=false, down=false, space=false, enter=false;

    public double randNum(Graphics canvas)
    {
        double x;
        x=Math.random()*100;
        return x;
    }
    public void init()
    {

        addKeyListener(this);
        addFocusListener(this);
        play(getCodeBase(), "SpyHunter.mid");

    }
    public void paint(Graphics canvas)
    {
        clearScreen(canvas);

        canvas.setColor(Color.BLACK);  // Must do this so words will
                                       // display on the screen

         if (!focus)
         {
            canvas.setColor(Color.WHITE);
            canvas.drawString("Welcome to Alcatraz Breakout! The objective of the game is to escape Alcatraz.",10,10);
            canvas.drawString("Click the screen to begin the game.",10,30);
            return;
         }
         else if (focus)
         {
             startbody(canvas);
         }


     }
    public void showstats(Graphics canvas)
    {
        int hp=10,hpmax=10,defense=0,attack=0;
        canvas.drawString("HP: " + hp ,1750,10);
        canvas.drawString("/" + hpmax,1790,10);
        canvas.drawString("Attack: " + attack,1750,30);
        canvas.drawString("Defense: " + defense,1750,50);
    }
    public void startbody(Graphics canvas)
    {
        space=enter=left=right=up=down=false;
        clearScreen(canvas);
        canvas.setColor(Color.WHITE);
        showstats(canvas);
        canvas.drawString("You are in your cell.",0,10);
        canvas.drawString("Left: To the Training Room.",0,30);
        canvas.drawString("Right: To the Showers",0,50);
        curloc=0;
        makeMove(canvas,curloc, left,right,up,down);
        canvas.drawString(" "+curloc,0,70);
    }
    public void train(Graphics canvas)
    {
        System.out.println("You are at train");
        space=enter=left=right=up=down=false;
        clearScreen(canvas);
        canvas.setColor(Color.WHITE);
        showstats(canvas);
        canvas.drawString("You are in the training room.",0,10);
        canvas.drawString("Right: To your cell.",0,30);
        makeMove(canvas,curloc,left,right,up,down);

    }
    public void showers(Graphics canvas)
    {
        System.out.println("You are in the showers");
        space=enter=left=right=up=down=false;
        clearScreen(canvas);
        canvas.setColor(Color.WHITE);
        showstats(canvas);
        canvas.drawString("You are in the showers.",0,10);
        canvas.drawString("Left: To your cell.",0,30);
        curloc=1;
        makeMove(canvas,curloc, left,right,up,down);
    }
    public void cellBlockC(Graphics canvas)
    {
        space=enter=left=right=up=down=false;
        clearScreen(canvas);
        canvas.setColor(Color.WHITE);
        canvas.drawString("You are in CellBlock C", 0, 10);
        canvas.drawString("Left: Back to your cell",0,20);
        canvas.drawString("Up: to security room 1", 0, 40);
        curloc=2;
        makeMove(canvas,curloc, left,right,up,down);
    }
    public void SecRoom1(Graphics canvas)
    {
        space=enter=left=right=up=down=false;
        clearScreen(canvas);
        canvas.setColor(Color.WHITE);
        canvas.drawString("Your in Secrity room 1.", 0, 10);
        canvas.drawString("Down: Back to Cell Block C",0,20);
        canvas.drawString("Up: Left to Guard Post", 0, 40);
        curloc=3;
        makeMove(canvas,curloc, left,right,up,down);
    }
    public void tunnel(Graphics canvas)
    {
        space=enter=left=right=up=down=false;
        clearScreen(canvas);
        canvas.setColor(Color.WHITE);
        canvas.drawString("Your in Secrity room 1.", 0, 10);
        canvas.drawString("Down: Back to Cell Block C",0,20);
        canvas.drawString("Up: Left to Guard Post", 0, 40);
        curloc=7;
        makeMove(canvas,curloc, left,right,up,down);
    }
    public void guardPost(Graphics canvas)
    {
        space=enter=left=right=up=down=false;
        clearScreen(canvas);
        canvas.setColor(Color.WHITE);
        canvas.drawString("Your in the Guard Post.", 0, 10);
        canvas.drawString("Left: to go to the Court Yard",0,20);
        canvas.drawString("Right: to go to Security room 1", 0, 40);
        canvas.drawString("Up: to go to Solitary Confinement",0,60);
        curloc=4;
        makeMove(canvas,curloc, left,right,up,down);
    }
    public void courtyard(Graphics canvas)
    {
        space=enter=left=right=up=down=false;
        clearScreen(canvas);
        canvas.setColor(Color.WHITE);
        canvas.drawString("Your in the Court Yard", 0, 10);
        canvas.drawString("Down: to Security Room 2.",0,20);
        canvas.drawString("Up: To Guard Post", 0, 40);
        curloc=8;
        makeMove(canvas,curloc, left,right,up,down);
    }
    public void SecRoom2(Graphics canvas)
    {
        space=enter=left=right=up=down=false;
        clearScreen(canvas);
        canvas.setColor(Color.WHITE);
        canvas.drawString("Your in Security Room 2.", 0, 10);
        canvas.drawString("Up: To the Court Yard", 0, 40);
        curloc=9;
        makeMove(canvas,curloc, left,right,up,down);
    }
    public void fight(Graphics canvas)
    {
        space=enter=left=right=up=down=false;
        clearScreen(canvas);
        canvas.setColor(Color.WHITE);
        canvas.drawString("Your at the Final Fight!!!", 0, 10);
        curloc=11;
        makeMove(canvas,curloc, left,right,up,down);
    }
    public void free(Graphics canvas)
    {
        space=enter=left=right=up=down=false;
        clearScreen(canvas);
        canvas.setColor(Color.WHITE);
        canvas.drawString("You Won!!!!", 0, 10);
        curloc=11;
        makeMove(canvas,curloc, left,right,up,down);
    }
    public void solitaryConfinement(Graphics canvas)
    {
        space=enter=left=right=up=down=false;
        clearScreen(canvas);
        canvas.setColor(Color.WHITE);
        canvas.drawString("Your in Solitary Confinement.", 0, 10);
        canvas.drawString("Down: Guard Post",0,20);
        curloc=5;
        makeMove(canvas,curloc, left,right,up,down);
    }
    public void makeMove(Graphics canvas,int curloc,boolean left, boolean right, boolean up, boolean down )
    {
        if (curloc==0)
        {
            if(left)
            {
                train(canvas);
                left=right=up=down=false;
            }
        }
    }
     public void delayGame(int numSecs)
     {
        int delayTime = 1000 * numSecs;
        long startDelay = System.currentTimeMillis();
        long endDelay = 0;

        while (endDelay - startDelay < delayTime)
        {
          endDelay = System.currentTimeMillis();
        }
     }
     public void clearScreen(Graphics g)
     {
        g.setColor(Color.BLACK);
        g.fillRect(0,0,5000,5000);
     }
     public void keyPressed(KeyEvent e)
     {
      int key = e.getKeyCode();


      if (key == KeyEvent.VK_LEFT)
      {
        left = true;
        repaint();
      }

      if (key == KeyEvent.VK_DOWN)
      {
        down = true;
        repaint();
      }

      if (key == KeyEvent.VK_RIGHT)
      {
        right = true;
        repaint();
      }

      if (key == KeyEvent.VK_UP)
      {
        up = true;
        repaint();
      }

      if (key == KeyEvent.VK_SPACE)
      {
        space = true;
        repaint();
      }

      if (key == KeyEvent.VK_ENTER)
      {
        enter = true;
        repaint();
      }

     }
     public void focusGained(FocusEvent evt) {focus=true; repaint();}
     public void focusLost(FocusEvent evt) {focus=false;}
     public void keyReleased(KeyEvent e){}
     public void keyTyped(KeyEvent e) {}
     public void restart(Graphics g){}

}


因此,curloc的编号不会传递到makeMove中,并且程序键也无法正常工作。
谢谢
克里斯

编辑:

你在火车上
[消息重复1075次]
你在火车上
线程“ AWT-EventQueue-1”中的异常java.lang.StackOverflowError
    在sun.nio.cs.SingleByteEncoder.encodeArrayLoop(未知来源)
    在sun.nio.cs.SingleByteEncoder.encodeLoop(未知来源)
    在java.nio.charset.CharsetEncoder.encode(未知来源)
    在sun.nio.cs.StreamEncoder.implWrite(未知来源)
    在sun.nio.cs.StreamEncoder.write(未知来源)
    在java.io.OutputStreamWriter.write(未知源)
    在java.io.BufferedWriter.flushBuffer(未知来源)
    在java.io.PrintStream.write(未知源)
    在java.io.PrintStream.print(未知来源)
    在java.io.PrintStream.println(未知来源)
    在AlcatrazBreakout.train(AlcatrazBreakout.java:75)
    在AlcatrazBreakout.makeMove(AlcatrazBreakout.java:196)
    在AlcatrazBreakout.train(AlcatrazBreakout.java:81)
    在AlcatrazBreakout.makeMove(AlcatrazBreakout.java:196)
    在AlcatrazBreakout.train(AlcatrazBreakout.java:81)
    在AlcatrazBreakout.makeMove(AlcatrazBreakout.java:196)
    在AlcatrazBreakout.train(AlcatrazBreakout.java:81)
    在AlcatrazBreakout.makeMove(AlcatrazBreakout.java:196)
    在AlcatrazBreakout.train(AlcatrazBreakout.java:81)
    在AlcatrazBreakout.makeMove(AlcatrazBreakout.java:196)
    在AlcatrazBreakout.train(AlcatrazBreakout.java:81)
    在AlcatrazBreakout.makeMove(AlcatrazBreakout.java:196)
    在AlcatrazBreakout.train(AlcatrazBreakout.java:81)
    在AlcatrazBreakout.makeMove(AlcatrazBreakout.java:196)
    在AlcatrazBreakout.train(AlcatrazBreakout.java:81)
    在AlcatrazBreakout.makeMove(AlcatrazBreakout.java:196)
    在AlcatrazBreakout.train(AlcatrazBreakout.java:81)


这是我在Eclipse中的控制台输出

最佳答案

你不应该:


重写applet的paint()方法。通过重写JPanel的paintComponent()方法来完成自定义绘制,然后将面板添加到小程序中。
使用紧密循环来“延迟”游戏。而是使用Swing计时器来安排游戏的下一步。

07-24 09:20