import java.util.*;
import java.awt.*;
import javax.swing.*;
public class Bou extends JPanel{
static int dmax;
char name;
LinkedList<Integer> pile = new LinkedList<Integer>();
public Bou(char nam,int maisuu) {
name = nam;
while ( maisuu >0 ){
pile.add(maisuu);
maisuu--;
}
}
public Bou(char nam) {
name = nam;
pile.clear();
}
public String toString(){
String x = name + ":";
for ( int p : pile){
x= x + p + " ";
}
return x;
}
public int rmDisk(){
int diskn = pile.removeLast();
repaint();
return diskn;
}
public void addDisk(int diskn){
pile.add(diskn);
repaint();
}
public Dimension getPreferredSize() {
return new Dimension(200,200);
}
public void paintComponent(Graphics myg){
super.paintComponent(myg);
int boux =(int)getWidth()/2;
int minrad =(int)(boux*0.1);
int dr =(int)(boux*0.8/(dmax-1));
int bottom =(int)(getHeight()*0.9);
int dh =(int)(getHeight()*0.8/dmax);
int mai = 0;
for ( int diskn : pile ){
mai++;
int g =(int)(127+128/dmax*diskn);
int b =(int)(255-255/dmax*(diskn-1));
myg.setColor(new Color(0,g,b));
int radius = minrad+dr*(diskn-1);
int x = boux-radius;
int y = bottom-dh*mai;
myg.fillRect(x,y,radius*2,dh);
myg.setColor(new Color(0,0,0));
myg.drawRect(x,y,radius*2,dh);
}
}
}

Hanoi7.java を改良します。
import java.awt.*;
import javax.swing.*;
//import java.util.*;
public class Hanoi8 {
long ct = 0;
//Bou[] bou = new Bou[3];
public void idou(int n,Bou src,Bou dst,Bou tmp){
if (n > 0){
idou(n-1,src,tmp,dst);
int disk=src.rmDisk();
dst.addDisk(disk);
try {
Thread.sleep(1000);
}
catch(InterruptedException ex) {
System.err.println(ex);
}
ct++;
//for(Bou b: bou) System.out.println ( b );
//System.out.println ();
idou(n-1,tmp,dst,src);
}
}
public Hanoi8(int maisuu){
Bou bou1 = new Bou('A',maisuu);
Bou bou2 = new Bou('B');
Bou bou3 = new Bou('C');
Bou.dmax=maisuu*2;
JFrame dai = new JFrame("Hanoi");
dai.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
dai.setLayout(new GridLayout(1,3,0,0));
dai.add(bou1);
dai.add(bou2);
dai.add(bou3);
dai.pack();
dai.setVisible(true);
try { Thread.sleep(1000); }
catch(InterruptedException ex) { System.err.println(ex); }
idou(maisuu,bou1,bou3,bou2);
}
public static void main( String[] args ) {
int n = Integer.parseInt(args[0]);
Hanoi8 hanoi = new Hanoi8(n);
System.out.println(hanoi.ct + "回");
System.exit(0);
}
}
実行結果はこうなります

