我正在用java编写一个必须在applet中显示的国际象棋程序。我目前在填充棋子数组时遇到问题。目前这是在我的 JApplet 的 paint() 方法中完成的,我知道这是错误的,因为可以多次调用 paint。我已经尝试创建数组并将其填充到我的初始化方法中,但这根本不起作用。任何帮助,将不胜感激。
public class DrawChessBoard extends JApplet
implements MouseListener, MouseMotionListener {
ChessPiece myPiece;
ImageIcon square;
ImageObserver observer;
ChessBoard gameBoard;
boolean isMouseDragging = false;
int size; //square dimensions
public void initialize() {
setBackground(Color.white);
Image bSquare = square.getImage();
size = bSquare.getWidth(observer);
addMouseListener(this);
addMouseMotionListener(this);
}
public void paint(Graphics h) {
Graphics2D g = (Graphics2D) h;
//System.out.println("Am I being called more than once?");
gameBoard = new ChessBoard(8);
gameBoard.start();
gameBoard.paintBoard(g);
gameBoard.paintComponent(g);
}
}