Question: Need help converting the following code for Tower of Hanoi moves to javafx: import java.util.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class Tower{ private

Need help converting the following code for Tower of Hanoi moves to javafx:

import java.util.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.*;

public class Tower{

private JLabel label;

private JLabel label2;

private JLabel label3;

private JTextField textfield;

private static JTextField textfield2;

private static JTextArea jtArea;

private JButton button;

private JPanel panel;

public static void main(String[] args) {

// Scanner input = new Scanner(System.in);

// Read number of disks, n

// System.out.print("Enter number of disks: ");

// int n = input.nextInt();

// Find the solution recursively

// System.out.println("Moves are:");

//display();

new Tower();

// moveDisks(n, 'A', 'B', 'C');

// System.out.println("Number of calls to the method is: " + count);

}

static int count = 0;

/** The method for finding the solution to move n disks

from fromTower to toTower with auxTower */

static String st1="";

public static void moveDisks(int n, char fromTower, char toTower, char auxTower) {

count++;

if (n == 1){ // Stopping condition

jtArea.append(" Move disk " + n + " from " + fromTower + " to " + toTower);

}

else {

moveDisks(n - 1, fromTower, auxTower, toTower);

jtArea.append(" Move disk " + n + " from " + fromTower + " to " + toTower);

moveDisks(n - 1, auxTower, toTower, fromTower);

}

//textfield2.setText(st1);

}

public Tower()

{

JFrame frame = new JFrame("Test");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(600,600);

frame.setLayout(null);

JPanel panel = new JPanel();

panel.setBounds(10,10,600,600);

panel.setLayout(null);

textfield = new JTextField();

textfield.setBounds(5,30,100,20);

//textfield2=new JTextField();

jtArea=new JTextArea();

jtArea.setBounds(10,50,400,400);

JButton button = new JButton("Find Moves");

button.setBounds(130,30,100,20);

button.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

String s=textfield.getText().toString();

int n = Integer.parseInt(s);

System.out.println("Moves are:");

new Tower();

moveDisks(n, 'A', 'B', 'C');

jtArea.append(" Number of calls to the method is: " + count);

}

});

panel.add(button);

panel.add(textfield);

panel.add(jtArea);

frame.add(panel);

frame.setVisible(true);

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!