To conclude this chapter the TuttleTest class which was used to provide the illustration in Figure 4.1 will be briefly introduced. The following chapters will present some more sophisticated interfaces for the Tuttle class. The implementation of this class as far as its init() method is as follows.
0001 // Filename TuttleTest.java. 0002 // First attempt at a Tuttle Interface, 0003 // interim version only. 0004 // 0005 // Written for the Java Interface Book Chapter 4. 0006 // Fintan Culwin, v 0.2, August 1997. 0007 0008 0009 import java.awt.*; 0010 import java.awt.event.*; 0011 import java.applet.*; 0012 0013 import Tuttles.Tuttle; 0014 0015 0016 public class TuttleTest extends Applet 0017 implements ActionListener { 0018 0019 private Tuttle theTuttle; 0020 private Label feedbackLabel; 0021 private Panel feedbackPanel;
The context imports the Tuttle class from the Tuttles package and the TuttleTest class is declared as implementing the ActionListener interface, as it will be its own listener when ActionEvents are generated by its buttons. Three instance attributes are delcare don lines 0019 to 0021: an instance of the Tuttle class called theTuttle, a Label called feedbackLabel and a Panel called feedbackPanel upon which it will be mounted.
0023 public void init() { 0024 0025 Panel tuttlePanel, tuttleButtonsPanel; 0026 Button fwdButton, bwdButton, rtnButton, ltnButton, rstButton, 0027 clrButton, cstButton, fgrButton, fglButton, fgbButton, 0028 bgyButton, bgwButton, pdnButton, pupButton; 0029 0030 this.setLayout( new BorderLayout()); 0031 0032 tuttlePanel = new Panel(); 0033 tuttlePanel.setBackground( Color.white); 0034 theTuttle = new Tuttle( this, 400, 400); 0035 tuttlePanel.add( theTuttle); 0036 0037 tuttleButtonsPanel = new Panel(); 0038 tuttleButtonsPanel.setLayout( new GridLayout( 2, 7)); 0039 0040 fwdButton = new Button( "Fwd"); 0041 fwdButton.setActionCommand( "Fwd"); 0042 fwdButton.addActionListener( this); 0043 tuttleButtonsPanel.add( fwdButton); 0044 0045 bwdButton = new Button( "Bwd"); 0046 bwdButton.setActionCommand( "Bwd"); 0047 bwdButton.addActionListener( this); 0048 tuttleButtonsPanel.add( bwdButton); ---- //Other Button construction and configuration omitted! 0114 feedbackPanel = new Panel(); 0115 feedbackPanel.setBackground( Color.white); 0116 feedbackLabel = new Label(); 0117 feedbackPanel.add( feedbackLabel); 0118 0119 this.add( tuttleButtonsPanel, "South"); 0120 this.add( tuttlePanel, "Center"); 0121 this.add( feedbackPanel, "North"); 0122 this.feedback(); 0125 } // End init.
This method declares two Panels, which will be required for layout control on line 0025 and, on lines 0026 to 0028, the fourteen buttons which are shown at the bottom of the interface. The first part of the method, on line 0030, establishes a BorderLayout policy for the Applet. The Tuttle instance, theTuttle, is then created and mounted in its own Panel in lines 0032 to 0035.
Following this the Panel to mount the buttons on is created and its layout policy set to a 2 row by 7 column GridLayout. Each Button in turn is then created, has its ActionCommand attribute set, has the Applet registered as its ActionListener and is added to the tuttleButtonsPanel. Once all Buttons have been processed, lines 0114 to 0117 create the feedbackLabel and install it onto its own Panel. The init() method concludes by assembling the interface and calls the feedback() method, as follows, to produce the initial information display at the top of the interface.
0187 private void feedback() { 0188 feedbackLabel.setText( theTuttle.getDetails()); 0189 feedbackPanel.doLayout(); 0190 } // End feedback.
The feedback() method obtains the information it needs by calling theTuttle's getDetails() method and installs this String into the feedbackLabel, calling the feedbackPanel's doLayout() method to ensure that the size of the Label is changed, if necessary.
All ActionEvents generated by the Buttons are sent to the actionPerformed() method, which extracts the commandString from it and calls the appropriate Tuttle method with the required argument, if any, as follows. As indicated in the implementation a movement, forwards or backwards, is limited to 25 tuttle steps and a turn, right or left, to 30 degrees. This limitation will be addressed in the chapters which follow.
0129 public void actionPerformed( ActionEvent event) { 0130 0131 String itsCommand = event.getActionCommand(); 0132 0133 if ( itsCommand.equals("Fwd")) { 0134 theTuttle.forward( 25); 0135 } else if ( itsCommand.equals("Bwd")) { 0136 theTuttle.backward( 25); 0137 } else if ( itsCommand.equals("Rtn")) { 0138 theTuttle.turnRight( 30); ---- // Other branches omitted! 0176 } else if ( itsCommand.equals("Pup")) { 0177 theTuttle.setPenUp(); 0178 } // End if. 0179 0180 this.feedback(); 0181 } // End actionPerformed.
Pressing all of the buttons at the bottom of the interface will ensure that each of the methods which the Tuttle class offers is called at least once, thus producing an initial confidence that the Tuttle class seems to be working correctly.
TuttleTest.java
TuttleTest
Summary of Chapter 4
4.4 The TuttleCursor class