6.5 The MenuBarTuttle class

The MenuBarTuttle class has the responsibility for constructing the MenuBarTuttleInstance passing to it a Frame which it has created and subsequently installing into it an instance of the Tuttle class and a feedback area, before the Fame, and the interface it contains, is made visible to the user. It then listens to the ActionEvents sent to it by the MenuBarTuttleInterface and the ExitDialog instances, controlling theTuttle and the applet in response. The implementation of the MenuBarTuttle class as far as the start of its init() method is as follows.

0001  // Filename MenuBarTuttle.java. 0002  // Supplies a main menu interface for the Tuttle class. 0003  // 0004  // Written for Java Interface book chapter 6. 0005  // Fintan Culwin, v 0.2, August 1997. 0006   0007  package MenuBarTuttle; 0008   0009  import java.awt.*; 0010  import java.applet.*; 0011  import java.awt.event.*; 0012  import java.util.StringTokenizer; 0013   0014  import Tuttles.Tuttle; 0015  import MenuBarTuttle.MenuBarTuttleInterface; 0016   0017  import MenuBarTuttle.ExitDialog; 0018  import MenuBarTuttle.VersionDialog; 0019  import MenuBarTuttle.MenuBarTuttleHelpDialog; 0020   0021  public class MenuBarTuttle extends    Applet  0022                             implements ActionListener { 0023   0024  private Label  feedbackLabel; 0025  private Panel  feedbackPanel; 0026   0027  private Tuttle                  theTuttle; 0028  private MenuBarTuttleInterface  theInterface; 0029   0030  private ExitDialog              exitDialog; 0031  private VersionDialog           versionDialog; 0032  private MenuBarTuttleHelpDialog helpDialog;

Lines 0014 to 0019 import the various classes indicated in the instance diagram in Figure 6.4 after the required Java packages, including the StringTokenizer class, have been imported. Following the class declaration, on lines 0021 and 0022, the feedback Label and Panel are declared as instance attributes, as are theTuttle Tuttle instance and theInterface MenuBarTuttleInterface instances, on lines 0024 to 0028. The instance attribute declarations conclude with the declaration of three specialized dialogs on lines 0030 to 0032. The init() method constructs the applet, as follows,

0036     public void init() {  0037      0038     Panel  tuttlePanel;  0039     Frame  tuttleFrame; 0040      0041        tuttleFrame = new Frame(); 0042        tuttleFrame.setTitle( "Menu Bar Tuttle Interface"); 0043        tuttleFrame.setBackground( Color.white); 0044        tuttleFrame.setFont( new Font( "TimesRoman", Font.PLAIN, 20)); 0045                          0046        feedbackPanel = new Panel(); 0047        feedbackLabel = new Label(); 0048        feedbackPanel.add( feedbackLabel); 0049         0050        tuttlePanel   = new Panel();  0051        theTuttle     = new Tuttle( this, 500, 500);             0052        tuttlePanel.add( theTuttle);  0053         0054        theInterface = new MenuBarTuttleInterface( tuttleFrame,  0055                                                   (ActionListener) this); 0056        theInterface.setForegroundCheckmark( "blue"); 0057        theInterface.setBackgroundCheckmark( "yellow"); 0058        theInterface.setPenUpCheckmark( false)  0059   0060        tuttleFrame.add( tuttlePanel,   "Center"); 0061        tuttleFrame.add( feedbackPanel, "South"); 0062         0063        tuttleFrame.setSize( tuttleFrame.getPreferredSize());       0064        this.feedback(); 0065        tuttleFrame.setVisible( true);     0066          0067        helpDialog    = new MenuBarTuttleHelpDialog( tuttleFrame); 0068        exitDialog    = new ExitDialog( tuttleFrame, this);  0069        versionDialog = new VersionDialog( tuttleFrame, this.getAppletInfo());           0070     } // End init.

The init() method commences, on lines 0041 to 0044, by constructing and configuring an instance of the Frame class called tuttleFrame. Lines 0046 to 0048 then prepare the feedbackPanel and lines 0050 to 0052 the tuttlePanel, containing theTuttle Tuttle instance. Lines 0054 to 0055 then construct theInterface, passing as arguments the tuttleFrame to have the menu bar installed into and the identity of the MenuBarTuttle instance (this) being initialized as its listener object. Once constructed the color and pen menus are configured as appropriate on lines 0056 to 0058. A Frame instance, by default, has a BorderLayout policy and the MenuBar is automatically added to its "North" location. Lines 0060 and 0061 place the tuttlePanel into its "Center" location and the feedbackPanel into its "South" location. Lines 0063 to 0065 set the size of the Frame, update the feedbackPanel and then call the Frame setVisible() method to make the interface visible to the user. The final stage of the init() method, on lines 0067 to 0069, constructs the three specialized dialogs, which will be considered in detail below.

The actionPerformed() method of the MenuBarTuttle class will be called whenever the user activates any of the items on theInterface's menus. The first part of this method is as follows.

0071     public void actionPerformed( ActionEvent event) {  0072   0073     StringTokenizer tokenizer = new StringTokenizer(  0074                                           event.getActionCommand()); 0075     String theCommand  = tokenizer.nextToken(); 0076     String theArgument = ""; 0077      0078        if ( tokenizer.hasMoreTokens()) {  0079           theArgument = tokenizer.nextToken();       0080        } // End if. 0081   0082   0083        if ( theCommand.equals( "exit")) {  0084           if ( theArgument.equals( "show")) {           0085              exitDialog.setVisible( true);  0086           } else if ( theArgument.equals( "please")) {  0087              System.exit( 0); 0088           } // End if.       0089                     0090        } else if ( theCommand.equals( "fd")) {  0091           theTuttle.forward( Integer.parseInt( theArgument)); 0092        } else if ( theCommand.equals( "bd")) {  0093           theTuttle.backward( Integer.parseInt( theArgument)); 0094   0095        } else if ( theCommand.equals( "tr")) {  0096           theTuttle.turnRight( Integer.parseInt( theArgument)); 0097        } else if ( theCommand.equals( "tl")) {   0098           theTuttle.turnLeft( Integer.parseInt( theArgument));

The method commences by constructing a StringTokenizer instance, called tokenizer, initialized to process the commandString retrieved from the ActionEvent passed to actionPerformed() as an argument. The commandStrings installed into theInterface's MenuItems included single word commands, such as "cs" for clear screen or "pu" for pen up; and two word commands, such as "fd 10" for forward 10 or "bg green" for background green. The tokenizer will facilitate the scanning of the commandString to divide the multiple word command into its constituent phrases. Line 0075 extracts the first word of the command into theCommand and lines 0076 to 0080 extract the second word, if any, into theArgument.

The remaining part of the performAction() method, starting on line 0083, consists of a multiple way if structure containing a branch for each of the possible commands. To facilitate maintenance the sequence of commands in the if structure corresponds to the sequence in which their MenuItems were added to the MenuBar in the constructor of the MenuBarTuttleInterface class. Consequently the first command to be considered, on lines 0083 to 0088, is the "exit" command; this part of the method will be described below when the ExitDialog class is described below.

Lines 0090 to 0098 process the commands originating from the Move and Turn menu. The commands passed will be "fd", "bd", "tr" and "tl" for forward, backward, turn right and turn left respectively. Each of these commands is followed by an integer indicating the number of steps to move or the number of degrees to turn. The repeated phrase Integer.parseInt( theArgument) will convert the integer contained as a String in theArgument into a primitive int value. This value is passed as an argument to the appropriate Tuttle command.

For example, if the user activates the MenuItem labelled "15 Degrees" on the Turn Right menu, the commandString passed in the ActionEvent instance will be "tr 15". The tokenizer will place "tr" into the theCommand String and "15" into the theArgument String. The if condition on line 0095 will evaluate true and line 0096 will call theTuttle's turnRight() method passing as an argument the int value 15. This will result in the tuttle shown to the user turning 15 degrees to the right and, as will be demonstrated below, the feedback shown to the user changing to show the tuttle's new direction.

The next commands to be considered originate from the Colors menu and will contain the command "fg" (foreground) or "bg" (background) followed by the name of the color. The "fg" command is processed as follows.

0101        } else if ( theCommand.equals( "fg")) { 0102           if ( theArgument.equals( "green")) {  0103              theTuttle.setForeground( Color.green); 0116           } else if ( theArgument.equals( "red")) {              ----           // Other branches omitted 0114           } // End if. 0115           theInterface.setForegroundCheckmark( theArgument);

Line 0101 guards for the "fg" command and the if structure starting on line 0102 contains a branch for each allowed tuttle color, the branches containing a call of the theTuttle's setForeground() method passing as an argument the appropriate manifest value from the Java Color class. Once the color has been changed the theInterface setForegroundCheckmark() method is called to update the checkmarks on the interface's foreground color menu. The implementation of the processing of the "bg" command is essentially identical.

The processing of the commands from the Screen menu do not need to consider the value stored in theArgument as they are all single word commands. The branches corresponding to the last three items on the menu are as follows.

0135        } else if ( theCommand.equals( "cr")) {    0136           theTuttle.clearAndReset(); 0137        } else if ( theCommand.equals( "pu")) {    0138           theTuttle.setPenUp(); 0139           theInterface.setPenUpCheckmark( true); 0140        } else if ( theCommand.equals( "pd")) {    0141           theTuttle.setPenDown(); 0142           theInterface.setPenUpCheckmark( false);

The "cr" command calls theTuttle's clearAndReset() method which does not require an argument. The "pu" and "pd" commands not only call theTuttle's setPenUp() and setPenDown() methods but also call theInterface's setPenStatus() method to update the checkmarks on the Screen menu. The actionPerformed() method concludes by processing the commands originating from the Help menu.

0145        } else if ( theCommand.equals( "version")) {    0146           versionDialog.setVisible( true);         0147        } else if ( theCommand.equals( "help")) {    0148           helpDialog.setVisible( true);         0149        } // End if. 0150         0151        this.feedback();        0152     } // End actionPerformed.

The versionDialog and helpDialog setVisible() methods will be considered below. After the outer if structure concludes on line 0149 the feedback() method is called to update the feedback shown to the user at the bottom of the interface and the method concludes on line 0152.




6.6 The Dialog class

6.4 The MenuBarTuttleInterface class

A Java GUI programmer's primer
Java GUI Programmers Primer, A
ISBN: 0139088490
EAN: 2147483647
Year: 1998
Pages: 85
Authors: Fintan Culwin

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