Java GUI Programmers Primer, A

The TextMenuTuttleInterface class supplies the part of the interface shown at the bottom of Figure 7.2, consisting of a single non-editable TextArea instance. The implementation of this class, as far as the end of its constructor, is as follows.

0001 // Filename TextMenuTuttleInterface.java. 0002 // Supplies text menu interface for a Tuttle. 0003 // 0004 // Written for Java Interface book chapter 7. 0005 // Fintan Culwin, v 0.2, August 1997. 0006 0007 package TextMenuTuttle; 0008 0009 import java.awt.*; 0010 import java.applet.*; 0011 import java.awt.event.*; 0012 0013 0014 class TextMenuTuttleInterface extends Panel { 0015 0016 protected static final int TOP_LEVEL_MENU = 0; 0017 protected static final int MOVE_MENU = 1; 0018 protected static final int MOVE_FORWARD_MENU = 2; 0019 protected static final int MOVE_BACKWARD_MENU = 3; 0020 protected static final int TURN_MENU = 4; 0021 protected static final int TURN_LEFT_MENU = 5; 0022 protected static final int TURN_RIGHT_MENU = 6; 0023 protected static final int COLOR_MENU = 7; 0024 protected static final int FOREGROUND_COLOR_MENU = 8; 0025 protected static final int BACKGROUND_COLOR_MENU = 9; 0026 protected static final int PEN_MENU = 10; 0027 protected static final int SCREEN_MENU = 11; 0028 protected static final int HELP_MENU = 12; 0029 protected static final int EXIT_MENU = 13; 0030 0031 private int menuState = TOP_LEVEL_MENU; 0032 0033 private TextArea menuArea; 0034 0035 protected TextMenuTuttleInterface( KeyListener itsListener) { 0036 0037 menuArea = new TextArea( 5, 60); 0038 menuArea.setEditable( false); 0039 menuArea.addKeyListener( itsListener); 0040 0041 this.add( menuArea); 0042 } // End TextMenuTuttleInterface constructor.

The class declaration, on line 0014, indicates that it extends the Panel class. It commences, on lines 0016 to 0029, with the declaration of fourteen protected manifest values to represent the possible states of the interface and, on line 0031, an instance attribute called menuState to record the state of the menu. The intention is that the menuState attribute will always reflect the menu which the interface is currently showing. Two protected methods, setMenuState() and menuStateIs() are provided to support the use of this attribute. As these resources are protected they can be seen by the only other class in the package, the textMenuTuttle class, nut are invisible outside the package. The use which the textMenuTuttle class makes of this knowledge will be described below.

The constructor, on lines 0035 to 0042, creates a non-editable TextArea instance, called menuArea, with 5 rows and 60 columns, and registers the KeyListener instance, itsListener, passed as an argument as its KeyListener attribute. This will cause the appropriate methods in itsListener to be called as the user operates the keyboard. The constructor concludes by adding the menuArea instance into itself as its only instance child. The implementation of the setMenuState() method is as follows.

0046 protected void setMenuState( int newState) { 0047 0048 menuState = newState; 0049 0050 switch( menuState) { 0051 case TOP_LEVEL_MENU: 0052 menuArea.setText( topLevelMenu); 0053 break; 0054 0055 case MOVE_MENU: 0056 menuArea.setText( topLevelMenu + moveMenu); 0057 break; 0058 0059 case MOVE_FORWARD_MENU: 0060 menuArea.setText( topLevelMenu + moveMenu + movefMenu); 0061 break; ---- // Other cases omitted. 0103 case EXIT_MENU: 0104 menuArea.setText( topLevelMenu + exitMenu); 0105 break; 0106 } // End switch. 0107 } // End setMenuState.

The method commences, on line 0048, by setting the value of menuState to the value of the argument passed to the method in newState. It continues with a multi-way switch structure containing a branch for each possible menu state and, within each branch, calls the menuArea's setText() method to cause the appropriate menu to be shown to the user. To accomplish this a number of Strings are declared at the end of the class, for example the Strings referred to in the fragment above are declared as follows.

0115 private static final String topLevelMenu = 0116 " Move Turn Colors Pen Screen Help Exit"; 0117 private static final String moveMenu = 0118 "\n Move: Forwards Backwards"; 0119 private static final String movefMenu = 0120 "\n Move Forwards: 5 10 25 "; ---- // Other cases omitted. 0141 private static final String exitMenu = 0142 "\n Exit: Yes No"; 0143 } // End class TextMenuTuttleInterface.

Thus on line 0060 as the interface moves into the MOVE_FORWARD_MENU state, the three strings on lines 0116, 0118 and 0120 are catenated together and displayed in the menuArea to produce the appearance shown in the lower illustration from Figure 7.3. The only other method of this class is the inquiry method menuStateIs(), whose implementation is as follows.

0110 protected int menuStateIs() { 0111 return menuState; 0112 } // End menuStateIs.


TextMenuTuttleInterface.java


7.4 The TextMenuTuttle class

7.2 The TextMenuTuttle interface


Категории