Results 1 to 4 of 4
  1. #1
    wassat676 is offline Member
    Join Date
    May 2011
    Posts
    11
    Rep Power
    0

    Default 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.
    Java 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;
    	}
    }
    
    }
    Any help with this issue would be appreciated, I believe the problem lies
    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-06-2011 at 12:51 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    wassat676 is offline Member
    Join Date
    May 2011
    Posts
    11
    Rep Power
    0

    Default

    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-06-2011 at 12:52 AM.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

Similar Threads

  1. Towers of Hanoi
    By myst in forum New To Java
    Replies: 1
    Last Post: 07-11-2010, 07:24 PM
  2. Replies: 5
    Last Post: 04-28-2010, 05:42 PM
  3. Towers of Hanoi using Producer/Consumer Pattern
    By dooey in forum New To Java
    Replies: 0
    Last Post: 09-08-2009, 01:45 PM
  4. Problems displaying and array of JPanels
    By dousedfirepants in forum New To Java
    Replies: 3
    Last Post: 11-13-2008, 06:47 AM
  5. Problems with JPanels and displaying
    By Mastergeek666 in forum AWT / Swing
    Replies: 1
    Last Post: 01-19-2008, 01:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •