Ver Mensaje Individual
  #1 (permalink)  
Antiguo 04/05/2011, 22:34
daneco1720
 
Fecha de Ingreso: noviembre-2010
Mensajes: 105
Antigüedad: 13 años, 6 meses
Puntos: 0
Exclamación mover una imagen en un tablero de ajedrez

Hola estoy creando un ajedrez y solo quiero simular el movimiento de una torre
ya cree el tablero y puedo validar los movimientos posibles pero no se como mover el icono a una de las posiciones disponibles si alguien me pudiera ayudar estoy confundido...necesito ayuda urg¡¡
aqui pego todo el programa espero me puedan ayudar
Código:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.ImageIcon;
//<applet code="AppletInterfaz7" width="800" height="800"></applet>
public class AppletInterfaz7 extends Applet implements ActionListener{

    // Creación de los botones
JButton boton[] = new JButton[64];
Panel p1, p2;
TextArea t=new TextArea( );

    public AppletInterfaz7() {
    
      p1=new Panel(new GridLayout (8, 64, 1, 1));  // 3 filas y 3 columnas
      p2 = new Panel(new BorderLayout());
      //setLayout(new FlowLayout());
      for(int i=0;i<64;i++)
{
   boton[i] = new JButton(" ");
}
      for (int i=0;i<64;i++){
    
 p1.add(boton[i]);
 }
  p2.add(t, BorderLayout.SOUTH);
 

      for (int i=0;i<64;i++){

 boton[i].addActionListener(this);
 }
  add(p1);
  add(p2);
      // TABLERO
        
         boton[1-1].setBackground(Color.white);
         boton[2-1].setBackground(Color.black);
         boton[3-1].setBackground(Color.white);
         boton[4-1].setBackground(Color.black);
         boton[5-1].setBackground(Color.white);
         boton[6-1].setBackground(Color.black);
         boton[7-1].setBackground(Color.white);
         boton[8-1].setBackground(Color.black);
         boton[9-1].setBackground(Color.black);
         boton[10-1].setBackground(Color.white);
         boton[11-1].setBackground(Color.black);
         boton[12-1].setBackground(Color.white);
         boton[13-1].setBackground(Color.black);
         boton[14-1].setBackground(Color.white);
         boton[15-1].setBackground(Color.black);
         boton[16-1].setBackground(Color.white);
         boton[17-1].setBackground(Color.white);
         boton[18-1].setBackground(Color.black);
         boton[19-1].setBackground(Color.white);
         boton[20-1].setBackground(Color.black);
         boton[21-1].setBackground(Color.white);
         boton[22-1].setBackground(Color.black);
         boton[23-1].setBackground(Color.white);
         boton[24-1].setBackground(Color.black);
         boton[25-1].setBackground(Color.black);
         boton[26-1].setBackground(Color.white);
         boton[27-1].setBackground(Color.black);
         boton[28-1].setBackground(Color.white);
         boton[29-1].setBackground(Color.black);
         boton[30-1].setBackground(Color.white);
         boton[31-1].setBackground(Color.black);
         boton[32-1].setBackground(Color.white);
         boton[33-1].setBackground(Color.white);
         boton[34-1].setBackground(Color.black);
         boton[35-1].setBackground(Color.white);
         boton[36-1].setBackground(Color.black);
         boton[37-1].setBackground(Color.white);
         boton[38-1].setBackground(Color.black);
         boton[39-1].setBackground(Color.white);
         boton[40-1].setBackground(Color.black);
         boton[41-1].setBackground(Color.black);
         boton[42-1].setBackground(Color.white);
         boton[43-1].setBackground(Color.black);
         boton[44-1].setBackground(Color.white);
         boton[45-1].setBackground(Color.black);
         boton[46-1].setBackground(Color.white);
         boton[47-1].setBackground(Color.black);
         boton[48-1].setBackground(Color.white);
         boton[49-1].setBackground(Color.white);
         boton[50-1].setBackground(Color.black);
         boton[51-1].setBackground(Color.white);
         boton[52-1].setBackground(Color.black);
         boton[53-1].setBackground(Color.white);
         boton[54-1].setBackground(Color.black);
         boton[55-1].setBackground(Color.white);
         boton[56-1].setBackground(Color.black);
         boton[57-1].setBackground(Color.black);
         boton[58-1].setBackground(Color.white);
         boton[59-1].setBackground(Color.black);
         boton[60-1].setBackground(Color.white);
         boton[61-1].setBackground(Color.black);
         boton[62-1].setBackground(Color.white);
         boton[63-1].setBackground(Color.black);
         boton[64-1].setBackground(Color.white);

       boton[10].setIcon(new ImageIcon("src/brook.gif"));

    }
  
    public void actionPerformed(ActionEvent m) {

        int i=0,j=0,k=0,y=0,z=0,x=0,pos[]=new int[64];
        // crear los movimientos validos para la torre
       if (m.getSource() == boton[10]) {
        while(j<64){
        if (m.getSource() == boton[j]) {

         k=j;
         y=j;
         z=j;
         x=j;

          boton[j].setForeground(Color.blue);

         if(j<64){
         while(k+8<64){
         
          boton[k+8].setBackground(Color.gray);
          k=k+8;
          pos[i]=k;
          i++;
             }

         
     while(y <64){
         boton[y].setBackground(Color.gray);
         pos[i]=y;
          y=y+1;
          i++;
         if(y==8||y==16 || y==24||y ==32 ||y ==40||y ==48||y ==56||y==64){y=64;}
             }
         while(x-8>=0 && x<64){
          boton[x-8].setBackground(Color.gray);
            x=x-8;
           pos[i]=x;        
         i++;
             }
        
         while(z>=0 && z<64){
          boton[z].setBackground(Color.gray);
          pos[i]=z;
          if(z==0||z==8 || z==16|| z==24||z ==32 ||z ==40||z ==48||z ==56||z==64){z=-1;}
          
          z=z-1;
          i++;
             }

            }//cierra if
          
          }//if de movimiento
        
        j++;
       
        }// cierra while hemos logrado los movimientos
      
       int p=0;
        while(p<64){
           
        if(m.getSource()==boton[p]){
            i=0;
            while (i<64){
            System.out.println(" entra hasta aqui el if del while");
            if(i==pos[p]){
            boton[p].setIcon(new ImageIcon("src/brook.gif"));
            
            i=65;
            System.out.println(" entra hasta aqui"+p);
            }
            
                i++;
            
        }


           }// if segunda accion la verdad no esta funcionando
            p++;
           }//while
        // restablecer();
      }//fin primer if de eventos

        
        
      }//actionperformed

   
   }


 /* for(i=0;i<64;i++){

       // t.setText( "color"+ boton[i].getBackground());
        System.out.println(" posicion "+ pos[i]);
        }*/