Java 7 for Absolute Beginners

(nextflipdebug5) #1
CHAPTER 12 ■ VIDEO GAMES

targetClickPanel.setPreferredSize(new Dimension(400, 200));
targetClickPanel.setBackground(Color.WHITE);
scorePanel.setPreferredSize(new Dimension(400, 50));
scorePanel.setLayout(new BoxLayout(scorePanel, BoxLayout.X_AXIS));
scorePanel.add(timeLabel);
scorePanel.add(timeDisplayLabel);
scorePanel.add(scoreLabel);
scorePanel.add(scoreDisplayLabel);
frame.getContentPane().add(scorePanel);
frame.getContentPane().add(targetClickPanel);
frame.getContentPane().setLayout(new
BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
frame.pack();
frame.setVisible(true);
}


// start or restart the game
private void init() {
timer.start();
startTime = System.currentTimeMillis();
}


// The main method
public static void main(String[] args) {
TargetClick targetClick = new TargetClick();
targetClick.createAndShowGUI();
targetClick.init();
}


// The game loop (which isn't a loop)
// Also contains the game logic
public void actionPerformed(ActionEvent e) {
// check for user input (step 1 in a game loop)
if (e.getActionCommand() != null) {
if (e.getActionCommand().equals("Exit")) {
timer.stop();
System.exit(0);
}
if (e.getActionCommand().equals("New Game")){
newGame();
}
if (e.getActionCommand().equals("Set Game Length")) {
String option = JOptionPane.showInputDialog(frame,
"Number of Targets:");
if (option != null) {
gameLength = Integer.parseInt(option);
}
newGame();
}
}
// update the score
updateScorePanel();

Free download pdf