Java 7 for Absolute Beginners

(nextflipdebug5) #1

CHAPTER 12 ■ VIDEO GAMES


As you can tell from the score, I hit four 10-point targets and one 25-point target before I took the
screen shot. I find the back row to be hard to hit, but it's supposed to be that way, so I guess that's a
good sign.
The ShootingGallery game consists of six classes:


  • ShootingGallery defines and manages the user interface.

  • ShootingGalleryPanel manages the playing area and contains the game loop.

  • ShootingGalleryShooter manages the cursor.

  • ShootingGalleryTargetRow manages an individual row of targets.

  • ShootingGalleryTarget manages an individual target.

  • ShootingGallerySprites manages the sprites used by the game.


Listing 12-4. The ShootingGallery class

package com.bryantcs.examples.videogames;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

public class ShootingGallery implements ActionListener {

// Define the components
private ShootingGalleryPanel shootingGalleryPanel;
private JPanel scorePanel = new JPanel();
private JLabel scoreLabel = new JLabel("Score: ");
static JLabel scoreDisplayLabel = new JLabel("0");
private JFrame frame = new Jframe("Shooting Gallery");
// Set up a place to keep score (default is 0)
static int score;
// Set the game size
static Dimension gameDimension = new Dimension(500, 500);

// Add a menu (just one option, Exit)
private void addMenu(JFrame frame) {
JMenu file = new JMenu("File");
file.setMnemonic('F');
JMenuItem exitItem = new JMenuItem("Exit");
exitItem.setMnemonic('x');
exitItem.addActionListener(this);
Free download pdf