Loading the Fish


The two fish images are fish1.gif, which points to the left, and fish2.gif, which points to the right. GIF images are a good choice here, because you can draw your images on a transparent background; as the fish move around, the square background on which they're drawn will not appear, because it's transparentonly the fish image will be visible. That means the fish will actually look like fish, not boxes, swimming around.

Besides loading the fish images, this is a good time to load the background image of bubbles, because the application needs to know how large that image is so it can size the window correctly. This application comes with a file named bubbles.gif that you can see as the background, as shown in Figure 1.1. However, you can use your own image insteadthe possibilities are endless.

To make sure the images are loaded correctly, Aquarium uses a MediaTracker object, which will alert the user if there are problems in loading fish1.gif, fish2.gif, and the background image, bubbles.gif. Here's how the MediaTracker object, tracker, is set up:

 import java.awt.*; import java.awt.event.*; public class Aquarium extends Frame {     MediaTracker tracker;    Aquarium()    {         setTitle("The Aquarium");         tracker = new MediaTracker(this);         .         .         .        this.addWindowListener(new WindowAdapter(){             public void windowClosing(                 WindowEvent windowEvent){                     System.exit(0);                 }             }         );     }     .     .     . } 

This application stores the fish and background images in objects of the java.awt.Image class, which is going to be used extensively throughout the chapter. The significant methods of this class appear in Table 1.2.

Table 1.2. The Significant Methods of the java.awt.Image Class

Method

Does This

abstract void flush()

Deallocates all resources currently used by the Image object

ImageCapabilities getCapabilities(GraphicsConfiguration gc)

Returns an object of the ImageCapabilities class, which contains information about the capabilities of this image

abstract Graphics getGraphics()

Returns a graphics context that you can use to draw in this image

abstract int getHeight(ImageObserver observer)

Returns the height of the image, in pixels

abstract Object getProperty(String name, ImageObserver observer)

Returns the value of a property of this image, which you specify by name

Image getScaledInstance(int width, int height, int hints)

Returns a scaled version of the current image, with the specified height and width

abstract ImageProducer getSource()

Returns the actual object used to create the pixels in this image

abstract int getWidth(ImageObserver observer)

Returns the width of this image


To load in the images, you can use the getImage method of the AWT's toolkit, a built-in treasure chest of helpful functions. The Toolkit class is abstract and can't be instantiated directly, but you can use the Toolkit class's static method geTDefaultToolkit to get a working toolkit object for the current application. Here's how you can load in the images the application needs, storing the fish images in an array named fishImages and the background image in an image named aquariumImage:

 import java.awt.*; import java.awt.event.*; public class Aquarium extends Frame {     Image aquariumImage;     Image[] fishImages = new Image[2];     MediaTracker tracker;    Aquarium()    {         setTitle("The Aquarium");         tracker = new MediaTracker(this);         fishImages[0] = Toolkit.getDefaultToolkit().getImage             ("fish1.gif");         tracker.addImage(fishImages[0], 0);         fishImages[1] = Toolkit.getDefaultToolkit().getImage             ("fish2.gif");         tracker.addImage(fishImages[1], 0);         aquariumImage = Toolkit.getDefaultToolkit().getImage             ("bubbles.gif");         tracker.addImage(aquariumImage, 0);         try {             tracker.waitForID(0);         }catch (Exception ex) {             System.out.println(ex.getMessage());         }         .         .         .        this.addWindowListener(new WindowAdapter(){             public void windowClosing(                 WindowEvent windowEvent){                     System.exit(0);                 }             }         );     }     .     .     . } 

Now the fish and background images are available in code. You can size the window to match the background image with the setSize method, make sure the window can't be resized (because the background image is of a fixed size) with the setResizable method, and show the window on the screen with the window's setVisible method, as shown in the following code:

 import java.awt.*; import java.awt.event.*; public class Aquarium extends Frame {     Image aquariumImage;     Image[] fishImages = new Image[2];     MediaTracker tracker;    Aquarium()    {         setTitle("The Aquarium");         .         .         .         try {             tracker.waitForID(0);         }catch (Exception ex) {             System.out.println(ex.getMessage());         }         setSize(aquariumImage.getWidth(this), aquariumImage.getHeight(this));         setResizable(false);         setVisible(true);         .         .         .        this.addWindowListener(new WindowAdapter(){             public void windowClosing(                 WindowEvent windowEvent){                     System.exit(0);                 }             }         );     }     .     .     . } 

This application will use one more Image object besides the three it already hasmemoryImage. This is where double buffering comes into play; to avoid making the image of the aquarium flicker, the application draws its visual display offscreen and then draws the completed scene on the screen all at once. This means Java can avoid drawing a fish, updating the entire display, drawing another fish, updating the entire display, and so on, which would make the aquarium flicker.

You can create this offscreen Image object, memoryImage, in memory. In addition, the application creates a Graphics object, called memoryGraphics, that can be used to draw in the new memory image this way:

 import java.awt.*; import java.awt.event.*; public class Aquarium extends Frame {     Image aquariumImage, memoryImage;     Graphics memoryGraphics;     Image[] fishImages = new Image[2];     MediaTracker tracker;    Aquarium()    {         .         .         .         setSize(aquariumImage.getWidth(this), aquariumImage.getHeight(this));         setResizable(false);         setVisible(true);         memoryImage = createImage(getSize().width, getSize().height);         memoryGraphics = memoryImage.getGraphics();        this.addWindowListener(new WindowAdapter(){             public void windowClosing(                 WindowEvent windowEvent){                     System.exit(0);                 }             }         );     }     .     .     . } 

The Graphics class is what lets you do all the drawing in this application, and the significant methods of this class (that is, the ones you'll see later in this book) appear in Table 1.3.

Table 1.3. The Significant Methods of the java.awt.Graphics Class

Method

Does This

void draw3DRect(int x, int y, int width, int height, boolean raised)

Draws a 3D rectangle in outline

abstract void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle)

Draws an arc, in outline, inside the specified rectangle

void drawChars(char[] data, int offset, int length, int x, int y)

Draws the text given by the given character array

abstract boolean drawImage(Image img, int x, int y, ImageObserver observer)

Draws an image in the graphics object

abstract void drawLine(int x1, int y1, int x2, int y2)

Draws a line connecting (x1, y1) and (x2, y2), using the current drawing color

abstract void drawOval(int x, int y, int width, int height)

Draws an oval, in outline, using the current drawing color

abstract void drawPolygon(int[] xPoints, int[] yPoints, int nPoints)

Draws a polygon, in outline, whose vertices are defined by arrays of x and y coordinates

abstract void drawPolyline(int[] xPoints, int[] yPoints, int nPoints)

Draws lines connecting the points in the given arrays of both x and y coordinates

void drawRect(int x, int y, int width, int height)

Draws the given rectangle, in outline

abstract void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)

Draws a rectangle with round corners, in outline, with the current color

abstract void drawString(String str, int x, int y)

Draws the given text using the Graphics object's current font and color

void fill3DRect(int x, int y, int width, int height, boolean raised)

Draws a filled 3D rectangle using the current color

abstract void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle)

Draws a filled elliptical arc inside the given rectangle using the current color

abstract void fillOval(int x, int y, int width, int height)

Draws a filled oval inside the given rectangle using the current color

abstract void fillPolygon(int[] xPoints, int[] yPoints, int nPoints)

Draws a filled polygon, as defined by the given arrays of x and y coordinates, using the current color

void fillPolygon(Polygon p)

Draws a filled polygon, as defined by the given Polygon object, using the current color

abstract void fillRect(int x, int y, int width, int height)

Draws a filled rectangle using the current color

abstract void fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)

Draws a filled rounded corner rectangle using the current color

abstract Color getColor()

Returns this Graphics object's current color

abstract Font getFont()

Returns this Graphics object's current font

abstract void setColor(Color c)

Sets this Graphics object's color to the given color

abstract void setFont(Font font)

Sets this Graphics object's font to the specified font


Okay, the setup is complete; the images are ready to go, and the window is ready to be used. What about getting the fish to actually start doing something?



    Java After Hours(c) 10 Projects You'll Never Do at Work
    Java After Hours: 10 Projects Youll Never Do at Work
    ISBN: 0672327473
    EAN: 2147483647
    Year: 2006
    Pages: 128

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net