/* Name: Chris Johnson johnson@dcs.gla.ac.uk Last modified: 5/1/99 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.*; 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(); } public boolean action (Event event, Object what) { // every time a button is pressed then move the drone a step in the // corresponding direction - it is assumed that maze.update will check // for any collisions with the walls. if(event.target== left_button) { maze.update(-step,0); } else if (event.target== right_button) { maze.update(+step,0); } else if (event.target== up_button) { maze.update(0,-step); } else if (event.target== down_button) { maze.update(0,+step); } return true; } }