Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts
Tuesday, July 12, 2011
Monday, July 11, 2011
Sunday, June 26, 2011
How to write a chessboard in JAVA?
import javax.swing.*;
import java.awt.*;
public class ChessBoard {
JFrame frame;
JPanel squares[][] = new JPanel[8][8];
public ChessBoard() {
frame = new JFrame("Chess Board");
frame.setSize(500, 500);
frame.setLayout(new GridLayout(8, 8));
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
squares[i][j] = new JPanel();
if ((i + j) % 2 == 0) {
squares[i][j].setBackground(Color.black);
} else {
squares[i][j].setBackground(Color.white);
}
frame.add(squares[i][j]);
}
}
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
new ChessBoard();
}
}
import java.awt.*;
public class ChessBoard {
JFrame frame;
JPanel squares[][] = new JPanel[8][8];
public ChessBoard() {
frame = new JFrame("Chess Board");
frame.setSize(500, 500);
frame.setLayout(new GridLayout(8, 8));
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
squares[i][j] = new JPanel();
if ((i + j) % 2 == 0) {
squares[i][j].setBackground(Color.black);
} else {
squares[i][j].setBackground(Color.white);
}
frame.add(squares[i][j]);
}
}
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
new ChessBoard();
}
}
Subscribe to:
Posts (Atom)
