diff --git a/cs621/proj4/KnightsTour.java b/cs621/proj4/KnightsTour.java index cb9ffdd..212c2d1 100644 --- a/cs621/proj4/KnightsTour.java +++ b/cs621/proj4/KnightsTour.java @@ -89,7 +89,7 @@ public class KnightsTour extends JFrame int width = m_ktBoard.getWidth(); int height = m_ktBoard.getHeight(); JPanel oldBoardPanel = m_boardPanel; - m_boardPanel = new JPanel(new GridLayout(width, height, 1, 1)); + m_boardPanel = new JPanel(new GridLayout(height, width, 1, 1)); m_boardPanel.setBorder(BorderFactory.createEmptyBorder(5, 0, 0, 0)); for (int x = 0; x < width; x++) { @@ -145,17 +145,48 @@ public class KnightsTour extends JFrame /* create a new KnightsTourBoard object */ m_ktBoard = new KnightsTourBoard(width, height); createBoardPanel(); + + /* run the tour in a separate thread */ + m_startButton.setEnabled(false); - /* run the tour and update GUI */ - if (m_ktBoard.tour(startx, starty)) - { - createBoardPanel(); - m_statusLabel.setText("Finished."); - } - else - { - m_statusLabel.setText("Solution not found!"); - } + new Thread(new TourThread(startx, starty)).start(); + } + } + } + + private class TourThread implements Runnable + { + int m_startx, m_starty; + + public TourThread(int startx, int starty) + { + m_startx = startx; + m_starty = starty; + } + + public void run() + { + if (m_ktBoard.tour(m_startx, m_starty)) + { + SwingUtilities.invokeLater(new Runnable() { + public void run() + { + createBoardPanel(); + m_statusLabel.setText("Finished."); + m_startButton.setEnabled(true); + } + }); + } + else + { + SwingUtilities.invokeLater(new Runnable() { + public void run() + { + createBoardPanel(); + m_statusLabel.setText("Solution not found!"); + m_startButton.setEnabled(true); + } + }); } } }