added reset and exit button, moved status label down

git-svn-id: svn://anubis/gvsu@92 45c1a28c-8058-47b2-ae61-ca45b979098e
This commit is contained in:
josh 2008-03-29 00:14:57 +00:00
parent 80ee38a8cb
commit 0ec7c4f53f

View File

@ -18,13 +18,15 @@ public class KnightsTour extends JFrame
private JTextField m_startYField;
private JButton m_startButton;
private JButton m_cancelButton;
private JButton m_exitButton;
private JButton m_resetButton;
private JPanel m_mainPanel;
private JPanel m_boardPanel;
private JLabel m_statusLabel;
public KnightsTour()
{
super("Josh's Knights Tour project for CS621");
super("Josh's Knight's Tour project for CS621");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(500, 600);
@ -32,55 +34,67 @@ public class KnightsTour extends JFrame
getContentPane().add(m_mainPanel = new JPanel(new BorderLayout()) {{
setBorder(BorderFactory.createEmptyBorder(9, 9, 9, 9));
add(new JPanel() {{
setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 7));
add(new JPanel() {{ /* board size panel */
setBorder(BorderFactory.createTitledBorder("Board Size"));
add(new JPanel() {{
setBorder(BorderFactory.createEmptyBorder());
setLayout(new GridLayout(2, 2, 5, 5));
add(new JLabel("Width:", SwingConstants.RIGHT));
add(m_widthField = new JTextField("5"));
add(new JLabel("Height:", SwingConstants.RIGHT));
add(m_heightField = new JTextField("5"));
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
add(new JPanel() {{
setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 7));
add(new JPanel() {{ /* board size panel */
setBorder(BorderFactory.createTitledBorder("Board Size"));
add(new JPanel() {{
setBorder(BorderFactory.createEmptyBorder());
setLayout(new GridLayout(2, 2, 5, 5));
add(new JLabel("Width:", SwingConstants.RIGHT));
add(m_widthField = new JTextField("5"));
add(new JLabel("Height:", SwingConstants.RIGHT));
add(m_heightField = new JTextField("5"));
}});
}});
}});
add(new Box.Filler(new Dimension(5, 0),
new Dimension(5, 0),
new Dimension(5, 0)));
add(new JPanel() {{ /* starting position panel */
setBorder(BorderFactory.createTitledBorder("Starting Position"));
add(new JPanel() {{
setBorder(BorderFactory.createEmptyBorder());
setLayout(new GridLayout(2, 2, 5, 5));
add(new JLabel("Column (X):", SwingConstants.RIGHT));
add(m_startXField = new JTextField("2"));
add(new JLabel("Row (Y):", SwingConstants.RIGHT));
add(m_startYField = new JTextField("2"));
add(new Box.Filler(new Dimension(5, 0),
new Dimension(5, 0),
new Dimension(5, 0)));
add(new JPanel() {{ /* starting position panel */
setBorder(BorderFactory.createTitledBorder("Starting Position"));
add(new JPanel() {{
setBorder(BorderFactory.createEmptyBorder());
setLayout(new GridLayout(2, 2, 5, 5));
add(new JLabel("Column (X):", SwingConstants.RIGHT));
add(m_startXField = new JTextField("2"));
add(new JLabel("Row (Y):", SwingConstants.RIGHT));
add(m_startYField = new JTextField("2"));
}});
}});
}});
add(new Box.Filler(new Dimension(5, 0),
new Dimension(5, 0),
new Dimension(5, 0)));
add(new JPanel() {{ /* start button panel */
setBorder(BorderFactory.createTitledBorder("Start"));
add(new JPanel() {{
setBorder(BorderFactory.createEmptyBorder());
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
add(m_startButton = new JButton("Start Knight's Tour"));
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),
new Dimension(0, 4),
new Dimension(0, 4)));
add(m_statusLabel = new JLabel());
add(new Box.Filler(new Dimension(5, 0),
new Dimension(5, 0),
new Dimension(5, 0)));
add(new JPanel() {{ /* start button panel */
setBorder(BorderFactory.createTitledBorder(
"Control Knight's Tour"));
add(new JPanel() {{
setBorder(BorderFactory.createEmptyBorder());
setLayout(new GridLayout(2, 2, 5, 5));
add(m_startButton = new JButton("Start"));
m_startButton.addActionListener(m_handler);
add(m_resetButton = new JButton("Reset"));
m_resetButton.addActionListener(m_handler);
m_resetButton.setEnabled(false);
add(m_cancelButton = new JButton("Cancel"));
m_cancelButton.addActionListener(m_handler);
m_cancelButton.setEnabled(false);
add(m_exitButton = new JButton("Exit"));
m_exitButton.addActionListener(m_handler);
}});
}});
add(new Box.Filler(new Dimension(0, 0),
new Dimension(Short.MAX_VALUE, 0),
new Dimension(10000, 10000)));
}});
add(new JPanel() {{
setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
add(m_statusLabel = new JLabel("Ready."));
add(new Box.Filler(new Dimension(0, 0),
new Dimension(10000, 0),
new Dimension(10000, 10000)));
}});
add(new Box.Filler(new Dimension(0, 0),
new Dimension(Short.MAX_VALUE, 0),
new Dimension(10000, 10000)));
}}, BorderLayout.NORTH);
}});
@ -124,39 +138,7 @@ public class KnightsTour extends JFrame
{
if (e.getSource() == m_startButton)
{
m_statusLabel.setText("Running...");
/* first validate input text fields */
int width, height, startx, starty;
try
{
width = Integer.parseInt(m_widthField.getText());
height = Integer.parseInt(m_heightField.getText());
startx = Integer.parseInt(m_startXField.getText());
starty = Integer.parseInt(m_startYField.getText());
}
catch (NumberFormatException nfe)
{
m_statusLabel.setText("Non-numerical input!");
return;
}
if (width < 1 || height < 1 || startx < 0 || starty < 0 ||
startx >= width || starty >= height)
{
m_statusLabel.setText("Invalid input!");
return;
}
/* create a new KnightsTourBoard object */
m_ktBoard = new KnightsTourBoard(width, height);
createBoardPanel();
/* run the tour in a separate thread */
m_startButton.setVisible(false);
m_cancelButton.setVisible(true);
m_tourThread = new Thread(new TourThread(startx, starty));
m_tourThread.start();
setUpTour(true);
}
else if (e.getSource() == m_cancelButton)
{
@ -164,11 +146,64 @@ public class KnightsTour extends JFrame
{
m_tourThread.stop();
m_tourThread = null;
m_startButton.setVisible(true);
m_cancelButton.setVisible(false);
m_startButton.setEnabled(true);
m_cancelButton.setEnabled(false);
m_resetButton.setEnabled(true);
m_statusLabel.setText("Cancelled.");
}
}
else if (e.getSource() == m_resetButton)
{
setUpTour(false);
}
else if (e.getSource() == m_exitButton)
{
System.exit(0);
}
}
}
private void setUpTour(boolean runit)
{
/* first validate input text fields */
int width, height, startx, starty;
try
{
width = Integer.parseInt(m_widthField.getText());
height = Integer.parseInt(m_heightField.getText());
startx = Integer.parseInt(m_startXField.getText());
starty = Integer.parseInt(m_startYField.getText());
}
catch (NumberFormatException nfe)
{
m_statusLabel.setText("Non-numerical input!");
return;
}
if (width < 1 || height < 1 || startx < 0 || starty < 0 ||
startx >= width || starty >= height)
{
m_statusLabel.setText("Invalid input!");
return;
}
/* create a new KnightsTourBoard object */
m_ktBoard = new KnightsTourBoard(width, height);
createBoardPanel();
if (runit)
{
/* run the tour in a separate thread */
m_startButton.setEnabled(false);
m_cancelButton.setEnabled(true);
m_resetButton.setEnabled(false);
m_statusLabel.setText("Running...");
m_tourThread = new Thread(new TourThread(startx, starty));
m_tourThread.start();
}
else
{
m_statusLabel.setText("Reset.");
}
}
@ -191,8 +226,9 @@ public class KnightsTour extends JFrame
{
createBoardPanel();
m_statusLabel.setText("Finished.");
m_startButton.setVisible(true);
m_cancelButton.setVisible(false);
m_startButton.setEnabled(true);
m_cancelButton.setEnabled(false);
m_resetButton.setEnabled(true);
}
});
}
@ -203,8 +239,9 @@ public class KnightsTour extends JFrame
{
createBoardPanel();
m_statusLabel.setText("Solution not found!");
m_startButton.setVisible(true);
m_cancelButton.setVisible(false);
m_startButton.setEnabled(true);
m_cancelButton.setEnabled(false);
m_resetButton.setEnabled(true);
}
});
}