Creating the Rink


Like the Aquarium application, where the tank was created in the constructor, the hockey rink is created in Slapshot!'s constructor. Some of this constructor will be familiar from the Aquarium project in the previous chapter, but there's a lot more going on here.

To let the user select optionsstarting a game, ending one, setting the speed, and so onthis application uses a menu system. After creating a menu bar, the code creates a File menu and stocks it with items such as Start, End, Set Speed…, and Exit. These menu items are connected to the code using an action listener, which means the Slapshot class must implement the ActionListener interface and add an actionPerformed method to handle menu selections. The menu items are added to a menu, which is then added to the menu bar, which in turn is added to the application itself, as shown here:

 public class Slapshot extends Frame implements ActionListener, Runnable {         .         .         . Slapshot() {     menubar = new MenuBar();     menu1 = new Menu("File");     menuitem0 = new MenuItem("Start");     menu1.add(menuitem0);     menuitem0.addActionListener(this);     menuitem1 = new MenuItem("End");     menu1.add(menuitem1);     menuitem1.addActionListener(this);     menuitem2 = new MenuItem("Set speed...");     menu1.add(menuitem2);     menuitem2.addActionListener(this);     menuitem3 = new MenuItem("Exit");     menu1.add(menuitem3);     menuitem3.addActionListener(this);     menubar.add(menu1);     setMenuBar(menubar);         .         .         . 

The significant methods of the Menu class appear in Table 2.1, and the significant methods of the MenuItem class appear in Table 2.2.

Table 2.1. The Significant Methods of the java.awt.Menu Class

Method

Does This

MenuItem add(MenuItem mi)

Adds a new menu item to the current menu

void addSeparator()

Adds a line used to separate items within the menu

int countItems()

Deprecated. Use getItemCount() instead of this method

MenuItem getItem(int index)

Returns the item at the given index of this menu.

int getItemCount()

Returns the number of items currently in this menu

void insert(MenuItem menuitem, int index)

Places a menu item in this menu at the given position

boolean isTearOff()

Returns true if this menu is a tear-off menu

void remove(int index)

Deletes the menu item at the given index from this menu

void remove(MenuComponent item)

Deletes the given menu item from this menu

void removeAll()

Deletes all the items in this menu


Table 2.2. The Significant Methods of the java.awt.MenuItem Class

Method

Does This

void addActionListener(ActionListener l)

Adds an action listener so that you can get menu item selection events

void disable()

Deprecated. Use the setEnabled(boolean) method instead

void enable()

Deprecated. Use the setEnabled(boolean) method instead

void enable(boolean b)

Deprecated. Use the setEnabled(boolean) method instead

ActionListener[] getActionListeners()

Returns an array containing the action listeners listening to this menu item

String getLabel()

Returns the label of this menu item

MenuShortcut getShortcut()

Returns the MenuShortcut object for this menu item

boolean isEnabled()

Returns TRue if this menu item is enabled

void removeActionListener(ActionListener l)

Removes the given action listener, which means it will no longer get events from this menu item

void setEnabled(boolean b)

Specifies whether or not this menu item is enabled and therefore can be selected by the user

void setLabel(String label)

Sets the label for this menu item to the text you pass

void setShortcut(MenuShortcut s)

Sets a MenuShortcut object for this menu item


The constructor next adds a MouseListener and MouseMotionListener to the application to let the user move his blocker with the mouse, and it also implements the MouseListener and MouseMotionListener interfaces. In addition, it creates a dialog box of the OkCancelDialog class (discussed later in the chapter) that's used to let the user enter a new speed:

[View full width]

public class Slapshot extends Frame implements ActionListener, MouseListener, MouseMotionListener, Runnable OkCancelDialog textDialog; . . . { menubar = new MenuBar(); . . . addMouseListener(this); addMouseMotionListener(this); textDialog = new OkCancelDialog(this, "Set speed (1-100)", true); . . .

Slapshot! also needs to display the user's score and the computer's score, and it'll use two AWT Label controls for that. After setting the AWT layout to null, you can use the label's setBounds method to place the labels where you want them:

 setLayout(null); label1 = new Label(); label1.setText("0"); label1.setBounds(180, 310, 20, 20); label1.setVisible(false); add(label1); label2 = new Label(); label2.setText("0"); label2.setBounds(400, 310, 20, 20); label2.setVisible(false); add(label2);     .     .     . 

You can see the methods of the Label class in Table 2.3.

Table 2.3. The Significant Methods of the java.awt.Label Class

Method

Does This

int getAlignment()

Returns the alignment setting of this label

String getText()

Returns the text currently stored in this label

void setAlignment(int alignment)

Sets the alignment of this label to the given value

void setText(String text)

Sets the text stored in this label to the text you pass


Next, a media tracker monitors the loading the images neededthe rink itself, the puck image, and the blocker image (a short upright bar). After that, the main window is sized and displayed:

 tracker = new MediaTracker(this); backGroundImage = Toolkit.getDefaultToolkit().     getImage("rink.gif"); tracker.addImage(backGroundImage, 0); gifImages[0] = Toolkit.getDefaultToolkit().     getImage("puck.gif"); tracker.addImage(gifImages[0], 0); gifImages[1] = Toolkit.getDefaultToolkit().     getImage("blocker.gif"); tracker.addImage(gifImages[1], 0); try {     tracker.waitForID(0); }catch (InterruptedException e) {     System.out.println(e); } setTitle("Slapshot!"); setResizable(false); setSize(backGroundImage.getWidth(this),     backGroundImage.getHeight(this)); setVisible(true);     .     .     . 

Finally, the constructor creates a memory image for double buffering, creates and starts the new thread that will move the pucks, and adds a window listener to handle window-closing events:

     memoryImage = createImage(getSize().width, getSize         ().height);     memoryGraphics = memoryImage.getGraphics();     thread = new Thread(this);     thread.start();     this.addWindowListener(new WindowAdapter(){         public void windowClosing(             WindowEvent e){                 runOK = false;                 System.exit(0);             }         }     ); } 

When the application starts, the constructor runs and the hockey rink appears; to make something actually happen, you have to select the Start item in the File menu.



    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