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>
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.