import java.awt.*; import java.applet.Applet; import java.util.Stack; // Jorge Granados Delgadillo public class Hanoi extends Applet implements Runnable { int torreInicial, torreIntermedia, torreFinal; Stack torre[]; int retardo; int numMov; static final Color col[] = { Color.red, Color.blue, Color.yellow, Color.green, Color.magenta, Color.pink, Color.cyan, Color.orange, Color.darkGray }; Dimension dimAguja; Image imagenEnMemoria; Thread hilo; public void init() { )).intValue(); retardo = Integer.parseInt(getParameter("retardo")); imagenEnMemoria = createImage(size().width, size().height); } public void start() { torre = new Stack[3]; for (int numTorre = 0; numTorre < 3; numTorre++) torre[numTorre] = new Stack(); dimAguja = new Dimension(); setBackground(Color.white); if (hilo == null) { hilo = new Thread(this); hilo.start(); } } public void stop() { if (hilo != null) { hilo.stop(); hilo = null; } } public void update(Graphics g) { paint(g); } public void paint(Graphics g) { for (int numTorre = 0; numTorre < 3; numTorre++) { ja.width /4, dimAguja.height / 2, dimAguja.width / 3, int posiDisco = torre[numTorre].indexOf(new Integer(numDisco)); if (posiDisco != -1) { Aguja.width / 2, numDisco * dimAguja.width, dimAguja.height, true); } } } + new Integer(numMov).toString(), dimAguja.width / 2, g.drawImage(imagenEnMemoria, 0, 0, this); } public void run() { int torreInicial = 0; int torreIntermedia = 1; int torreFinal = 2; repaint(); try { Thread.sleep(2000); numMov = 0; } catch (InterruptedException e) { return; } } oDeDiscos) { torre[torreFinal].push(torre[torreInicial].pop()); numMov++; repaint(); try { Thread.sleep((long)retardo); } catch (InterruptedException e) { return; } return; } hanoi(torreInicial, torreFinal, torreIntermedia, 1); } }