Draw a Chessboard in Java Applet

Given task is to draw a Chessboard in Java Applet


  1. Create a rectangle with length and breadth of 20 unit each, with 10 rows and columns of chess.
  2. As soon as even position occurs in row and column change the color of a rectangle with BLACK, else it will be WHITE

Below is the implementation of the above approach:

Applet Program:





import java.awt.*;
import java.applet.*;
// Extends Applet Class
public class Chess extends Applet {
    static int N = 10;
    // Use paint() method
    public void paint(Graphics g)
        int x, y;
        for (int row = 0; row < N; row++) {
            for (int col = 0; col < N; col++) {
                // Set x coordinates of rectangle
                // by 20 times
                x = row * 20;
                // Set y coordinates of rectangle
                // by 20 times
                y = col * 20;
                // Check whether row and column
                // are in even position
                // If it is true set Black color
                if ((row % 2 == 0) == (col % 2 == 0))
                // Create a rectangle with
                // length and breadth of 20
                g.fillRect(x, y, 20, 20);



Note: To run the applet in command line use the following commands

> javac Chess.java
> appletviewer Chess.html

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.