Java 7 for Absolute Beginners

(nextflipdebug5) #1

CHAPTER 7 ■ WRITING A USER INTERFACE


if (count > 0) {
thisButton.setIcon(MineIcon.getNumberIcon(count));
}
}
}
}
}

void endGame(boolean won) {
showAll();
String wonOrLost;
int option;
if (won) {
wonOrLost = "You won!";
} else {
wonOrLost = "You lost.";
}
option = JOptionPane.showConfirmDialog(mineSweeper.frame, wonOrLost
+ " Play again?", wonOrLost,
JOptionPane.YES_NO_OPTION);
if (option == 1) {
System.exit(0);
} else {
newGame(mineSweeper.rows, mineSweeper.columns);
}
}

void newGame(int previousRows, int previousColumns) {
for (int i = 0; i < previousRows; i++) {
for (int j = 0; j < previousColumns; j++) {
mineSweeper.minePanel.remove(mineSweeper.mineButtons[i][j]);
}
}
mineSweeper.init();
mineSweeper.minePanel.validate();
mineSweeper.frame.validate();
mineSweeper.frame.pack();
updateLabels();
}
}

The MineSweeperHelper class has a constructor, as shown in Listing 7-21.

Listing 7-21. The MineSweeperHelper constructor

public MineSweeperHelper(MineSweeper mineSweeper) {
this.mineSweeper = mineSweeper;
}

This constructor doesn't do much, but what it does is critically important. All the methods in the
class work on an instance of the MineSweeper class, so we must have an instance for them. The
constructor gives us that instance. As we saw in the constructor for the MineSweeper class, the
Free download pdf