Paint the pieces
As next we have to paint the pieces. Each piece is saved in a image with transparent background. The images that we use on this site are from ArtToday and are protected by international copyright laws, so we can't offer you a seperate download of the images.
Let's go to the code. First we have to encode the pieces in a number. We make the following conventions:
- The 1th digit saves the ability to castle (moved / not moved)
- The 2th digit saves the color of the piece. 1 is for withe, 2 for black
- The 3th digit saves the type of a piece: 1 for pawn, 2 for knight, 3 for bishop, 4 for rook, 5 for queen and 6 for king.
Now we still have an empty board. In the method
newgame (), we build the start position:
//build start position
int [] org = {
99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
99,124, 22, 23, 25,126, 23, 22,124, 99,
99, 21, 21, 21, 21, 21, 21, 21, 21, 99,
99, 00, 00, 00, 00, 00, 00, 00, 00, 99,
99, 00, 00, 00, 00, 00, 00, 00, 00, 99,
99, 00, 00, 00, 00, 00, 00, 00, 00, 99,
99, 00, 00, 00, 00, 00, 00, 00, 00, 99,
99, 11, 11, 11, 11, 11, 11, 11, 11, 99,
99,114, 12, 13, 15,116, 13, 12,114, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99 };
for (int i=0; i < 120; i++)
board [i] = org [i];
//update screen
repaint ();Of course we also have to load the pieces. Because we wan't to display the pieces really often, we save them in an array. We declare the array in the global namespace of the class Board. To load the pieces we need reference of the Applet. We save the reference in the variable parent.
//the images
Image [] pieces = new Image [18];
//reference of the applet
Applet parent;The constructor of the class Board loads the images and makes parent pointing to the applet:
//load images
MediaTracker controler = new MediaTracker (ref);
pieces [1] = ref.getImage (ref.getCodeBase (),"images/white_pawn.gif");
pieces [2] = ref.getImage (ref.getCodeBase (),"images/white_knight.gif");
pieces [3] = ref.getImage (ref.getCodeBase (),"images/white_bishop.gif");
pieces [4] = ref.getImage (ref.getCodeBase (),"images/white_rook.gif");
pieces [5] = ref.getImage (ref.getCodeBase (),"images/white_queen.gif");
pieces [6] = ref.getImage (ref.getCodeBase (),"images/white_king.gif");
pieces [11] = ref.getImage (ref.getCodeBase (),"images/black_pawn.gif");
pieces [12] = ref.getImage (ref.getCodeBase (),"images/black_knight.gif");
pieces [13] = ref.getImage (ref.getCodeBase (),"images/black_bishop.gif");
pieces [14] = ref.getImage (ref.getCodeBase (), "images/black_rook.gif");
pieces [15] = ref.getImage (ref.getCodeBase (), "images/black_queen.gif");
pieces [16] = ref.getImage (ref.getCodeBase (), "images/black_king.gif");
for (int i = 1; i < 7; i++) {
controler.addImage (pieces [i], 0);
controler.addImage (pieces [i + 10], 0);
}
try {
controler.waitForAll (); //wait for appletsr geladen wurden
} catch (InterruptedException e) {
controler.checkID (0, true);
}
//set reference
parent = ref;Now we only have to paint the pieces in the
paintField ()method:
//paint pieces
try {
g.drawImage (pieces [board [index] % 100 - 10], x * 40, y * 40, 40, 40, parent);
} catch ( ArrayIndexOutOfBoundsException e) {}show applet step 3
souce code of step 3