Java Reference
In-Depth Information
// We need to know whether the player can shoot
private boolean readyToShoot;
// We need to know the current step
// when drawing the cursor after a shot
private int currentStep;
// We need to know the maximum steps
// (Changing this value would change
// the timeout between shots, by the way.)
private int maxSteps = 50;
// The constructor
public ShootingGalleryShooter(int width) {
// Add the listeners for mouse clicks and motion
addMouseListener(this);
addMouseMotionListener(this);
// Let the player shoot right away
readyToShoot = true;
currentStep = maxSteps;
// Draw the cursor 20 pixels from the right edge
xPosition = width - 20;
}
public void paintComponent(Graphics g) {
// Let the parent draw itself
// (sets the panel's background color)
super.paintComponent(g);
// Set the cursor's background color
this.setBackground(Color.WHITE);
// Figure out whether the player can shoot
if (currentStep < maxSteps) {
// If not, increment the current step
currentStep++;
readyToShoot = false;
} else {
readyToShoot = true;
}
// Fill two rectangles to show how long
// the player has to wait to shoot again
g.setColor(Color.RED);
g.fillRect(xPosition, yPosition - (maxSteps / 2), 3, currentStep);
g.setColor(Color.YELLOW);
g.fillRect(xPosition, yPosition - (maxSteps / 2) + currentStep, 3, maxSteps -
currentStep);
Search WWH ::




Custom Search