Estoy haciendo un programa en java donde el usuario puede jugar torres de hanoi o resolverlo solo, pero cuando hago q se resuelve solo me tira d una vz la solucion enontonces e intentado cn timers y sleeps para q pare redibuje y asi se mire paso a paso pero no funciona aki ta mi codigo si alguien me puede ayudar le debo la vida gracias
//************************************************** ******

la funcion se llama hanoi y donde dibujo es a travez del paint component

//************************************************** ******

import java.awt.*;
import java.io.*;
import java.text.*;
import java.util.*;
import javax.sound.sampled.*;
import javax.swing.*;
import javax.swing.Timer;
import java.awt.event.*;
import java.lang.*;

class stack
{
public int n;
int[] s=new int[10];

int pop()
{
int e=s[n];
s[n]=0;
n=n-1;
return e;
}

void push(int e)
{
n=n+1;
s[n]=e;
}
}

public class bbb extends JPanel
{

public JButton manual;
public JButton auto,jugar,nojugar;
public JButton ok,ok2;
public JButton torre1,torre2,torre3;
public JLabel info=new JLabel("Numero de Discos: ");
public JLabel instruc=new JLabel("Presiona la base de las torres para mover las fichas");
public JLabel instruc2=new JLabel("No puedes poner una pieza grande sobre una pequenia!");
public JComboBox numeros=new JComboBox();
public JComboBox velocidad=new JComboBox();

public boolean seguir=false,parar=false,primera=true;
public int n1,n2,n3;
public int click1=0;
public int opcion=1,tiempo=50;
public int op=1,continuar=0,cont=0;
public int piezas=0;
public int posx,posy;
public stack stack1=new stack(),stack2=new stack(),stack3=new stack();

public bbb() throws IOException
{
setPreferredSize(new Dimension(1366,768));
PanelMouseListener mouse=new PanelMouseListener();
addMouseListener(mouse);
addMouseMotionListener(mouse);
inicio();
torre1=new JButton();
torre2=new JButton();
torre3=new JButton();
torre1.setBounds(new Rectangle(150,600,250,25));
torre2.setBounds(new Rectangle(550,600,250,25));
torre3.setBounds(new Rectangle(950,600,250,25));
torre1.setBackground(Color.white);
torre2.setBackground(Color.white);
torre3.setBackground(Color.white);
torre1.addActionListener(new C1());
torre2.addActionListener(new C2());
torre3.addActionListener(new C3());
ok=new JButton("OK");
ok.setBounds(new Rectangle(270,50,70,25));
ok.addActionListener(new okiz());
ok2=new JButton("OK");
ok2.setBounds(new Rectangle(270,50,70,25));
ok2.addActionListener(new vel());
add(ok2);ok2.setVisible(false);
manual=new JButton("Manual");
manual.setBounds(new Rectangle(50,50,100,25));
manual.addActionListener(new m());
auto=new JButton("Automatico");
auto.setBounds(new Rectangle(50,80,100,25));
auto.addActionListener(new a());
jugar=new JButton("PLAY");
jugar.setBounds(new Rectangle(100,100,70,25));
jugar.addActionListener(new play());
nojugar=new JButton("PAUSE");
nojugar.setBounds(new Rectangle(100,150,70,25));
nojugar.addActionListener(new stop());
setLayout(null);
info.setBounds(new Rectangle(50,50,170,25));
info.setForeground(Color.white);
instruc.setBounds(new Rectangle(970,50,570,25));
instruc.setForeground(Color.white);
instruc2.setBounds(new Rectangle(970,70,570,25));
instruc2.setForeground(Color.white);
add(instruc);add(instruc2);
add(jugar);add(nojugar);jugar.setVisible(false);no jugar.setVisible(false);
add(info); info.setVisible(false);
add(ok); ok.setVisible(false);
add(manual);
add(auto);
add(torre1);add(torre2);add(torre3);
torre1.setVisible(false);torre2.setVisible(false); torre3.setVisible(false);

numeros.setBounds(new Rectangle(210,50,50,25));
numeros.addItem(1);numeros.addItem(2);numeros.addI tem(3);numeros.addItem(4);numeros.addItem(5);
numeros.addItem(6);numeros.addItem(7);numeros.addI tem(8);numeros.addItem(9);numeros.addItem(10);
add(numeros); numeros.setVisible(false);

velocidad.setBounds(new Rectangle(150,50,100,25));
velocidad.addItem("Lenta");
velocidad.addItem("Intermedia");
velocidad.addItem("Rapida");
add(velocidad);
velocidad.setVisible(false);
}

public boolean gano()
{
if ((stack3.n+1)==piezas)
return true;
else
return false;
}

public void inicio()
{
stack1.n=-1; stack2.n=-1; stack3.n=-1;

for(int i=0;i<10;i++)
{
stack1.push(0);
stack2.push(0);
stack3.push(0);
}

stack1.n=-1; stack2.n=-1; stack3.n=-1;
}

public void hanoi(int num, stack t1,stack t2,stack t3)
{
try {Thread.sleep(2000);} catch (InterruptedException e) {}
while(parar==true)
{}
if (num==1)
{
t3.push(t1.pop());
}
else
{
repaint();
hanoi((num-1),t1,t3,t2);
hanoi(1,t1,t2,t3);
hanoi((num-1),t2,t1,t3);
}
}


class PanelMouseListener extends MouseAdapter implements MouseMotionListener
{
//procedimientos cuando se hace click en un lugar del panel con el mouse
public void mouseClicked(MouseEvent e)
{
posx=e.getX();
posy=e.getY();
}
//procedimientos cuando se inicia el mouse en el panel
public void mouseEntered(MouseEvent e)
{
posx=e.getX();
posy=e.getY();
}
public void mouseExited(MouseEvent e)
{}
public void mousePressed(MouseEvent e)
{}
//procedimientos cuando se el mouse se desplaza en el panel (se apacha y teniendolo apachado se mueve)
public void mouseDragged(MouseEvent e)
{
posx=e.getX();
posy=e.getY();
}
//procedimientos cuando se mueve el mouse en el panel
public void mouseMoved(MouseEvent e)
{
posx=e.getX();
posy=e.getY();
}
}

public void paintComponent(Graphics g)
{
ImageIcon fondo= new ImageIcon("fondo.jpg");
g.drawImage(fondo.getImage(),0, 0,1366,768,null);

if (opcion==1) //ninguna
{}
else//manual o automatiko
{
g.setColor(Color.white);
g.fillRect(150,600,250,25);
g.fillRect(550,600,250,25);
g.fillRect(950,600,250,25);
g.setColor(Color.red);
g.fillRect(270,325,10,275);
g.fillRect(270+400,325,10,275);
g.fillRect(270+800,325,10,275);

int x, y,ii,top=0;
g.setColor(Color.yellow);

x=150;y=580;
if (stack1.n==-1){top=0;}else{top=stack1.n+1;}
for(ii=0;ii<top;ii++)
{ g.fillRect(x+(((10-stack1.s[ii])*125)/10),y-(((ii)*250)/10),(stack1.s[ii]*250)/10,20);}

x=550;y=580;
if (stack2.n==-1){top=0;}else{top=stack2.n+1;}
for(ii=0;ii<top;ii++)
{g.fillRect(x+(((10-stack2.s[ii])*125)/10),y-(((ii)*250)/10),(stack2.s[ii]*250)/10,20);}

x=950;y=580;
if (stack3.n==-1){top=0;}else{top=stack3.n+1;}
for(ii=0;ii<top;ii++)
{g.fillRect(x+(((10-stack3.s[ii])*125)/10),y-(((ii)*250)/10),(stack3.s[ii]*250)/10,20);}
}
setOpaque(false);
}

private class C1 implements ActionListener //torre1
{
public void actionPerformed(ActionEvent algo)
{ try{
if (click1==0)
{click1=1;}
else
{
if (click1==2)
{
if (stack1.n==-1 )
{stack1.push(stack2.pop());repaint();}
else
if (stack2.s[stack2.n]<stack1.s[stack1.n])
{stack1.push(stack2.pop());repaint();}
else
{JOptionPane.showMessageDialog(getComponentPopupMe nu(), "La pieza en la torre2 que desea mover debe ser mas pequenia que las de la torre1");}
}
else
{
if (stack1.n==-1 )
{stack1.push(stack3.pop());repaint();}
else
if (stack3.s[stack3.n]<stack1.s[stack1.n])
{stack1.push(stack3.pop());repaint();}
else
{JOptionPane.showMessageDialog(getComponentPopupMe nu(), "La pieza en la torre3 que desea mover debe ser mas pequenia que las de la torre1");}
}
continuar=0;
click1=0;
if (gano()){JOptionPane.showMessageDialog(getComponen tPopupMenu(),"~*~*~*~GANO~*~*~*~");}
}
}catch(Exception a){JOptionPane.showMessageDialog(getComponentPopup Menu(), "Movimiento no permitido");}
}
}

private class C2 implements ActionListener //torre2
{
public void actionPerformed(ActionEvent algo)
{ try{
if (click1==0)
{click1=2;}
else
{
if (click1==1)
{
if (stack2.n==-1)
{stack2.push(stack1.pop());repaint();}
else
if (stack1.s[stack1.n]<stack2.s[stack2.n])
{stack2.push(stack1.pop());repaint();}
else
{JOptionPane.showMessageDialog(getComponentPopupMe nu(), "La pieza en la torre1 que desea mover debe ser mas pequenia que las de la torre2");}
}
else
{
if(stack2.n==-1 )
{stack2.push(stack3.pop());repaint();}
else
if (stack3.s[stack3.n]<stack2.s[stack2.n])
{stack2.push(stack3.pop());repaint();}
else
{JOptionPane.showMessageDialog(getComponentPopupMe nu(), "La pieza en la torre3 que desea mover debe ser mas pequenia que las de la torre2");}
}
continuar=0;
click1=0;
if (gano()){JOptionPane.showMessageDialog(getComponen tPopupMenu(),"~*~*~*~GANO~*~*~*~");}
}
}catch(Exception a){JOptionPane.showMessageDialog(getComponentPopup Menu(), "Movimiento no permitido");}
}
}

private class C3 implements ActionListener //torre3
{
public void actionPerformed(ActionEvent algo)
{ try{
if (click1==0)
{click1=3;}
else
{
if (click1==1)
{
if(stack3.n==-1 )
{stack3.push(stack1.pop());repaint();}
else
if (stack1.s[stack1.n]<stack3.s[stack3.n])
{stack3.push(stack1.pop());repaint();}
else
{JOptionPane.showMessageDialog(getComponentPopupMe nu(), "La pieza en la torre1 que desea mover debe ser mas pequenia que las de la torre3");}
}
else
{
if(stack3.n==-1 )
{stack3.push(stack2.pop());repaint();}
else
if(stack2.s[stack2.n]<stack3.s[stack3.n])
{stack3.push(stack2.pop());repaint();}
else
{JOptionPane.showMessageDialog(getComponentPopupMe nu(), "La pieza en la torre2 que desea mover debe ser mas pequenia que las de la torre2");}
}
continuar=0;
click1=0;
if (gano()){JOptionPane.showMessageDialog(getComponen tPopupMenu(),"~*~*~*~GANO~*~*~*~");}
}
}catch(Exception a){JOptionPane.showMessageDialog(getComponentPopup Menu(), "Movimiento no permitido");}
}
}

private class play implements ActionListener //manual
{
public void actionPerformed(ActionEvent algo)
{
parar=false;
if(primera=true)
{
hanoi(stack1.n+1,stack1,stack2,stack3);
primera=false;
}
}
}

private class stop implements ActionListener //manual
{
public void actionPerformed(ActionEvent algo)
{
parar=true;
}
}

private class vel implements ActionListener //manual
{
public void actionPerformed(ActionEvent algo)
{
if (velocidad.getSelectedItem()=="Lenta")
{tiempo=150;}
else
if (velocidad.getSelectedItem()=="Intermedia")
{tiempo=75;}
else
tiempo=50;
ok2.setVisible(false);
jugar.setVisible(true);
nojugar.setVisible(true);
repaint();
}
}

private class m implements ActionListener //manual
{
public void actionPerformed(ActionEvent algo)
{
manual.setVisible(false);
auto.setVisible(false);
info.setVisible(true);
numeros.setVisible(true);
ok.setVisible(true);
op=2;
repaint();
}
}

private class a implements ActionListener //auto
{
public void actionPerformed(ActionEvent algo)
{
manual.setVisible(false);
auto.setVisible(false);
info.setVisible(true);
numeros.setVisible(true);
ok.setVisible(true);
op=3;
repaint();
}
}

private class okiz implements ActionListener //ok
{
public void actionPerformed(ActionEvent algo)
{
int no=Integer.parseInt(numeros.getSelectedItem().toSt ring());
piezas=no;
if (no>0 && no<11)
{
info.setVisible(false);
numeros.setVisible(false);
ok.setVisible(false);
int i=0;int resta=0;
while(i<no)
{
resta=10-i;
stack1.push(resta);
i=i+1;
}
opcion=2;
//try{
// Thread.sleep(2000); }catch(InterruptedException ie){ }
if (op==3)
{
info.setText("Velocidad: ");info.setVisible(true);
velocidad.setVisible(true);
ok2.setVisible(true);
}
else
if (op==2)
{torre1.setVisible(true);torre2.setVisible(true);t orre3.setVisible(true);}
}
else
{
}
repaint();
}
}

}

I thought English was the medium here!! Post your code within code tags

soryy is the first time I just this kind of stuff, well Im making the game of Hanoi and one thing I need is that the game solv itself, so I did it but I dont know if my paintComponent or were is my problem cuz I cant see like the process and neither if I use sleep or wait so I want to know how to solv it

You mean, you want to see the rectangles move? and all other stuff's are working properly?

May i know what exactly you are attempting here?

well all the stuff works good the only thing is that I want to see how it moves and I cant see the problem, in the part that the user can move his stuff the programa works good and the rectangles move well but I dont know why if in a part works good why in this one dont

Shouldn't repaint() method be called after each step of the recursion?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.