Learn how to Draw a Chessboard in Java (Swing/AWT)

In this article, We will learn how to draw a Chessboard in Java Swing and AWT. The chessboard should consist of 8 x 8 squares with alternating light and dark colors. Below is the Java code implementing the Chessboard GUI using Java Swing.

Like in the image below:

Code

import javax.swing.*;
import java.awt.*;

public class Chessboard extends JFrame {

    public Chessboard() {
        setTitle("Chessboard");
        setSize(400, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new GridLayout(8, 8));

        createChessboard();
        setVisible(true);
    }

    private void createChessboard() {
        Color lightColor = new Color(255, 255, 255);  // Light square color (white)
        Color darkColor = new Color(139, 69, 19);     // Dark square color (brown)

        for (int row = 0; row < 8; row++) {
            for (int col = 0; col < 8; col++) {
                JPanel square = new JPanel();
                square.setPreferredSize(new Dimension(50, 50));

                // Set background color based on row and column to create the checkerboard pattern
                if ((row + col) % 2 == 0) {
                    square.setBackground(lightColor);
                } else {
                    square.setBackground(darkColor);
                }

                add(square);
            }
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(Chessboard::new);
    }
}

Explanation about How to draw a Chessboard in Java

  1. Setting Up the Frame:
    • The Chessboard in Java class extends JFrame to create the main window.
    • The setTitle and setSize methods set the title and size of the frame.
    • setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ensures that the application terminates when the window is closed.
    • setLayout(new GridLayout(8, 8)) configures the frame layout as an 8×8 grid.
  2. Creating the Clickable :
    • The createClickableChessboard method generates the graphical representation of the chessboard in Java.
    • Light and dark colors are defined for the squares.
    • A nested loop iterates over each row and column, creating a panel for each square.
    • Buttons are added to the panels, and an action listener is attached to each button.
    • The action listener displays a message indicating the clicked square’s row and column using JOptionPane.
  3. Running the Application:
    • The main method invokes the SwingUtilities.invokeLater to run the GUI on the event dispatch thread.

Quick Go Through

FunctionsExplanation
ChessboardThis class extends JFrame, representing the main frame of our application.
Constructorpublic Chessboard() Sets up the frame properties such as title, size, default close operation, and layout, and invokes the method to create the chessboard in Java.
Creating the Chessboardprivate void createChessboard() Generates the chessboard by iterating through rows and columns.
Color PropertiesColor lightColor Represents the color for light squares (white). Color darkColor Represents the color for dark squares (brown).
Grid LayoutsetLayout(new GridLayout(8, 8)) Sets the layout of the frame to an 8×8 grid, ensuring each square fits nicely.
Square CreationNested loops iterate through each row and column. JPanel square Represents each square on the chessboard in Java. square.setPreferredSize(new Dimension(50, 50)) Sets the preferred size of each square to 50×50 pixels.
Coloring SquaresAlternates the background color based on the sum of row and column indices. Even sum: Light square color. Odd sum: Dark square color.
Launching the Applicationpublic static void main(String[] args) Invokes the application using SwingUtilities.invokeLater() for thread safety.

GitHub

Visit our GitHub Repository by clicking on the button below for more projects.