Need help calling from a different method
So basically I'm trying to call the squares array from the method called Board and I want to use it in a method called pairInput. Both methods are within the same class called Board. Here the codes for both methods.
Code:
public Board()
{
int initialXValue = 25; // starting left-hand x value
int x = initialXValue; // x position
int xIncrement = 70; // increment to x for each column
int initialYValue = 50; // starting left-hand x value
int y = initialYValue; // y position
int yIncrement = 70; // increment to y for each row
// variables used for drawing
Square gameTitle = new Square( 125, 20, 10, "white", true, "9 Tiles Puzzle");
Square background = new Square( x-OFFSET, y-OFFSET, (SQUARE_SIZE+OFFSET)*(NUMBER_OF_ROWS) + OFFSET, "black", true, "");
int[] presetArray = {3,2,7,5,8,4,6,1};
Square squares[] = new Square[ 8];
// Create 9 boxes at the correct spacing intervals for the background
for(int i=0; i<8; i++){
int xtemp;
int ytemp;
if(i < 3){
xtemp = i*xIncrement + initialXValue;
ytemp = initialYValue;
}
else if(i < 6){
xtemp = (i-3)*xIncrement + initialXValue;
ytemp = initialYValue + yIncrement*1;
}
else{
xtemp = (i-6)*xIncrement + initialXValue;
ytemp = initialYValue + yIncrement*2;
}
squares[ i] = new Square(xtemp, ytemp, SQUARE_SIZE, "lightGray", true);
}
// Assign labels in a specific order to the square object array we just created
for(int i=0; i<8; i++){
squares[ i].setLabel(Integer.toString(presetArray[ i]));
}
} //end constructor Board()
Code:
public void pairInput( int p)
{
Square s1 = squares[ 0].getLabel();
Square s2 = squares[ 1].getLabel();
Square s3 = squares[ 2].getLabel();
Square s4 = squares[ 3].getLabel();
Square s5 = squares[ 4].getLabel();
Square s6 = squares[ 5].getLabel();
Square s7 = squares[ 6].getLabel();
Square s8 = squares[ 7].getLabel();
if( p == s1) {
p = s1;
px = p.getX();
py = p.getY();
}
else if( p == s2) {
p = s2;
}
else if( p == s3) {
p = s3;
}
else if( p == s4) {
p = s4;
}
else if( p == s5) {
p = s5;
}
else if( p == s6) {
p = s6;
}
else if( p == s7) {
p = s7;
}
else if( p == s8) {
p = s8;
}
}
Also if you see the part where it says px=p.getX(); and the same for y, I am trying to call a method called getX() from a class called Square. Not sure if I wrote that correctly or not either...
Any help would be greatly appreciated.
-Mayur