Write a program named CheckerBoard.java. The checkerboard class will create and
ID: 3854412 • Letter: W
Question
Write a program named CheckerBoard.java. The checkerboard class will create and display a checkerboard using a two-dimensional array. The first dimension will be the X axis and the second dimension will be the Y axis. Display the board to the console by printing the letters “B” and “W” every other one, indicating black and white locations on the board. Below is an example of the expected output:
B W B W B W B W
W B W B W B W B
B W B W B W B W
W B W B W B W B
B W B W B W B W
W B W B W B W B
B W B W B W B W
W B W B W B W B
Explanation / Answer
The required code for the checkboard is as follows. Hope you'll understand the code and manage to draw it on your own.Here it goes,
checkBoard.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package checkerboard;
import java.awt.*;
import java.applet.*;
/**
*
* @author Akshay Bisht
*/
public class checkBoard extends Applet {
/* This applet draws a Yellow-Black CheckerBoard.
The size of the applet is 160/160 pixels.
*/
public void paint(Graphics g) {
int vertical; // Row Numbers, from 0 to 7
int horiontal; // Column Numbers, from 0 to 7
int a,b; // These are for the top left corner
for ( vertical = 0; vertical < 8; vertical++ ) {
for ( horiontal = 0; horiontal < 8; horiontal++) {
a = horiontal * 20;
b = vertical * 20;
if ( (vertical % 2) == (horiontal % 2) )
g.setColor(Color.yellow);
else
g.setColor(Color.black);
g.fillRect(a, b, 20, 20);
}
} // end of row
} // end of paint
} // end of class
Please rate the answer if it helps.....Thankyou
Hope it helps.....
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.