-2

i was been trying to do a chess game and got stuck on swaping two piecess

this is my ChessSquare class

   public class ChessSquare extends JButton 



{ 




public int Yposition;
public int Xposition;
private String Type;
public ChessSquare( Icon s, int y , int x , String piece )

{
 super(s);

 Yposition = y;
 Xposition = x;
 Type = piece ;
}         

public int getXposition()
 {
   return this.Xposition;
 }  


    public int getYposition()
 {
   return this.Xposition;
 }  


    public String getType()
   {
      return "";
   }

 }  

then i add this to the ChessBoard class, using the ChessSquare class as 2D array

     public class ChessBoard extends JFrame implements ActionListener
  {
     String type;
     ChessSquare[][] s = new ChessSquare[8][8];
     int xPos1,xPos2,yPos1,yPos2,i,j;
     JPanel panel;


    ........


    public void actionPerformed(ActionEvent e)
  {
       Boolean flag = false ;

         if(!flag)
  {
     xPos1 = s.getYposition();
     yPos1 = s.getXposition();

     flag = true;
   }
     else
   {

    xPos2 = s.getYposition();
    yPos2 = s.getXposition();


    s[xPos1][yPos1] = s[xPos2][yPos2];
    s[xPos2][yPos2] = s[xPos1][yPos1];
    flag = false;


 }


 }

 } 

trying to swap two pieces...but its not working? please help... error

madth3
  • 7,275
  • 12
  • 50
  • 74
jdg
  • 11
  • 1
  • 1
  • 3

1 Answers1

0

The methods getXposition and getYposition are instance methods of your custom ChessSquare class, rather than your ChessSquare array. To access the methods you would have to reference the elements in your 2D array, e.g.:

xPos1 = s[0][0].getYposition();
yPos1 = s[0][0].getXposition();
Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • why complicating the simple things, don't do that, not never as an answer :-), its about prepare all things that you needed before, intialize on ...., [see here](http://stackoverflow.com/a/10385302/714968) and [here (and see EventHandler == my love)](http://stackoverflow.com/a/9007348/714968) , and used LayoutManager can returns coordinates too (in most cases JComponent can be swapable in this array, but (the same) there no reason do it that in most cases == wrong concept) – mKorbel Mar 19 '13 at 08:06