I would like submitting a simple chess board drawing example using Unicode characters. There 3 classes involved into this tiny project.
ChessLabel.java
import java.awt.Color;
import java.awt.Font;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
public class ChessLabel extends JLabel {
Font font = new Font("Ariel", Font.PLAIN, 24);
Color bgLight = new Color(222, 184, 135);
Color bgDark = new Color(139, 69, 19);
ChessLabel(String s)
{
super(s);
}
void set(int idx, int row)
{
setFont(font);
setOpaque(true);
setBackground((idx+row)%2 == 0 ? bgDark : bgLight);
setHorizontalAlignment( SwingConstants.CENTER );
}
}
Board.java
import java.awt.*;
import javax.swing.JFrame;
public class Board extends JFrame {
//Initialise arrays to hold panels and images of the board
private ChessLabel[] labels = new ChessLabel[] {
// white
new ChessLabel("u2656"), new ChessLabel("u2658"), new ChessLabel("u2657"),
new ChessLabel("u2655"), new ChessLabel("u2654"), new ChessLabel("u2657"),
new ChessLabel("u2658"), new ChessLabel("u2656"), new ChessLabel("u2659"),
new ChessLabel("u2659"), new ChessLabel("u2659"), new ChessLabel("u2659"),
new ChessLabel("u2659"), new ChessLabel("u2659"), new ChessLabel("u2659"),
new ChessLabel("u2659"),
// empty
new ChessLabel(" "), new ChessLabel(" "), new ChessLabel(" "),
new ChessLabel(" "), new ChessLabel(" "), new ChessLabel(" "),
new ChessLabel(" "), new ChessLabel(" "), new ChessLabel(" "),
new ChessLabel(" "), new ChessLabel(" "), new ChessLabel(" "),
new ChessLabel(" "), new ChessLabel(" "), new ChessLabel(" "),
new ChessLabel(" "), new ChessLabel(" "), new ChessLabel(" "),
new ChessLabel(" "), new ChessLabel(" "), new ChessLabel(" "),
new ChessLabel(" "), new ChessLabel(" "), new ChessLabel(" "),
new ChessLabel(" "), new ChessLabel(" "), new ChessLabel(" "),
new ChessLabel(" "), new ChessLabel(" "), new ChessLabel(" "),
new ChessLabel(" "), new ChessLabel(" "),
// black
new ChessLabel("u265F"), new ChessLabel("u265F"), new ChessLabel("u265F"),
new ChessLabel("u265F"), new ChessLabel("u265F"), new ChessLabel("u265F"),
new ChessLabel("u265F"), new ChessLabel("u265F"), new ChessLabel("u265C"),
new ChessLabel("u265E"), new ChessLabel("u265D"), new ChessLabel("u265B"),
new ChessLabel("u265A"), new ChessLabel("u265D"), new ChessLabel("u265E"),
new ChessLabel("u265C")
};
public Board()
{
} // Board()
void display()
{
setTitle("Chess board with unicode images");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Container contentPane = getContentPane();
GridLayout gridLayout = new GridLayout(8, 8);
contentPane.setLayout(gridLayout);
int row = -1;
for (int i = 0; i < labels.length; i++)
{
if(i % 8 == 0) row ++; // increment row number
labels[i].set(i, row);
contentPane.add(labels[i]);
} // i
setSize(600, 600);
setLocationRelativeTo(null);
setVisible(true);
} // display()
} // class Board
And ChessBoardTest.java
public class ChessBoardTest {
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
Board board = new Board();
board.display();
}
}