Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Help with tower of Hanoi

807589Mar 11 2008 — edited Jan 16 2009
I am having trouble figuring out how I can get the disks to be drawn in the window based on the number in the comboBox.

This is the code I have so far. There are a few variable that have yet to be used and I may not need them at all.
package project4;

import javax.swing.*;
import java.awt.*;

public class Hanoi extends JFrame {

    private static final int WINDOW_WIDTH = 600;
    private static final int WINDOW_HEIGHT = 400;
    private ControlPanel controlPanel;
    private Container container;
    private JPanel gamePanel;
    private AnimationPanel animation = new AnimationPanel();

    public static void pauseBoard() {
        try {
            Thread.sleep(20);
        }//end try 
        catch (Exception e) {
            System.out.println(e);
            e.printStackTrace();
        }//end catch
    }//end pauseBoard
    public Hanoi() {
        super("Hanoi");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(WINDOW_WIDTH, WINDOW_HEIGHT);

        gamePanel = new JPanel();


        controlPanel = new ControlPanel();
        gamePanel.setBackground(Color.BLUE);
        setLayout(new BorderLayout());
        add(gamePanel);
        add(controlPanel, BorderLayout.SOUTH);
        center(this);

        setVisible(true);

        while (true) {
            pauseBoard();
            repaint();
        }
    }//end Hanoi
    public void paint(Graphics g) {
        g.setColor(Color.RED);
        g.fillRect(75, 310, 100, 20);
        g.fillRect(113, 110, 20, 200);

        g.fillRect(250, 310, 100, 20);
        g.fillRect(290, 110, 20, 200);

        g.fillRect(435, 310, 100, 20);
        g.fillRect(475, 110, 20, 200);
    }//end paint
    public static void center(JFrame frame) {
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        Point center = ge.getCenterPoint();

        int w = frame.getWidth();
        int h = frame.getHeight();

        int x = center.x - w / 2, y = center.y - h / 2;
        frame.setBounds(x, y, w, h);
        frame.validate();
    }//end center
    public static void main(String[] args) {
        Hanoi h = new Hanoi();
    }//end main
}
package project4;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class ControlPanel extends JPanel {

    public JButton solve;
    public JButton restart;
    public JComboBox diskNumber;
    public JPanel container1;

    public ControlPanel() {
        solve = new JButton("Solve");
        solve.addActionListener(new solveListener());
        restart = new JButton("Restart");
        restart.addActionListener(new restartListener());
        String[] numbers = {"3", "4", "5", "6", "7", "8", "9", "10"};
        diskNumber = new JComboBox(numbers);
        diskNumber.addActionListener(new diskListener());
        add(solve);
        add(restart);
        add(diskNumber);
        setVisible(true);
    }//end ControlPanel
    public void paintComponent(Graphics g) {
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, this.getWidth(), this.getHeight());
    }//end paintComponent
    private class solveListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
        }//end actionPerformed
    }//end solveListener
    private class restartListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
        }//end actionPerformed
    }//end restartListener
    private class diskListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {

        }//emd actionPerformed
    }//end discListener
}//end class ControlPanel
I've also got this animation class but now that I look at it I'm not even sure I need it. This is where I had planned on trying to draw the disks and I had been trying to get the pillars drawn in here as well but it didn't seem to want to cooperate.
package project4;

import javax.swing.*;
import java.awt.*;

public class AnimationPanel extends JPanel {

    public AnimationPanel() {
    }//end AnimationPanel
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.RED);
        g.fillRect(300, 100, 20, 20);
    }//end paintComponent
}//end class AnimationPanel\
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 13 2009
Added on Mar 11 2008
21 comments
539 views