5 Checkerboard Write a program that takes a psotive integer

5. Checkerboard

Write a program that takes a psotive integer n and displays n x n checkerboard. For example, when n = 5, the program displays as in Figure15-30. Make sure that adjacent squares differ in color regardless of whether n is odd or even. Write the program in such a way that it works for different values of n by changing only one line in your code. Hint: Use un-editable TextField objects in a GridPane, and use CSS to set background colors.

Figure 15-30 Checkerboard (Mark Schrier. Copyright Oracle, Inc.) Checker… -

Solution

import java.awt.*; import java.applet.*; import java.util.*; 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 gr) { int row; int col; int x,y; Scanner row = new Scanner( System.in ); col=row; 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); } } } }
5. Checkerboard Write a program that takes a psotive integer n and displays n x n checkerboard. For example, when n = 5, the program displays as in Figure15-30.

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site