/********************************************************************** * Created by: Eric Gilbert * Date: 1/24/99 * Designed to mimic the game Deluxe Lights Out(r) by Tiger Electronics **********************************************************************/ import java.applet.*; import java.awt.*; import java.awt.event.*; import java.util.Random; public class LightsOut extends Applet implements ActionListener { Button[][] lights = new Button[6][6]; Color mostlyRed = new Color(255, 50, 50); Color veryDarkGray = Color.gray; public void init() { // Color and layout the buttons on the screen setLayout(new GridLayout(6, 6)); // Make the screen a grid double choice; Random generator = new Random(); for(int i = 0; i < 6; i++) for(int j = 0; j < 6; j++) { choice = generator.nextDouble(); lights[i][j] = new Button(""); if (choice < 0.5) lights[i][j].setBackground(veryDarkGray); else lights[i][j].setBackground(mostlyRed); add(lights[i][j]); lights[i][j].addActionListener(this); } } public void changeColor(Button toChange) { // Invert Color of Button if (toChange.getBackground() == mostlyRed) toChange.setBackground(veryDarkGray); else toChange.setBackground(mostlyRed); } public void actionPerformed(ActionEvent e) { int row = 0, column = 0; // Where did the mouse push the button? for(int i = 0; i < 6; i++) for(int j = 0; j < 6; j++) if (lights[i][j] == e.getSource()) { row = i; column = j; } changeColor(lights[row][column]); if (column - 1 >= 0) changeColor(lights[row][column - 1]); if (row + 1 <= 5) changeColor(lights[row + 1][column]); if (column + 1 <= 5) changeColor(lights[row][column + 1]); if (row - 1 >= 0) changeColor(lights[row - 1][column]); } }