The SemiDirectTuttle class is responsible for constructing the interface by creating the Tuttle and the SemiDirectTuttleInterface instances, together with a feedback Panel as used in the TuttleTest applet in the previous chapter, and subsequently responding the ActionEvents which are dispatched to it from the TuttleButtons. The implementation of this class as far as the end of its init() action is as follows.
0001 // Filename SemiDirectTuttle.java. 0002 // Supplies a semi-direct interface the Tuttle class 0003 // using TuttleButtons. 0004 // 0005 // Written for Java Interface book chapter 5. 0006 // Fintan Culwin, v 0.2, August 1997. 0007 0008 package SemiDirectTuttle; 0009 0010 import java.awt.*; 0011 import java.applet.*; 0012 import java.awt.event.*; 0013 0014 import Tuttles.Tuttle; 0015 import SemiDirectTuttleInterface; 0016 0017 public class SemiDirectTuttle extends Applet 0018 implements ActionListener { 0019 0020 private Label feedbackLabel; 0021 private Tuttle theTuttle; 0022 private Panel feedbackPanel; 0023 0024 public void init() { 0025 0026 Panel tuttlePanel = new Panel(); 0027 SemiDirectTuttleInterface theInterface; 0028 0029 this.setFont( new Font( "TimesRoman", Font.PLAIN, 14)); 0030 feedbackLabel = new Label(); 0031 feedbackPanel = new Panel(); 0032 feedbackPanel.add( feedbackLabel); 0033 0034 theTuttle = new Tuttle( this, 500, 500); 0035 tuttlePanel.add( theTuttle); 0036 0037 theInterface = new SemiDirectTuttleInterface( this); 0038 0039 this.setLayout( new BorderLayout()); 0040 this.add( feedbackPanel, "North"); 0041 this.add( tuttlePanel, "Center"); 0042 this.add( theInterface, "South"); 0043 0044 this.feedback(); 0045 } // End init.
The init() action commences, on line 0029, by specifying the Font which will be inherited by the feedbackLabel and used to display the Tuttle's status information. It continues, on lines 0030 to 0032, by constructing the feedbackLabel and adding it to the feedbackPanel.
On lines 0034 and 0035, the Tuttle instance, theTuttle, is then constructed specifying a 500 by 500 pixel drawing area and is added to its Panel. Line 0037 constructs the SemiDirectTuttleInterface, theInterface, passing as its argument the identity of the SemiDirectTuttle applet (this) which is currently being constructed.
Having prepared the three parts of the interface these are added in the appropriate BorderLayout locations of the Applet Panel, to provide the complete interface shown in Figure 5.4. The last action of the init() action is to call the SemiDirectTuttle's private feedback() action to cause the Tuttle's initial status to be shown when the interface is first presented to the user.
As the SemiDirectTuttle class states that it implements the ActionListener interface, it has to implement an actionPerformed() action, as follows.
0048 public void actionPerformed( ActionEvent event) { 0049 0050 String theCommand = event.getActionCommand(); 0051 0052 if ( theCommand.equals( "Forwards")) { 0053 theTuttle.forward( 25); 0054 } else if ( theCommand.equals( "Backwards")) { 0055 theTuttle.backward( 25); 0056 } else if ( theCommand.equals( "Turn left")) { 0057 theTuttle.turnLeft( 15); ---- Other if branches omitted. 0094 } // End if. 0095 0096 this.feedback(); 0097 } // End actionPerformed.
The first stage of this action, on line 0050, is to retrieve the theCommand String from the event dispatched to it from the SemiDirectTuttleInterface. A 21 way if/ else if structure then provides a branch for each possible commandString associated with each of the 21 TuttleButtons; calling the appropriate theTuttle action and passing the appropriate argument, if required.
For example, if the user were to press the turnLeft button the ActionEvent generated would contain the commandString "Turn left". This would cause the condition on line 0056 to evaluate true and the turnLeft() action of theTuttle would be called, on line 0057, causing the tuttle to turn 15o to the left. Likewise pressing the forwardButton would result in the tuttle moving 25 steps forward when theTuttle's forward() action is called on line 0053. Unfortunately this interface does not allow the user any choice in the amounts of turn or movement which are associated with the buttons.
The final stage of the actionPerformed() action, on line 0096, is to call the feedback() action so that any changes in the Tuttle's status are reported back to the user. The implementation of this action is identical to that of the comparable action presented in the TuttleTest applet at the end of the last chapter and is presented here for the sake of completeness.
0100 private void feedback() { 0101 feedbackLabel.setText( theTuttle.getDetails()); 0102 feedbackPanel.doLayout(); 0103 } // End feedback. 0104 } // End SemiDirectTuttle.
This completes the SemiDirectTuttle implementation, details of how to obtain the parts of the source code omitted for the sake of brevity can be found at the start of Appendix B.
SemiDirectTuttle.java
SemiDirectTuttle
Summary of Chapter 5
5.4 The SemiDirectTuttleInterface class