bugishere

Listing Program of Hanoi Tower (GUI Display)

Posted on: January 19, 2011

According to my post yesterday about Make GUI Display. In this post, I will reveal the  program syntax of Hanoi Tower Game in Java (NetBeans). Because the program has two classes, Main.java and NewJFrame.java. Then follows the program syntax (listing) of each class.

a. Main.java

/*

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/

package hanoi;

/**

*

* @author afdhal syahrullah

*/

public class Main {

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

NewJFrame form = new NewJFrame ();

form.setVisible(true);

}

}

b. NewJFrame.java

/*

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/

/*

* NewJFrame.java

*

* Created on Dec 25, 2010, 11:02:50 AM

*/

package hanoi;

import javax.swing.JOptionPane;

/**

*

* @author afdhal syahrullah

*/

public class NewJFrame extends javax.swing.JFrame {

/** Creates new form NewJFrame */

public NewJFrame() {

initComponents();

}

/** This method is called from within the constructor to

* initialize the form.

* WARNING: Do NOT modify this code. The content of this method is

* always regenerated by the Form Editor.

*/

@SuppressWarnings(“unchecked”)

// <editor-fold defaultstate=”collapsed” desc=”Generated Code”>

private void initComponents() {

titleLabel = new javax.swing.JLabel();

ringLabel = new javax.swing.JLabel();

ringTextField = new javax.swing.JTextField();

jSeparator1 = new javax.swing.JSeparator();

executeButton = new javax.swing.JButton();

resetButton = new javax.swing.JButton();

processLabel = new javax.swing.JLabel();

jScrollPane1 = new javax.swing.JScrollPane();

processTextArea = new javax.swing.JTextArea();

jProgressBar1 = new javax.swing.JProgressBar();

exitButton = new javax.swing.JButton();

totalLabel = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

setTitle(“Hanoi Tower Program”);

titleLabel.setText(“Hanoi Tower”);

ringLabel.setText(“Enter Amount of Rings :”);

ringTextField.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

ringTextFieldActionPerformed(evt);

}

});

executeButton.setText(“Execute”);

executeButton.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

executeButtonActionPerformed(evt);

}

});

resetButton.setText(“Reset”);

resetButton.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

resetButtonActionPerformed(evt);

}

});

processLabel.setText(“Process :”);

processTextArea.setColumns(20);

processTextArea.setRows(5);

jScrollPane1.setViewportView(processTextArea);

exitButton.setText(“Exit”);

exitButton.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

exitButtonActionPerformed(evt);

}

});

totalLabel.setText(“Total Movements are : “);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());

getContentPane().setLayout(layout);

layout.setHorizontalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addContainerGap()

.addComponent(ringLabel)

.addGap(18, 18, 18)

.addComponent(ringTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 110, Short.MAX_VALUE)

.addGap(18, 18, 18)

.addComponent(executeButton)

.addGap(18, 18, 18)

.addComponent(resetButton)

.addContainerGap())

.addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 435, Short.MAX_VALUE)

.addGroup(layout.createSequentialGroup()

.addContainerGap()

.addComponent(processLabel)

.addContainerGap(381, Short.MAX_VALUE))

.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()

.addContainerGap()

.addComponent(exitButton)

.addGap(18, 18, 18)

.addComponent(totalLabel)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 83, Short.MAX_VALUE)

.addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

.addContainerGap())

.addGroup(layout.createSequentialGroup()

.addContainerGap()

.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 415, Short.MAX_VALUE)

.addContainerGap())

.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()

.addContainerGap(178, Short.MAX_VALUE)

.addComponent(titleLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(167, 167, 167))

);

layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addGap(18, 18, 18)

.addComponent(titleLabel)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(1, 1, 1)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(ringLabel)

.addComponent(resetButton)

.addComponent(executeButton)

.addComponent(ringTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addGap(18, 18, 18)

.addComponent(processLabel)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE)

.addGap(18, 18, 18)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)

.addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(exitButton)

.addComponent(totalLabel)))

.addContainerGap())

);

pack();

}// </editor-fold>

private void ringTextFieldActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

}

private void resetButtonActionPerformed(java.awt.event.ActionEvent evt) {

ringTextField.setText(“”);

processTextArea.setText(“”);

this.totalLabel.setText(“”) ;

}

private void executeButtonActionPerformed(java.awt.event.ActionEvent evt) {

try {

cincin = Integer.parseInt(

this.ringTextField.getText());

}

catch (Exception e) {

JOptionPane.showMessageDialog(this, “Bad Amount of Disk Entry”, “Error”, JOptionPane.ERROR_MESSAGE);

return;

}

int move = 0;

Towers(cincin, ‘A’, ‘B’, ‘C’);

move = processTextArea.getLineCount();

move = move – 1 ;

this.totalLabel.setText(“Total Movements are = ” + move);

}

private void Towers(int topN, char from, char inter, char to) {

if (topN == 1) {

//System.out.println(“Disk 1 from ” + from + ” to ” + to);

this.processTextArea.append(“Disk 1 from ” + from + ” to ” + to + “\n”);

} else {

Towers(topN – 1, from, to, inter);

this.processTextArea.append(“Disk ” + topN + ” from ” + from + ” to ” + to + “\n”);

Towers(topN – 1, inter, from, to);

}

}

private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {

System.exit(0);

}

/**

* @param args the command line arguments

*/

public static void main(String args[]) {

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new NewJFrame().setVisible(true);

}

});

}

// Variables declaration – do not modify

private javax.swing.JButton executeButton;

private javax.swing.JButton exitButton;

private javax.swing.JProgressBar jProgressBar1;

private javax.swing.JScrollPane jScrollPane1;

private javax.swing.JSeparator jSeparator1;

private javax.swing.JLabel processLabel;

private javax.swing.JTextArea processTextArea;

private javax.swing.JButton resetButton;

private javax.swing.JLabel ringLabel;

private javax.swing.JTextField ringTextField;

private javax.swing.JLabel titleLabel;

private javax.swing.JLabel totalLabel;

// End of variables declaration

private int cincin;

}

8 Responses to "Listing Program of Hanoi Tower (GUI Display)"

This site just created my week! I were seeking around for information on this. I’m glad now that I ran across this webpage. Woohoo!

Thanks very much😀

woy i2 program yg sy kasih tau cara bikinx toh :p

Where can I download this project of Hanoi tower? I need it.

Thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: