1. Chess Board Write a program that draws an 8 X 8 chess board, with alternating
ID: 3857412 • Letter: 1
Question
1. Chess Board
Write a program that draws an 8 X 8 chess board, with alternating gray and white squares. You can use the SetTextColor and Gotoxy procedures from the Irvine32 library. Avoid the use of global variables, and use declared parameters in all procedures. Use short procedures that are focused on a single task. ( A videonote for this exercise is posted on the web site.)
2. Chess Board with Alternating Colors
this exercise extends Exercise 9. Every 500 milliseconds, change the color of the colored squares and redisplay the board. Continue until you have shown the board 16 times, using all possible 4-bit background colors.( The white squares remain white throughout.)
Explanation / Answer
import javax.swing.*; import java.awt.Graphics; import java.awt.Color; public class CheckerBoard extends JFrame { public void paint(Graphics g) { int row; int col; int x; int y; for ( row = 0; row < 9; row++ ) { for ( col = 0; col < 8; col++) { x = col * 22; y = row * 22; if ( (row % 2) == (col % 2) ) g.setColor(Color.WHITE); else g.setColor(Color.BLACK); g.fillRect(x, y, 22, 22); } } } public static void main(String args[]) { CheckerBoard check = new CheckerBoard(); check.setTitle("CheckerBoard"); check.setSize(180, 200); check.setDefaultCloseOperation(EXIT_ON_CLOSE); check.setVisible(true); } }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.