import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/* <applet code="Hanoi.class" width="400" height="350"> </applet> */

public class Hanoi extends JApplet implements ActionListener {
  JTextField input;
  JTextArea output;

  @Override  
  public void init() {
    input = new JTextField("0", 8);
    input.addActionListener(this);
    output = new JTextArea(18, 22);
    setLayout(new FlowLayout());
    add(input);
    add(output);
  }

  void hanoi(int n, String a, String b, String c) {
    if (n==1) {
      output.append("円盤 1を、"+a+"から"+b+"へ。\n");
    }
    else {
      hanoi(n-1, a, c, b);
      output.append("円盤 "+n+"を、"+a+"から"+b+"へ。\n");
      hanoi(n-1, c, b, a);
    }
  }
  
  public void actionPerformed(ActionEvent e) {
    int n = Integer.parseInt(input.getText());

    hanoi(n, "棒A", "棒B", "棒C");
    output.append("以上\n\n");
  }

}

