CHAPTER 12 ■ VIDEO GAMES
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;
public class TargetClick implements ActionListener {
// Create the user interface components
private TargetClickPanel targetClickPanel = new TargetClickPanel();
private JPanel scorePanel = new JPanel();
private JLabel timeLabel = new JLabel("Time: ");
private JLabel scoreLabel = new JLabel(" Targets Hit: ");
private JLabel timeDisplayLabel = new JLabel("0");
private JLabel scoreDisplayLabel = new JLabel("0");
private JFrame frame = new JFrame("TargetClick");
// Create the timer, which will manage the game loop
private Timer timer = new Timer(400, this);
// Create some other bits needed by the game logic
static Random random = new Random();
static int score;
private long startTime;
long elapsedTime = 0;
private int gameLength = 50;
// Add a menu, as we've seen before private void addMenu(JFrame frame) {
JMenu file = new JMenu("File");
file.setMnemonic('F');
JMenuItem exitItem = new JMenuItem("Exit");
JMenuItem newGameItem = new JMenuItem("New Game");
JMenuItem gameLengthItem = new JMenuItem("Set Game Length");
exitItem.setMnemonic('x');
exitItem.addActionListener(this);
newGameItem.setMnemonic('n');
newGameItem.addActionListener(this);
gameLengthItem.setMnemonic('s');
gameLengthItem.addActionListener(this);
file.add(exitItem);
file.add(newGameItem);
file.add(gameLengthItem);
JMenuBar menuBar = new JMenuBar();
menuBar.add(file);
frame.setJMenuBar(menuBar);
}
// Display the user interface, as we've seen before
private void createAndShowGUI() {
addMenu(frame);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);