Class KnightsTour

java.lang.Object
   |
   +----java.awt.Component
           |
           +----java.awt.Container
                   |
                   +----java.awt.Panel
                           |
                           +----java.applet.Applet
                                   |
                                   +----KnightsTour

public class KnightsTour
extends Applet
implements BoardListener, ActionListener, ItemListener
An applet that finds a Hamiltonian cycle for the knight on a chess board. Uses a simple and intuitive graphical user interface, letting the user resize the board, choose the start square for searching, optionally view the process as an animation, and pause at will.

This class provides a functioning environment for the KnightThread and GraphicalBoard objects that do the actual work. Note that using a GUI is not mandatory: the program could be implemented as a text-mode application as well.

Author:
Markku Vähäaho
See Also:
KnightThread, GraphicalBoard

Variable Index

 o anim
 o animCheckbox
 o board
 o clearButton
 o height
 o image
 o label
 o panel
 o pauseButton
 o running
 o thread
 o width

Constructor Index

 o KnightsTour()

Method Index

 o actionPerformed(ActionEvent)
Takes appropriate action when the user selects a button.
 o boardClicked(int, int)
Sets the start square that the user chose and starts the KnightThread threaed.
 o boardResized(int, int)
Creates a new board of the specified size and initializes a new thread for it.
 o getAppletInfo()
Returns textual information of the applet.
 o getParameterInfo()
Returns information of acceptable applet parameters that can be specified in the HTML file.
 o init()
Initializes the applet.
 o itemStateChanged(ItemEvent)
Toggles animation on or off when the 'Animation' checkbox state changes.
 o start()
Frees applet resources.
 o stop()
Suspends the KnightThread thread.

Variables

 o board
 private GraphicalBoard board
 o image
 private Image image
 o thread
 private KnightThread thread
 o label
 private final Label label
 o panel
 private final Panel panel
 o clearButton
 private final Button clearButton
 o pauseButton
 private final Button pauseButton
 o animCheckbox
 private final Checkbox animCheckbox
 o width
 private int width
 o height
 private int height
 o running
 private boolean running
 o anim
 private boolean anim

Constructors

 o KnightsTour
 public KnightsTour()

Methods

 o init
 public void init()
Initializes the applet. Loads the knight image and creates a set of buttons, a GraphicalBoard and a KnightThread. The image name is specified in the applet parameters with the tag 'image'.

Overrides:
init in class Applet
 o start
 public void start()
Frees applet resources. Stops the thread, if running, and flushes the knight image from memory. public void destroy() { thread.stop(); image.flush(); } /** Starts the KnightThread thread.

Overrides:
start in class Applet
 o stop
 public void stop()
Suspends the KnightThread thread.

Overrides:
stop in class Applet
 o getAppletInfo
 public String getAppletInfo()
Returns textual information of the applet.

Returns:
a short two-lined copyright notice.
Overrides:
getAppletInfo in class Applet
 o getParameterInfo
 public String[][] getParameterInfo()
Returns information of acceptable applet parameters that can be specified in the HTML file.

Returns:
an array describing the parameters this applet looks for.
Overrides:
getParameterInfo in class Applet
 o boardResized
 public void boardResized(int new_width,
                          int new_height)
Creates a new board of the specified size and initializes a new thread for it. As a part of the BoardListener interface, the method is invoked by GraphicalBoard when the board is resized by the user.

Parameters:
new_width - new width of the board in squares.
new_height - new height of the board in squares.
 o boardClicked
 public void boardClicked(int x,
                          int y)
Sets the start square that the user chose and starts the KnightThread threaed. As a part of the BoardListener interface, the method is invoked by GraphicalBoard when a square is clicked on by the user. Clicks are disabled from here on.

Parameters:
x - x-index of the clicked square.
y - y-index of the clicked square.
See Also:
BoardListener
 o actionPerformed
 public void actionPerformed(ActionEvent e)
Takes appropriate action when the user selects a button. The effect of selecting the 'Clear' button is the same as if the board had just been resized. Also suspends or resumes the KnightThread thread when the user selects the 'pause' button.

Parameters:
e - button event received from AWT.
See Also:
ActionListener
 o itemStateChanged
 public void itemStateChanged(ItemEvent e)
Toggles animation on or off when the 'Animation' checkbox state changes.

Parameters:
e - checkbox event received from AWT.