Building the Aquarium


This application doesn't need a lot of sophisticated windowing techniques or controls, so it draws itself in a straightforward way using the Java Abstract Windowing Toolkit, also known as the AWT (some of the upcoming applications that need to be more powerful will use Swing). The main class in this application is Aquarium, and you need to base it on a class that can display the aquarium, which means using a class such as Frame in the AWT:

 import java.awt.*; public class Aquarium extends Frame {         .         .         . 

This class relies on the Frame class (full name java.awt.Frame), and you can find the significant methods of this class in Table 1.1.

Table 1.1. The Significant Methods of the java.awt.Frame Class

Method

Does This

void addNotify()

Connects this frame window to a native screen resource

int getCursorType()

Deprecated. Used to get the type of cursor in this frame, but now you should use Component.getCursor()

int getExtendedState()

Returns the full extended state of this frame

static Frame[] getFrames()

Returns an array that holds all the Frame objects the application has created

Image getIconImage()

Returns the icon used for this framethis is the image used when the frame is minimized

Rectangle getMaximizedBounds()

Returns the bounding rectangle for this frame if it is maximized

MenuBar getMenuBar()

Returns the menu bar used in this frame, if there is one

int getState()

Returns the state of this frame. Note that this method is considered obsolete

String getTitle()

Returns the title used in the frame

boolean isResizable()

Returns a value of true if this frame may be resized

boolean isUndecorated()

Returns a value of TRue if this frame is not decorated

Protected String paramString()

Returns a parameter string that specifies the state of this frame

void remove(MenuComponent m)

Removes the menu bar from this frame and disposes of it

void removeNotify()

Disconnects this frame from its native screen resource, which means it cannot be displayed

void setCursor(int cursorType)

Deprecated. Use Component.setCursor(Cursor) instead

void setExtendedState(int state)

Specifies the extended state of this frame

void setIconImage(Image image)

Specifies the icon that will be displayed when this frame is minimized

void setMaximizedBounds(Rectangle bounds)

Specifies the rectangle corresponding to the maximum bounds for this frame

void setMenuBar(MenuBar mb)

Specifies the menu bar you want to use in this frame

void setResizable(boolean resizable)

Specifies whether this frame may be resizable

void setState(int state)

Specifies the state of this frame. Note that this method is now obsolete

void setTitle(String title)

Specifies the title you want to use in this frame

void setUndecorated(boolean undecorated)

Specifies whether you want to use decorations in this frame


All this window has to do is to open when the application starts and display a background of bubbles, as shown in Figure 1.1, as well as the fish. When the Aquarium application first runs, its main method will be called, but that's a static method with many restrictions on the data you can work with, so it's a good idea to create a new Aquarium object in main, because the new object will not suffer those restrictions:

 import java.awt.*; public class Aquarium extends Frame {     public static void main(String[] args)     {         new Aquarium();     }         .         .         . } 

The new Aquarium object's constructor will be where the action is. In this constructor, you can set the title of the Frame window using the setTitle method, and this window will be titled "The Aquarium," as shown in Figure 1.1. To close the window, you need to catch the window close event that occurs when the user clicks the close button at upper-right corner of the window. The AWT uses listener classes to catch events such as this one. Here's how you typically handle the closing of a window, using System.exit(0) to end the application:

 import java.awt.*; import java.awt.event.*; public class Aquarium extends Frame {    Aquarium()    {         setTitle("The Aquarium");         .         .         .        this.addWindowListener(new WindowAdapter(){             public void windowClosing(                 WindowEvent windowEvent){                     System.exit(0);                 }             }         );     }     public static void main(String[] args)     {         new Aquarium();     }         .         .         . } 

That takes care of the rudiments of creating and closing the tank in which the fish will swim. Now what about the getting the fish into the action?



    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

    Similar book on Amazon

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