seems to be working... with cancel button to terminate thread
git-svn-id: svn://anubis/gvsu@91 45c1a28c-8058-47b2-ae61-ca45b979098e
This commit is contained in:
parent
1faf4bdb9b
commit
80ee38a8cb
@ -8,6 +8,7 @@ import javax.swing.border.*;
|
|||||||
public class KnightsTour extends JFrame
|
public class KnightsTour extends JFrame
|
||||||
{
|
{
|
||||||
private KnightsTourBoard m_ktBoard = new KnightsTourBoard(5, 5);
|
private KnightsTourBoard m_ktBoard = new KnightsTourBoard(5, 5);
|
||||||
|
private Thread m_tourThread;
|
||||||
|
|
||||||
/* GUI elements */
|
/* GUI elements */
|
||||||
private ActionEventHandler m_handler = new ActionEventHandler(this);
|
private ActionEventHandler m_handler = new ActionEventHandler(this);
|
||||||
@ -16,6 +17,7 @@ public class KnightsTour extends JFrame
|
|||||||
private JTextField m_startXField;
|
private JTextField m_startXField;
|
||||||
private JTextField m_startYField;
|
private JTextField m_startYField;
|
||||||
private JButton m_startButton;
|
private JButton m_startButton;
|
||||||
|
private JButton m_cancelButton;
|
||||||
private JPanel m_mainPanel;
|
private JPanel m_mainPanel;
|
||||||
private JPanel m_boardPanel;
|
private JPanel m_boardPanel;
|
||||||
private JLabel m_statusLabel;
|
private JLabel m_statusLabel;
|
||||||
@ -67,6 +69,9 @@ public class KnightsTour extends JFrame
|
|||||||
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
|
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
|
||||||
add(m_startButton = new JButton("Start Knight's Tour"));
|
add(m_startButton = new JButton("Start Knight's Tour"));
|
||||||
m_startButton.addActionListener(m_handler);
|
m_startButton.addActionListener(m_handler);
|
||||||
|
add(m_cancelButton = new JButton("Cancel Knight's Tour"));
|
||||||
|
m_cancelButton.addActionListener(m_handler);
|
||||||
|
m_cancelButton.setVisible(false);
|
||||||
add(new Box.Filler(new Dimension(0, 4),
|
add(new Box.Filler(new Dimension(0, 4),
|
||||||
new Dimension(0, 4),
|
new Dimension(0, 4),
|
||||||
new Dimension(0, 4)));
|
new Dimension(0, 4)));
|
||||||
@ -91,9 +96,9 @@ public class KnightsTour extends JFrame
|
|||||||
JPanel oldBoardPanel = m_boardPanel;
|
JPanel oldBoardPanel = m_boardPanel;
|
||||||
m_boardPanel = new JPanel(new GridLayout(height, width, 1, 1));
|
m_boardPanel = new JPanel(new GridLayout(height, width, 1, 1));
|
||||||
m_boardPanel.setBorder(BorderFactory.createEmptyBorder(5, 0, 0, 0));
|
m_boardPanel.setBorder(BorderFactory.createEmptyBorder(5, 0, 0, 0));
|
||||||
for (int x = 0; x < width; x++)
|
for (int y = 0; y < height; y++)
|
||||||
{
|
{
|
||||||
for (int y = 0; y < height; y++)
|
for (int x = 0; x < width; x++)
|
||||||
{
|
{
|
||||||
JLabel l = new JLabel(m_ktBoard.getStepAt(x, y) == 0
|
JLabel l = new JLabel(m_ktBoard.getStepAt(x, y) == 0
|
||||||
? ""
|
? ""
|
||||||
@ -147,9 +152,22 @@ public class KnightsTour extends JFrame
|
|||||||
createBoardPanel();
|
createBoardPanel();
|
||||||
|
|
||||||
/* run the tour in a separate thread */
|
/* run the tour in a separate thread */
|
||||||
m_startButton.setEnabled(false);
|
m_startButton.setVisible(false);
|
||||||
|
m_cancelButton.setVisible(true);
|
||||||
|
|
||||||
new Thread(new TourThread(startx, starty)).start();
|
m_tourThread = new Thread(new TourThread(startx, starty));
|
||||||
|
m_tourThread.start();
|
||||||
|
}
|
||||||
|
else if (e.getSource() == m_cancelButton)
|
||||||
|
{
|
||||||
|
if (m_tourThread != null)
|
||||||
|
{
|
||||||
|
m_tourThread.stop();
|
||||||
|
m_tourThread = null;
|
||||||
|
m_startButton.setVisible(true);
|
||||||
|
m_cancelButton.setVisible(false);
|
||||||
|
m_statusLabel.setText("Cancelled.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -173,7 +191,8 @@ public class KnightsTour extends JFrame
|
|||||||
{
|
{
|
||||||
createBoardPanel();
|
createBoardPanel();
|
||||||
m_statusLabel.setText("Finished.");
|
m_statusLabel.setText("Finished.");
|
||||||
m_startButton.setEnabled(true);
|
m_startButton.setVisible(true);
|
||||||
|
m_cancelButton.setVisible(false);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -184,10 +203,12 @@ public class KnightsTour extends JFrame
|
|||||||
{
|
{
|
||||||
createBoardPanel();
|
createBoardPanel();
|
||||||
m_statusLabel.setText("Solution not found!");
|
m_statusLabel.setText("Solution not found!");
|
||||||
m_startButton.setEnabled(true);
|
m_startButton.setVisible(true);
|
||||||
|
m_cancelButton.setVisible(false);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
m_tourThread = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user