I'm writing a chess program in java that has to be displayed in an applet. I am currently having a problem with filling the array of chess pieces. This is currently being done in the paint() method of my JApplet and I know that's wrong because paint can be called more than once. I have tried creating the array and filling it in my initialize method but that doesn't work at all. Any help would be appreciated.
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);
}
}