Java 7 for Absolute Beginners

(nextflipdebug5) #1

CHAPTER 7 ■ WRITING A USER INTERFACE


because a method with that problem is overly complex, it's exactly the kind of place where errors arise.
So not only is such a method hard to debug, but it's likely that you have to debug it—a double whammy.
In the case of the newGame method, it performs one simple task (removing all the buttons from the
minefield panel), and then calls a chain of other methods that do the work of setting up a new game and
updating the interface. In particular, it calls the init method in the MineSweeper class, which makes a new
MineField object and sets up all the buttons in the play area. Then it validates the minePanel object in the
MineSweeper class, which ensures that all the old buttons won't appear and that the new buttons created
by the init method will appear. Then it validates the JFrame object, which forces it to correctly accept
the changed minePanel object. (Think of the validate method on Swing objects as a mechanism for
ensuring that any changes are shown to the user.) Then it calls the pack method on the JFrame object,
which finishes the job of refreshing our changed minefield interface. Finally, it updates the labels to their
starting values (which were set by the init method).
That's a lot to do, but we manage it by having methods that do clearly defined bits of it for us, rather
than lumping it all together in one spot, which would, again, be hard to understand, hard to debug, and
(worst of all) more likely to produce errors. Remember: If you have to struggle to figure out what a
method is doing, it probably needs to be multiple methods. On the other hand, it might also mean that
you have a design problem (probably a badly thought-out set of classes). Either way, take hard-to-
understand code as a sign of a problem and think of a way to make it easy to understand.
Now let's look at an unusual class, the MineIcon class, shown in Listing 7-27.

Listing 7-27. The MineIcon class

package com.apress.java7forabsolutebeginners.examples.MineSweeper;

import javax.swing.Icon;
import javax.swing.ImageIcon;

public class MineIcon {

private static Icon mineIcon = new ImageIcon("C:\\Projects\\MineSweeper\\mine.png");
private static Icon suspectIcon = new ImageIcon("C:\\Projects\\MineSweeper\\question.png");
private static Icon oneIcon = new ImageIcon("C:\\Projects\\MineSweeper\\one.png");
private static Icon twoIcon = new ImageIcon("C:\\Projects\\MineSweeper\\two.png");
private static Icon threeIcon = new ImageIcon("C:\\Projects\\MineSweeper\\three.png");
private static Icon fourIcon = new ImageIcon("C:\\Projects\\MineSweeper\\four.png");
private static Icon fiveIcon = new ImageIcon("C:\\Projects\\MineSweeper\\five.png");
private static Icon sixIcon = new ImageIcon("C:\\Projects\\MineSweeper\\six.png");
private static Icon sevenIcon = new ImageIcon("C:\\Projects\\MineSweeper\\seven.png");
private static Icon eightIcon = new ImageIcon("C:\\Projects\\MineSweeper\\eight.png");

static Icon getMineIcon() {
return mineIcon;
}

static Icon getSuspectIcon() {
return suspectIcon;
}

static Icon getNumberIcon(int mineCount) {
if (mineCount == 1) return oneIcon;
if (mineCount == 2) return twoIcon;
Free download pdf