Need help with this program! Please give me the correct answer. Write a program
ID: 3572006 • Letter: N
Question
Need help with this program! Please give me the correct answer.
Write a program using the DrawingPanel that contains a method for drawing a checkerboard.
The checkerboard should be 160 units wide (and tall) and should have an input parameters that allow the method to place the checkerboard at any x and y coordinates. For example you should be able to do the following in your main program.
drawCheckerboard(100,100);
drawCheckerboard(120,500);
To draw two different checkerboards.
You may choose your own colors for the squares on the checkerboard.
Explanation / Answer
Hii there,
This may help you out
import java.awt.*;
import java.applet.*;
public class Checkboard extends Applet {
public void paint (Graphics gr)
{
int row; //for rows
int column; //for columns
int x,y; //variables for co-ordinates
for ( x = 0 ; x < 8 ; x++)
{
for (y=0 ; y < 8 ; y++)
{
x = column * 160 ; // 160 is the width of the checkboard
y= row * 160 ; // 160 is the height of the checkboad
if ((row % 2) == (column % 2))
gr.setColor(Color.black); //any random color
else
gr.setColor(Color.white); //any random color
gr.fillRect(x , y , 160, 160);
}
}
}
}