The goal of this lab is to get more practice using Java's GUI capabilities to write programs that use windows and buttons.
Lights Out is a game with a 5x5 grid of lighted buttons. When you press each button, it and its neighbors are toggled on and off. For example, suppose a button's light is on, the one above it is on, the one to the left is off, the one to the right is on, and the one below is off. If you press this button, the result will be that button off, the one above if off, the one to the left on, the one to the right off, and the one below on. Below is an example of what the game might look like.
The game begins in a random (or pre-defined) orientation, and the object is to turn all the lights out. In this lab, you will create a game like this.
One detail about buttons which appeared on Monday's handout but was never
mentioned in class is the use of image icons.
Sometimes we may prefer decorate buttons with a picture rather than
some text.
In today's lab, we will want buttons to appear to have lights which turn on
and off.
Notice on Monday's handout (or in the Java API) that JButton
has a method setIcon(Icon)
.
Further, I list a class called ImageIcon
, which is a
subtype of Icon
.
The constructor of ImageIcon
takes the fame of a file;
this file can be in any standard image format, such as jpg or gif.
As usual, make a new directory for this project and cd into it.
Part of this program is going to be similar to what you did on Tuesday---both
windows, afterall, are just grids of buttons.
So, copy over your FifteenPuzzle.java
from last time,
but rename it LightsOut.java
.
cp ../lab13/FifteenPuzzle.java LightsOut.java
Also copy the following files which contain icons for on and off lights (unless you want to find your own on the Internet):
cp /homeemp/tvandrun/pub/235/reddot.jpg . cp /homeemp/tvandrun/pub/235/blackdot.jpg .
Make the following changes to LightsOut.java
:
counter
.
Light
,
but also comment those lines out for the time being since you haven't made
the Light
class yet.
Then run the program to see what the window looks like. The buttons will all be blank at this point.
Write the action listener class. You may want to have your
PuzzlePiece
code hand to look at, but this will be different
enough that you probably do not want simply to copy, paste, and modify
(like you did in LightsOut.java
.
This class should have the following properties:
Math.random
for this.
Copy the files you modified to
/homestu/jmichalk/labTA/turnInProjects/
yourFirstName,yourLastInitial/lab12