Chris Johnson, Index
Buttons - Better Class Structures...

/*
        Name:           Chris Johnson
                        johnson@dcs.gla.ac.uk
 
        Last modified:  11/1/2000 (by Pete Snowdon - snowdonp@dcs.gla.ac.uk)
 
        Description:    
        This class implements an interactive maze.   It forms
        part of a tutorial for a course on User Interface
        Design in Java on:

                http://www.dcs.gla.ac.uk/~johnson/teaching/hci-java
                
        A number of buttons (up, down, left and right) are placed 
        on a panel and these are used 
        to move the postion of a drone within a maze.

        Restrictions and extensions:
        As it stands the game is pretty boring - part of the aim of 
        the tutorial is to make it more interesting.
        (by including a score?  more walls?  keyboard interaction?).
*/


import java.awt.event.*;
import java.awt.*;
import java.applet.Applet;
 
public class MazeExample extends Applet {
 
        // create a canvas on which to draw the maze
        MazeCanvas maze = new MazeCanvas(this);
 
        // set up a panel to hold the control buttons
        Panel controls = new Panel();
 
        // create the buttons to place on the panel
        Button left_button = new Button("Left");  
        Button right_button = new Button("Right"); 
        Button up_button = new Button("Up");
        Button down_button = new Button("Down");
 
        // this specifies the number of pixels the drone will move at each step
        static final int step=5;
 
        public void init(){
 
                // sets the LayoutManager for the control panel only
                controls.setLayout(new FlowLayout(FlowLayout.LEFT));
                controls.add(left_button);
                controls.add(right_button);
                controls.add(up_button);
                controls.add(down_button);
 
                // sets the LayoutManager for the entire Applet
                setLayout(new BorderLayout());
                add("North", controls);
                add("Center", maze);
                // display the initialised maze
                maze.update(0,0); 
                validate();
 
        //sets the actioncommand for the buttons, part of 
        //the internationalisation of java, apparently.
        //We listen for these commands in the eventlistener
                left_button.setActionCommand("left");
                right_button.setActionCommand("right");
                up_button.setActionCommand("up");
                down_button.setActionCommand("down");
 
        //initialise the listener
                MazeMoveEvent mme = new MazeMoveEvent();
 
        //add the listener to each object
                left_button.addActionListener(mme);
                right_button.addActionListener(mme);
                up_button.addActionListener(mme);
                down_button.addActionListener(mme);
 
        }
 
        //inner class actionlistener
     class MazeMoveEvent implements ActionListener 
        {
                public void actionPerformed(ActionEvent event)
                {
                        if(event.getActionCommand() == "left") 
                        //uses the action command text
                        {
                                maze.update(-step,0);
                        }
                        else if (event.getActionCommand() == "right") 
                        //if not set then it uses button text
                        {
                                maze.update(+step,0);
                        }
                        else if (event.getActionCommand() == "up")
                        {
                                maze.update(0,-step);
                        }
                        else if (event.getActionCommand() == "down")
                        {
                                maze.update(0,+step);
                        }
 
                }
 
        }
  }  
See Exercise 3 for a complete listing.
forward