Você está na página 1de 4

Codigo del Applet de las Torres de Hanoi

Clase Principal
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Principal extends JApplet {

JTextField ndiscos;
JButton VerSolucion;
JLabel l;

public void init(){
JPanel p=new JPanel();
l=new JLabel("Numero de Discos");
ndiscos=new JTextField(6);
VerSolucion=new JButton("Ver Solucion");
VerSolucion.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
TorresFrame tf=new TorresFrame(Integer.parseInt(ndiscos.getText()));
tf.setVisible(true);
tf.setBounds(0,0,510,550);
tf.setLocationRelativeTo(null);
tf.setTitle("Solucion Torres de Hanoi");
}
});
p.add(l);
p.add(ndiscos);
p.add(VerSolucion);
add(p);
}} |
Clase TorresFrame
import java.awt.*;

public class TorresFrame extends Frame{
private Hanoi h;
private boolean resolver;

public TorresFrame(int ndiscos) {

setLayout(new FlowLayout());
h = new Hanoi(ndiscos);
resolver = true;
}

public void paint(Graphics g) {h.dibuja(g);
if(resolver) {
h.resuelve(g);
}
}

} |
Clase Hanoi
import java.awt.*;

public class Hanoi{private int n;
private Poste izq, cen, der;public Hanoi(int n) {
this.n = n;
izq = new Poste(n, 80);
cen = new Poste(0, 250);
der = new Poste(0, 420);
}public void resuelve(Graphics g) {solucion(n, izq, der, cen, g);
}public void dibuja(Graphics g) {izq.dibuja(g);
cen.dibuja(g);
der.dibuja(g);
}

public void reinicia() {izq.ponDiscos(n);
der.ponDiscos(0);
cen.ponDiscos(0);
}public void solucion(int n, Poste f, Poste d, Poste a,Graphics g) {if(n==0){
return;
}else{
solucion(n-1,f,a,d,g);
mover(f,a,g);
solucion(n-1,d,f,a,g);
} }

public void mover(Poste a, Poste b, Graphics g) {try {
Thread.sleep(1000);
} catch(InterruptedException e) {
g.drawString("Error en sleep", 100, 100);
}Disco d=a.quitaDisco();
b.agregaDisco(d);
a.dibuja(g);
b.dibuja(g);

}
} |
Clase Poste
import java.awt.*;

public class Poste {private int n;
private int x, y;
private int anchodisco, basedisco;
private int anchobase, anchoposte;
private Disco disco[];

public Poste(int n, int px) {
this.n = n;
x = px;
y = 500;
anchodisco = 20;
basedisco = 140;
anchobase = 150;
anchoposte = 10;
disco = new Disco[10];
for (int i = 0; i < n; i++) {
disco[i]= new Disco((int)(basedisco * (1- 0.1 * i)), anchodisco);
}
}

public void ponDiscos(int n) {this.n = n;
disco = new Disco[10];
for(int i= 0; i < n; i++) {
disco[i]= new Disco((int)(basedisco * (1 - 0.1*i)), anchodisco);
}
}public void agregaDisco(Disco d) {disco[n] = d;
n++;
}

public Disco quitaDisco() {if(n!=0){
n--;
}
Disco d = disco[n];
disco[n] = null;
return d;
}

public void dibuja(Graphics g) {g.setColor(Color.white);
g.fillRect(x - anchobase / 2, 500 - 15 * anchodisco, anchobase, 15 * anchodisco);
g.setColor(Color.black);
g.fillRect(x - anchobase / 2, 500 - anchodisco, anchobase, anchodisco);
g.setColor(Color.ORANGE);
g.fillRect(x - anchoposte / 2, 500 - 15 * anchodisco, anchoposte, 14 * anchodisco);
for(int i = 0; i < n; i++) {
if(disco[i]!=null){
disco[i].dibuja(g, x, 500 - anchodisco - i * anchodisco);
}
}
}
} |
Clase Disco
import java.awt.*;public class Disco {

private int base, altura;public Disco(int b, int h) {

this.base = b;
this.altura = h;
}
public void dibuja(Graphics g, int x, int y) {

g.setColor(Color.red);
g.fillRoundRect(x-base/2,y-altura, base, altura,10,10);
g.setColor(Color.black);
g.drawRoundRect(x-base/2,y-altura, base, altura,10,10);
}
}

Você também pode gostar