JAVA problem Write a program using the DrawingPanel that contains a method for d
ID: 3861312 • Letter: J
Question
JAVA problem
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, myG);
drawCheckerboard(120,500, myG);
To draw two different checkerboards.
You may choose your own colors for the squares on the checkerboard.
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. draw Checkerboard (100,100, myG); draw Checkerboard (120,500, myG); To draw two different checkerboards. You may choose your own colors for the squares on the checkerboardExplanation / Answer
Applet code:
<applet code="Checkerboard.class" width=160 height=160>
</applet>
Main java program:
import java.awt.*;
import java.applet.*;
public class Checkerboard extends Applet {
/* This applet draws a red-and-black checkerboard.
It is assumed that the size of the applet is 160
by 160 pixels.
*/
public void paint(Graphics g) {
int row; // Row number, from 0 to 7
int col; // Column number, from 0 to 7
int x,y; // Top-left corner of square
for ( row = 0; row < 8; row++ ) {
for ( col = 0; col < 8; col++) {
x = col * 20;
y = row * 20;
if ( (row % 2) == (col % 2) )
g.setColor(Color.red);
else
g.setColor(Color.black);
g.fillRect(x, y, 20, 20);
}
} // end for row
} // end paint()
} // end class
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.