// Bug.java // Written by THC for CS 5 Lab Assignment #2. // Represents an individual Bug. import java.applet.Applet; import java.awt.*; public class Bug { private static Image bugImage; // reference to the image for this Bug private static Applet theApplet; // the applet that will be displaying the image private static final int SPEED = 3; // how many pixels this Bug moves each time private static final int HEIGHT = 50, WIDTH = 33; // height and width of this Bug's image private Point upperLeft; // upper left corner of the current location of this Bug's image // Static method to initialize the static variables bugImage and theApplet. public static void init(Image picture, Applet applet) { bugImage = picture; theApplet = applet; } // Constructor. Makes a new Bug at the given location. public Bug(Point where) { upperLeft = where; } // Draw this Bug. public void draw(Graphics page) { page.drawImage(bugImage, upperLeft.x, upperLeft.y, theApplet); } // Move this Bug by SPEED pixels toward the top of the applet window. // Returns true if any part of this Bug is still in the window, // false if this Bug has moved fully off the top of the window. public boolean move() { upperLeft.y -= SPEED; return upperLeft.y + HEIGHT >= 0; } // Determines whether a Bug is hit by a Point. // Returns true if the Point is anywhere within this Bug's image. public boolean isHit(Point p) { return upperLeft.x <= p.x && p.x < upperLeft.x + WIDTH && upperLeft.y <= p.y && p.y < upperLeft.y + HEIGHT; } }