开始:Applet未初始化Checkers游戏

时间:2015-11-17 16:11:34

标签: java applet

所以我正在制作一个玩Checkers游戏的小程序,在这个阶段我只是想设置棋盘。我的代码编译,但是当我打开AppletViewer时,我收到一条消息“Start:Applet not initialized”。知道为什么?

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

public class CheckerGame extends Applet implements MouseListener {
int player = 1;
Checker [] [] board = new Checker [8][8];
int mouseX = -200;
int mouseY = -200;
int col;
int row;

Image whiteChecker;
Image redChecker;
Image boardBG;
Image whiteKing;
Image redKing;
public void init()
{
    this.addMouseListener(this);
    boardBG=this.getImage(this.getCodeBase(),"checkerboard.jpg");
    whiteChecker = this.getImage(this.getCodeBase(),"whiteChecker.png");
    redChecker = this.getImage(this.getCodeBase(),"redChecker.png");
    whiteKing = this.getImage(this.getCodeBase(),"whiteKing.png");
    redKing = this.getImage(this.getCodeBase(),"redKing.png");
    for(int r = 0; r < 8; r++)
    {
        for (int c = 0; c < 8; c++)
        {
            if(r < 3 && r%2 == 0 && c%2 == 1)
            {
                board[r][c].setPlayer(2);
                board[r][c].setKing(false);
            }
            else if (r == 1 && c%2 == 0)
            {
                board[r][c].setPlayer(2);
                board[r][c].setKing(false);
            }
            else if (r > 4 && r%2 == 1 && c%2 == 0)
            {
                board[r][c].setPlayer(1);
                board[r][c].setKing(false);
            }
            else if (r == 6 && c%2 == 1)
            {
                board[r][c].setPlayer(1);
                board[r][c].setKing(false);
            }
            else
            {                
                board[r][c].setPlayer(0);
                board[r][c].setKing(false);
            }
        }
    }
    this.setLayout(null);
}

public void mouseClicked(MouseEvent e) {
    mouseX=e.getX();
    mouseY=e.getY();
    col = (mouseX-100)/100;
    row = (mouseY-100)/100;
}

public void mousePressed(MouseEvent e) {
}

public void mouseReleased(MouseEvent e) {
}

public void mouseEntered(MouseEvent e) {
}

public void mouseExited(MouseEvent e) {
}

public void paint(Graphics g)
{
    g.drawImage(boardBG,100,100,800,800,this);
    for(int r = 0; r < 7; r++)
    {
        for(int c = 0; c < 7; c++)
        {
            if(board[r][c].getPlayer() == 2 && board[r][c].getKing() == false)
            {
                g.drawImage(whiteChecker,100+100*c,100+100*r,40,40,this);
            }
            else if(board[r][c].getPlayer() == 2 && board[r][c].getKing() == true)
            {
                g.drawImage(whiteKing,100+100*c,100+100*r,40,40,this);
            }
            if(board[r][c].getPlayer() == 1 && board[r][c].getKing() == false)
            {
                g.drawImage(redChecker,100+100*c,100+100*r,40,40,this);
            }
            else if(board[r][c].getPlayer() == 1 && board[r][c].getKing() == true)
            {
                g.drawImage(redKing,100+100*c,100+100*r,40,40,this);
            }
        }
    }
}

}

1 个答案:

答案 0 :(得分:2)

Vollmilchbb是正确的。您init()方法出错。

您犯了一个基本的Java错误。 虽然您已经创建了一个Checker数组但没有实例化这些元素。 虽然您未在此处向我们显示输出日志(控制台日志),但您会在这些日志中找到NullPointerException

它只是代码的一部分,但将代码置于init()方法中的块下面的代码中。

board[r][c] = new Checker()

<强>规则:
Java中的对象数组(除了像String数组这样的最终类对象数组)只是为数组所声明的类型的对象创建了占位符(指针可以说)。在访问对象之前,您必须设置一个对象或实例化一个对象。