|
Car Show Applet Chapter 5, Excercise 5.12:
// Car Show Applet
// Eric B.
//
// July 20, 2000
//
// Draws a car driving across the screen. In this case, a Saab 9^3.
// It looks nice, but I don't think I'd ever buy one. Too much hassle!
//
// Assignment 5.12, p265
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class CarShow extends Applet implements Runnable {
// Double buffering initialization
private Dimension appletSize;
private Image offscreenBuffer;
public Graphics gOff, g;
// Set up space for our images
private Image carImage, carImage2, carLogo;
// Set up space for the thread that will run to move the car
private Thread carLoop;
// Set up space for the car object
public Car myCar;
public void init() {
// Set up the double buffering.
g = getGraphics();
appletSize = getSize();
offscreenBuffer = createImage(appletSize.width, appletSize.height);
gOff = offscreenBuffer.getGraphics();
// Load the images into memory.
carImage = getImage(getDocumentBase(), "carGif.gif");
carImage2 = getImage(getDocumentBase(), "carGif_2.gif");
carLogo = getImage(getDocumentBase(), "carLogo.gif");
// Create our car object
myCar = new Car(500, 40, carImage, carImage2);
// Initialize the running thread and start
carLoop = new Thread(this);
carLoop.start();
}
public void run() {
// Notice that the below loop is always running - what we want
while (true) {
// Clear the drawing area
gOff.setColor(Color.white);
gOff.fillRect(0,0, 500, 500);
// Draw the 9^3 logo
gOff.drawImage(carLogo, 5, 5, null);
// Display the car. It will move of its own volition.
myCar.display(gOff);
// Paint the current contents of the buffer
paint(g);
// This try / catch is necessary for the sleep function.
try {carLoop.sleep(5);}
catch (InterruptedException e) {}
}
}
public void paint(Graphics g) {
// Blit the buffer
g.drawImage(offscreenBuffer, 0, 0, null);
}
}
class Car {
// Set up location information and the bitmaps we'll draw
private int x, y;
private Image carImage, carImage2;
private int flipped = 0;
// Construct our object
public Car(int x, int y, Image carImage, Image carImage2) {
this.x = x;
this.y = y;
this.carImage = carImage;
this.carImage2 = carImage2;
}
public void display(Graphics g) {
// Draw alternating images (flip between images every third frame)
// The flipped was once a boolean, which is more elegant, but the
// didn't look right flipped every other frame.
// Not that it looks right now... Still pretty spiffy, though.
if (flipped <= 2) {
g.drawImage(carImage, x, y, null);
flipped++;
} else {
g.drawImage(carImage2, x, y, null);
flipped = 0;
}
x--; // Right to left
if (x < -100) // If the car is off the screen on left, move to right
x = 500;
}
}
Click here for the CarShow Class File Click here for the Car Class File Catch The Creature Chapter 5, Excercise 5.8:
// Catch The Creature Applet
// Eric Boyd
//
// July 20, 2000
//
// A little game where you try to click the creature. In this case
// Micheal Stipe, lead singer of REM. (Hey, why not?)
//
// It displays the creature at random intervals in random places, with
// random pauses in between.
//
// A hit count and attempt count is displayed.
//
// Assignment 5.8, p264
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class CatchTheCreature extends Applet implements Runnable, MouseListener {
// Set up the playfield vars
protected static int playAreaWidth = 300, playAreaHeight = 300;
protected static int playAreaXStart = 50, playAreaYStart = 50;
// Double buffering initialization
private Dimension appletSize;
private Image offscreenBuffer;
public Graphics gOff, g;
// Set up space for our images
private Image creatureImage;
// Set up space for the thread that will run to move the creature
private Thread creatureLoop;
// Set up space for the creature object
public Creature myCreature;
// Our clicked audio clip
private AudioClip clicked;
public void init() {
// Set up the double buffering.
g = getGraphics();
appletSize = getSize();
offscreenBuffer = createImage(appletSize.width, appletSize.height);
gOff = offscreenBuffer.getGraphics();
// Load the images and audio into memory.
creatureImage = getImage(getDocumentBase(), "creatureStipe.gif");
clicked = getAudioClip(getCodeBase(), "sound.au");
// Create our car object
myCreature = new Creature(0, 0, creatureImage);
// Initialize the running thread and start
creatureLoop = new Thread(this);
creatureLoop.start();
this.addMouseListener(this);
}
public void run() {
int delay = 0;
// Notice that the below loop is always running - What we want
while (true) {
// Come up with a random interval
delay = (int)(Math.random() * 2000) + 500;
// Come up with a new location
myCreature.newLocation();
// Paint the current contents of the buffer
paint(g);
// This try / catch is necessary for the sleep function.
try {creatureLoop.sleep(delay);}
catch (InterruptedException e) {}
// Toggle the creature's visibility
if (myCreature.visible) {
myCreature.visible = false;
} else {
myCreature.visible = true;
myCreature.tryCount(); // If it appears, you have another chance
}
}
}
public void paint(Graphics g) {
// Clear the drawing area
gOff.setColor(Color.white);
gOff.fillRect(0, 0, appletSize.width, appletSize.height);
// Outline the playfield
gOff.setColor(Color.black);
gOff.drawRect(playAreaXStart, playAreaYStart, playAreaWidth, playAreaHeight);
// Display the hitcount
myCreature.displayCount(gOff);
// Display the creature if it's visible
if (myCreature.visible)
myCreature.display(gOff);
// Blit the buffer
g.drawImage(offscreenBuffer, 0, 0, null);
}
public void mouseClicked(MouseEvent e) {
// Check to see if the click is on the creature. Play the sound and repaint.
if (myCreature.checkClick(e.getX(), e.getY())) {
clicked.play();
paint(g);
}
}
// Junk methods for interface compatibility
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
}
class Creature {
// Declare our vars
private int x, y;
private int hitCount = 0;
private int tryCount = 0;
private Image creatureImage;
public boolean visible = true;
// Construct the creature
public Creature(int x, int y, Image creatureImage) {
this.x = x;
this.y = y;
this.creatureImage = creatureImage;
}
public void display(Graphics g) {
// Draw the image
g.drawImage(creatureImage, CatchTheCreature.playAreaXStart+x, CatchTheCreature.playAreaXStart+y, null);
}
public void displayCount(Graphics g) {
// Get rid of the standard font.
Font stdFont = new Font("Verdana", Font.BOLD, 9);
g.setFont(stdFont);
// Draw the text
g.setColor(Color.gray);
g.drawString("Hits: " + hitCount, 50, 38);
g.drawString("Tries: " + tryCount, 50, 48);
}
// Determine a random new location
public void newLocation() {
x = (int)(Math.random() * (CatchTheCreature.playAreaWidth-50));
y = (int)(Math.random() * (CatchTheCreature.playAreaHeight-50));
}
// Check to see if the click was on the creature.
// If so, hide the creature and increment the hitcounter.
public boolean checkClick(int clickX, int clickY) {
int checkX = clickX - CatchTheCreature.playAreaXStart;
int checkY = clickY - CatchTheCreature.playAreaYStart;
if (( checkX < x + 50) && ( checkX > x) && (checkY > y) && (checkY < y + 50) && (visible)) {
hitCount++;
visible = false;
return true;
} else {
return false;
}
}
// Increase the number of times displayed
public void tryCount() {tryCount++;}
}
Click here for the Catch.. Class FileClick here for the Creature Class File |