Write an applet which shows how to solve the Hanoi tower problem.
The applet must contain the following components:
-
A TextField which can be used to input the size of Hanoi
tower. When Enter was pressed in this TextField, the applet begins to show
steps to solve Hanoi tower.
-
Three graphics objects each rendering a pile of disks.
You can add some optional features for better presentation(score
will be better of course), such as
-
A component constrols the execution speed, such that users can see clearly how disks were moved.
-
Some fancy behaviors, such as flying disks between stacks.
-
Different colors for moving disks.
-
Another button for execution instead of the TextField.
Hint:
-
A pile of disks works like a stack. So you may create
a class Pile with push and pop methods.
-
Extends Canvas or Component to create a graphics object.
Examples about how to write an applet can be found in Chapter 1.
Here is an executable result>
Part of Pile.java is listed as follows to help you
understand how to do graphics on JDK1.1 (tested):
/* Program Name: Pile.java
* Subject: Maintain and render a pile of disks
* Author: Shiuh-Sheng Yu
* National ChiNan University
* Department of Information Management
* Since: 1999/03/20
* ToolKit: JDK1.1
*/
import java.awt.*;
public class Pile extends Canvas {
private int top;
private int[] data;
public Pile(int initDisks) {
super();
// initialize your data at follows
}
/**
* Push a disk of size n to this pile.
* This method enfoces screen update ASAP.
* @param size the size of the disk
*/
public void push(int size) {
// maintain your data structure
repaint();
}
/**
* Pop out a disk. This method enfoces screen update ASAP.
* @return size of the disk on top of the pile
*/
public int pop() {
// maintain your data structure
repaint();
// return the top disk
}
/**
* Draw disks of the pile on Graphics g
* /
public void paint(Graphics g) {
g.setColor(Color.blue);
// set any color you likes
// draw stick here
g.setColor(Color.red);
// set any color you likes
// draw disks here
}
}
Copy the following code and save it as AddConstraint.java.
This utility file helps you to layout your component.
/**
* Program Name: AddConstraint.java
* Subject: GridBagLayout utility
* Author: Shiuh-Sheng Yu
* National ChiNan University
* Department of Information Management
* Since: 06/14/1997
* Modify Date: 03/21/1999, add more comments
* ToolKit: JDK1.1
*/
public class AddConstraint {
public static void addConstraint(Container container, Component component,
int grid_x, int grid_y, int grid_width, int grid_height,
int fill, int anchor, double weight_x, double weight_y,
int top, int left, int bottom, int right) {
GridBagConstraints c = new GridBagConstraints();
c.gridx = grid_x; c.gridy = grid_y;
c.gridwidth = grid_width; c.gridheight = grid_height;
c.fill = fill; c.anchor = anchor;
c.weightx = weight_x; c.weighty = weight_y;
c.insets = new Insets(top,left,bottom,right);
((GridBagLayout)container.getLayout()).setConstraints(component,c);
container.add(component);
}
}
The applet example is listed as following:
/**
* Program Name: Hanoi.java
* Subject: An applet shows how to solve Hanoi tower problem
* Author: Shiuh-Sheng Yu
* National ChiNan University
* Department of Information Management
* Since: 1999/03/20
* Modify Date: 3/21/1999, add more comments
* Toolkit: JDK1.1
*/
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class Hanoi extends Applet implements ActionListener, Runnable {
TextField input;
Pile a=null, b=null, c=null;
Panel display;
int size;
// Setup GUI
public void init() {
this.setLayout(new GridBagLayout());
AddConstraint.addConstraint(this,
new Label("Enter integer and press Enter:"), 0, 1, 1, 1,
GridBagConstraints.NONE,
GridBagConstraints.WEST,
0,0,0,0,0,0);
AddConstraint.addConstraint(this,
input = new TextField(10), 1, 1, 1, 1,
GridBagConstraints.NONE,
GridBagConstraints.WEST,
0,0,0,0,0,0);
input.addActionListener(this);
AddConstraint.addConstraint(this,
display = new Panel(), 0, 0, 2, 1,
GridBagConstraints.BOTH,
GridBagConstraints.CENTER,
1,1,0,0,0,0);
display.setLayout(new GridLayout(1,3));
input.requestFocus();
}
// process user's action in TextField input
public void actionPerformed(ActionEvent e) {
try {
size = Integer.parseInt(e.getActionCommand());
if (size > 0) {
display.removeAll();
display.add(a = new Pile(size));
display.add(b = new Pile(0));
display.add(c = new Pile(0));
// Ensures that this component has a valid layout.
display.validate();
// Creates a thread to move our disks.
// So that we can return control back immediately to window system.
(new Thread(this)).start();
}
} catch(NumberFormatException ex) { // not a number in TextField
input.setText("");
}
}
// starts to move disks
public void run() {
move(size, a, b, c);
}
// recursively solve the Hanoi problem
private void move(int n, Pile from, Pile to, Pile free) {
if (n > 0) {
// move n-1 disks
// move 1 disk
try { // sleep 1 second to let users see what happened
Thread.currentThread().sleep(1000);
} catch(InterruptedException ex) {
}
// move n-1 disks
}
}
}