Draw a Chessboard in Java Applet
Java Programming

Draw a Chessboard in Java Applet

 

Java Program for Chess Game

Each square(rectangle) in the chessboard is 20 pixels by 20 pixels. The squares are grey and black. If the row and column are either even or odd, then the color is Grey. Otherwise, change the color to Black.

import java.awt.*;

import java.applet.*;

public class Checkerboarrd extends Applet

{

   public void paint(Graphics gra)

{

      int roow;   // 7 Rows

      int cool;   // 7 Columns

      int x_axis,y_axis;   // Top-left corner of square

      for ( roow = 5;  roow < 13;  roow++ )

          {

                 for ( cool = 5;  cool < 13;  cool++)

                {

                   x_axis = cool * 20;

                   y_axis = roow * 20;

                 if ( (roow % 2) == (cool % 2) )

                    gra.setColor(Color.gray);//if condition true then (even)color of the rectangle is gray

               else

                   gra.setColor(Color.black);//if condition false(odd)then the color of the rectangle is black

                  gra.fillRect(x_axis, y_axis, 20, 20);

              }

         } 

     }

}

//<applet code=”Checkerboarrd.class” height=500 width=500></applet>

Chessboard in Java

gra.setColor(c) is called to set the color that is used in drawing

 gra.fillRect(x_axis, y_axis, w(20), h(20)) fills the inside of the rectangle instead of just drawing an outline.

Recommended Posts

C++ Programming

Visibility modes in C++

In C++, visibility modes refer to the accessibility of class members (such as variables and functions) from different parts of a program. C++ provides three visibility modes: public, private, and protected. These modes control the access levels of class members concerning the outside world and derived classes. Public: Members declared as public are accessible from […]

Rekha Setia 
C++ Programming

Inheritance in C++

In C++, inheritance is a fundamental concept of object-oriented programming (OOP) that allows you to create a new class based on an existing class, known as the base or parent class. The new class is called the derived or child class. Inheritance facilitates code reuse and supports the creation of a hierarchy of classes. There […]

Rekha Setia 
C++ Programming

C++ Classes and Objects

In C++, classes and objects are fundamental concepts that support object-oriented programming (OOP). Here’s a brief overview of classes and objects in C++: Classes: In C++, a class is a user-defined data type that allows you to encapsulate data members and member functions into a single unit. Classes are the building blocks of object-oriented programming […]

Rekha Setia 

Leave A Comment