Results 1 to 4 of 4
- 05-05-2011, 11:34 PM #1
Member
- Join Date
- May 2011
- Posts
- 11
- Rep Power
- 0
Towers of hanoi Jpanels not displaying during solution
This is a class assignment for practicing recursion (Towers of Hanoi), I know that the recursive part (solve() method) is correct and I believe the JPanels (disks) are actually moving panel to panel (pegs). However, the screen doesnt seem to want to update and show the disks each time one moves.
Any help with this issue would be appreciated, I believe the problem liesJava Code:import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Stack; import javax.swing.*; import javax.swing.border.LineBorder; public class TowersOfHanoiGUI extends JFrame { private static final long serialVersionUID = 1L; //Constructs the new JFrame with a GUI pane and control pane TowersOfHanoiGUI() { getContentPane().setLayout(new BorderLayout()); add(new GUIPane(), BorderLayout.CENTER); add(new GUIControlPane(), BorderLayout.EAST); } //main method public static void main(String[] args) { TowersOfHanoiGUI frame = new TowersOfHanoiGUI(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(800, 400); frame.setResizable(false); frame.setTitle("Towers of Hanoi"); frame.setLocationRelativeTo(null); frame.setVisible(true); } } //This panel contains 3 peg panels for holdingthe disks as the move class GUIPane extends JPanel { private static final long serialVersionUID = 1L; private static Peg peg1; private static Peg peg2; private static Peg peg3; GUIPane() { setLayout(new GridLayout(1, 3, 0, 0)); peg1 = new Peg(); peg2 = new Peg(); peg3 = new Peg(); add(peg1); add(peg2); add(peg3); } //This is the actual solution code (recursive) public static void solve(int n, char fromTower, char toTower, char auxTower) throws InterruptedException { //GUIPane.class.wait(); int delay = 1000; if (n == 1) {// Stopping condition System.out.println("Move disk " + n + " from " + fromTower + " to " + toTower); moveDisks(fromTower, toTower); Thread.sleep(delay); } else { solve(n - 1, fromTower, auxTower, toTower); System.out.println("Move disk " + n + " from " + fromTower + " to " + toTower); moveDisks(fromTower, toTower); Thread.sleep(delay); solve(n - 1, auxTower, toTower, fromTower); } } // This method moves the disks from one stack to the other public static void moveDisks(char from, char to) throws InterruptedException { Disk hold; if (from == 'A' && to == 'B') { hold = peg1.getStack().pop(); peg2.getStack().push(hold); peg1.drawDisks(); peg2.drawDisks(); } else if (from == 'A' && to == 'C') { hold = peg1.getStack().pop(); peg3.getStack().push(hold); peg1.drawDisks(); peg3.drawDisks(); } else if (from == 'B' && to == 'A') { hold = peg2.getStack().pop(); peg1.getStack().push(hold); peg2.drawDisks(); peg1.drawDisks(); } else if (from == 'B' && to == 'C') { hold = peg2.getStack().pop(); peg3.getStack().push(hold); peg2.drawDisks(); peg3.drawDisks(); } else if (from == 'C' && to == 'A') { hold = peg3.getStack().pop(); peg1.getStack().push(hold); peg3.drawDisks(); peg1.drawDisks(); } else /*if (from == 'C' && to == 'B')*/ { hold = peg3.getStack().pop(); peg2.getStack().push(hold); peg3.drawDisks(); peg2.drawDisks(); } } public static Peg getPeg(int n) { if (n == 1) { return peg1; } else if (n == 2) { return peg2; } else { return peg3; } } } /* * The peg class hold the disks (also has a peg shape thing in the paintComponent method) * Each Peg (3 total) has a Stack object that the disks get pushed or popped onto by * moveDisks(). * */ class Peg extends JPanel { private static final long serialVersionUID = 1L; private Stack<Disk> tower; private int centerX; Peg() { setLayout(null); tower = new Stack<Disk>(); } public void paintComponent(Graphics g) { Graphics2D g2d = (Graphics2D)g; g2d.setColor(Color.black); g2d.fillRect(0, 0, getWidth(), getHeight()); g2d.setColor(Color.gray); g2d.fill3DRect(getWidth() / 2, (3 * getHeight()) / 8, getWidth() / 20, getHeight(), true); } // This drawDisks method loops through each element in the stack and placed the // Disks in the correct point on the screen. Note, this may be bugged, but it // should still be displaying the disk somewhere. public void drawDisks() { centerX = (getWidth() / 2) + 6; for (int j = this.tower.size() - 1; j >= 0; j--) { tower.get(j).setBounds((7 * this.tower.get(j).getDiskNum()), GUIPane.getPeg(2).getHeight() - (15 * this.tower.get(j).getDiskNum()), this.tower.get(j).getDiskWidth(), this.tower.get(j).getDiskHeight()); add(tower.get(j)); tower.get(j).repaint(); tower.get(j).revalidate(); } } public Stack<Disk> getStack() { return tower; } public int getCenterX() { return centerX; } } }
in the drawDisks(), moveDisks(), and maybe solve() methods. Ive tried to fix it 1000 ways to no avail. There is a similar thread ill try to find it.Last edited by wassat676; 05-05-2011 at 11:51 PM.
-
too long; didn't read
but I did search for Thread.sleep(...) and found it. Not sure if this is your problem but it could cause problems by putting your whole GUI app to sleep. Consider perhaps a Swing Timer. And you may want to isolate your problem a bit so you could post smaller amount of code.
- 05-05-2011, 11:48 PM #3
Member
- Join Date
- May 2011
- Posts
- 11
- Rep Power
- 0
Well, its nothing to do with Thread.sleep because I took that out and set the number of disks to 12 (requires 4095 moves to solve) and the thing still doesnt update when a disk is moved.
I also shortened it a bit, took out some code that I dont think really matters, Just know that the Disk object is just a jpanel.Last edited by wassat676; 05-05-2011 at 11:52 PM.
- 05-06-2011, 04:11 AM #4
You're blocking the EDT with program logic and that doesn't allow it to repaint the GUI.
Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI With JFC/Swing)
db
Similar Threads
-
Towers of Hanoi
By myst in forum New To JavaReplies: 1Last Post: 07-11-2010, 06:24 PM -
Omg! Towers of Hanoi. Seriously. Why isnt this right?
By Meta in forum New To JavaReplies: 5Last Post: 04-28-2010, 04:42 PM -
Towers of Hanoi using Producer/Consumer Pattern
By dooey in forum New To JavaReplies: 0Last Post: 09-08-2009, 12:45 PM -
Problems displaying and array of JPanels
By dousedfirepants in forum New To JavaReplies: 3Last Post: 11-13-2008, 05:47 AM -
Problems with JPanels and displaying
By Mastergeek666 in forum AWT / SwingReplies: 1Last Post: 01-19-2008, 12:32 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks