Hello, I'm writing a chess game and have got the board drawn but I am having trouble painting the pieces on the board.
Below is my code for the white pieces:
import javax.swing.*;
public class WhitePieces extends JLabel {
JLabel wPawn;
JLabel wBishop;
JLabel wKnight;
JLabel wRook;
JLabel wQueen;
JLabel wKing;
public JLabel wPawn() {
wPawn = new JLabel(new ImageIcon("wp.gif"));
return wPawn;
}
public JLabel wBishop() {
wBishop = new JLabel(new ImageIcon("wb.gif"));
return wBishop;
}
public JLabel wKnight() {
wKnight = new JLabel(new ImageIcon("wk.gif"));
return wKnight;
}
public JLabel wRook() {
wRook = new JLabel(new ImageIcon("wr.giv"));
return wRook;
}
public JLabel wQueen() {
wQueen = new JLabel(new ImageIcon("wq.gif"));
return wQueen;
}
public JLabel wKing() {
wKing = new JLabel(new ImageIcon("wk.gif"));
return wKing;
}
}
and this is the board painting part of the chess class:
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
double w = getWidth();
double h = getHeight();
double hInc = (w-1)/SIZE; //Horizontal Increment
double vInc = (h-1)/SIZE; //Vertical Increment
//Draw horizontal lines
for(int i = 0; i <= SIZE; i++) {
double x = i*hInc;
g2.draw(new Line2D.Double(x, 0, x, h));
}
//Draw vertical lines
for(int i = 0; i <= SIZE; i++) {
double y = i*vInc;
g2.draw(new Line2D.Double(0, y, w, y));
}
//Paint the board
for (board_pointx = 0; board_pointx < SIZE; board_pointx++){
for (board_pointy = 0; board_pointy < SIZE; board_pointy++) {
double x = board_pointx*hInc;
double y = board_pointy*vInc;
g2.setPaint(LIGHT_COLOUR);
g2.fill(new Rectangle2D.Double(x, y, hInc, vInc));
if (board_pointx%2 == 0 && board_pointy%2 == 0) {
g2.setPaint(DARK_COLOUR);
g2.fill(new Rectangle2D.Double.Double(x, y, hInc, vInc));
}
if (board_pointx%2 == 1 && board_pointy%2 == 1) {
g2.setPaint(DARK_COLOUR);
g2.fill(new Rectangle2D.Double.Double(x, y, hInc, vInc));
}
}
}
Any help would be great! Thanks