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
-
anim
-
-
animCheckbox
-
-
board
-
-
clearButton
-
-
height
-
-
image
-
-
label
-
-
panel
-
-
pauseButton
-
-
running
-
-
thread
-
-
width
-
-
KnightsTour()
-
-
actionPerformed(ActionEvent)
- Takes appropriate action when the user selects a button.
-
boardClicked(int, int)
- Sets the start square that the user chose and starts the KnightThread
threaed.
-
boardResized(int, int)
- Creates a new board of the specified size and initializes a new thread for
it.
-
getAppletInfo()
- Returns textual information of the applet.
-
getParameterInfo()
- Returns information of acceptable applet parameters that can be specified
in the HTML file.
-
init()
- Initializes the applet.
-
itemStateChanged(ItemEvent)
- Toggles animation on or off when the 'Animation' checkbox state changes.
-
start()
- Frees applet resources.
-
stop()
- Suspends the KnightThread thread.
board
private GraphicalBoard board
image
private Image image
thread
private KnightThread thread
label
private final Label label
panel
private final Panel panel
clearButton
private final Button clearButton
pauseButton
private final Button pauseButton
animCheckbox
private final Checkbox animCheckbox
width
private int width
height
private int height
running
private boolean running
anim
private boolean anim
KnightsTour
public KnightsTour()
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
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
stop
public void stop()
- Suspends the KnightThread thread.
- Overrides:
- stop in class Applet
getAppletInfo
public String getAppletInfo()
- Returns textual information of the applet.
- Returns:
- a short two-lined copyright notice.
- Overrides:
- getAppletInfo in class Applet
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
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.
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
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
itemStateChanged
public void itemStateChanged(ItemEvent e)
- Toggles animation on or off when the 'Animation' checkbox state changes.
- Parameters:
- e - checkbox event received from AWT.