// Checkerboard.java // Solution to Short Assignment 9 by THC. // Draws a checkerboard with black and red squares. Whichever square is // clicked on flashes between its regular color and yellow. // Uses private inner classes as listeners. import java.applet.Applet; import java.awt.*; import java.awt.event.*; import javax.swing.Timer; public class Checkerboard extends Applet { private final int DIMENSION = 8; // Want 8 x 8 board private final int SQUARE_SIZE = 30; // Each square is 30 x 30 pixels private Point clickPoint; // Where the mouse was clicked private boolean clickedIsYellow; // true if we're drawing the clicked point in yellow private Timer flashing; // Timer for the flashing square public void init() { // Set the size of the applet to be exactly the checkerboard size. setSize(DIMENSION * SQUARE_SIZE, DIMENSION * SQUARE_SIZE); // Nothing is clicked yet. clickPoint = null; clickedIsYellow = false; // Register this applet as the listener for mouse clicks. addMouseListener(new ClickListener()); // Create the Timer for flashing and make this applet be the listener. final int DELAY = 500; flashing = new Timer(DELAY, new TimerListener()); flashing.start(); // Draw the initial checkerboard. repaint(); } public void paint(Graphics page) { // Draw all the black and red squares. drawCheckerboard(page); // If we've clicked a point, and if we're in part of the Timer cycle when // we draw in yellow, then draw it in yellow. if (clickPoint != null && clickedIsYellow) { int row = clickPoint.y / SQUARE_SIZE; // Row that was clicked on int column = clickPoint.x / SQUARE_SIZE; // Column that was clicked on drawSquare(page, row, column, Color.yellow); } } // Draw the entire checkerboard in black and red. public void drawCheckerboard(Graphics page) { Color squareColor; // Color of the current square for (int row = 0; row < DIMENSION; row++) { for (int column = 0; column < DIMENSION; column++) { // A square is black if the row and column numbers are both even // or both odd. Otherwise, the square is red. if (row % 2 == column % 2) squareColor = Color.black; else squareColor = Color.red; drawSquare(page, row, column, squareColor); } } } // Draw a given square of the checkerboard. Row and column numbers start // from 0. public void drawSquare(Graphics page, int row, int column, Color color) { page.setColor(color); page.fillRect(column * SQUARE_SIZE, row * SQUARE_SIZE, SQUARE_SIZE, SQUARE_SIZE); } // Private inner class to listen for mouse events. private class ClickListener implements MouseListener { // Handle a mouse click by remembering the clicked point and repainting. public void mouseClicked(MouseEvent event) { // Save where the click was. clickPoint = event.getPoint(); // Redraw the checkerboard with the square clicked on flashing yellow. clickedIsYellow = true; repaint(); } // Provide empty definitions for unused event methods. public void mousePressed(MouseEvent event) {} public void mouseReleased(MouseEvent event) {} public void mouseEntered(MouseEvent event) {} public void mouseExited(MouseEvent event) {} } // Private inner class to handle timer events. private class TimerListener implements ActionListener { // Handle a timer event by reversing whether we draw the clicked square in yellow // and then repainting. public void actionPerformed(ActionEvent event) { clickedIsYellow = !clickedIsYellow; repaint(); } } }