Java 7 for Absolute Beginners

(nextflipdebug5) #1
CHAPTER 12 ■ VIDEO GAMES

attention to the tick method (which removes objects as they go off the bottom and replaces them with
new objects at the top, assuming the player doesn't shoot that particular target).


Listing 12-7. The ShootingGalleryTargetRow class


package com.bryantcs.examples.videogames;


import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.LinkedList;


import javax.swing.JPanel;


public class ShootingGalleryTargetRow extends JPanel {


private static final long serialVersionUID = 1L;


// We need a list of targets
private LinkedList targets =
new LinkedList();


// We need to know how many points this target is worth
private int targetValue;


// We need to know when to remove a target
// off the bottom and add a new one at the top
private int newTargetTicker;


// We need a sprite for the player to shoot
// Using a buffered image to reduce flicker
private BufferedImage sprite;


// The constructor, in which we set the values of the things we need to know
public ShootingGalleryTargetRow(BufferedImage sprite, int value, int delay) {
targetValue = value;
newTargetTicker = 0;
this.sprite = sprite;
}


public LinkedList getTargets() {
return targets;
}


@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.WHITE);
for (int targetCounter = 0; targetCounter < targets.size(); targetCounter ++) {
ShootingGalleryTarget currentTarget = targets.get(targetCounter);
if (currentTarget.getY() > getHeight() + ShootingGalleryPanel.TARGET_SPACE) {

Free download pdf