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.
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.