Thanks for taking the time to read. =)
The problem I am having is mainly Object visibility. I'm creating objects and attempting to create references to them before they have been initialized or have any data in them. You can get an idea what I'm trying to do here with the code provided.
Certain Squares can see other square ect. Now my code does compile! The issue is I have getter-methods that return null from these objects. I tried placing all the initializations in the Square-class declaration however that is where I get "Illegal Forward reference".
So finally, at the end of the code block you notice how I attempt to access these objects and their references. All four return null, what Im going for is some indication a reference exists.
I do understand slightly what is going wrong here, perhaps my approach with the objects is incorrect? I think my amateur skills are mainly what hold me back on this one =)
class Puzzle extends JFrame
{
Square square1;
Square square2;
Square square3;
Square square4;
Square square5;
Square square6;
Square square7;
Square square8;
Square square9;
Square square10;
Square square11;
Square square12;
Square square13;
Square square14;
Square square15;
Square square16;
public Puzzle()
{
PuzzleListener plist = new PuzzleListener();
JPanel puzzle_board = new JPanel(new GridLayout(4,4,5,5));
//randomize the text index's and the starting black square.
// Square(north, south, east,west,isblack,text)
square1 = new Square(null,square2,square5,null,false,"1");
square2 = new Square(null,square3,square6,square1,false,"2");
square3 = new Square(null,square4,square7,square2,false,"3");
square4 = new Square(null,null,square8,square3,false,"4");
square5 = new Square(square1,square6,square9,null,false,"5");
square6 = new Square(square2,square7,square10,square5,false,"6");
square7 = new Square(square3,square8,square11,square6,false,"7");
square8 = new Square(square4,null,square12,square7,false,"8");
square9 = new Square(square5,square10,square13,null,false,"9");
square10 = new Square(square6,square11,square14,square9,false,"10");
square11 = new Square(square7,square12,square15,square10,false,"11");
square12 = new Square(square8,null,square16,square11,false,"12");
square13= new Square(square9,square14,null,null,false,"13");
square14 = new Square(square10,square15,null,square10,false,"14");
square15 = new Square(square11,square16,null,square14,false,"15");
square16 = new Square(square12,null,null,square15,false,"16");
}
class Square extends JPanel
{
//visible lable
JLabel lblText;
//internal object ID
int id;
//Define the visible neighbors
Square north;
Square south;
Square east;
Square west;
//is the square black
boolean isBlack;
public Square(String text)
{
lblText = new JLabel(text);
//setLayout(new GridLayout(5,5));
lblText.setLocation(10,10000);
add(lblText);
}
public Square(Square n,Square e,Square s,Square w,boolean black, String text)
{
north = n;
east = e;
south = s;
west = w;
isBlack = black;
lblText = new JLabel(text);
//setLayout(new GridLayout(5,5));
lblText.setLocation(10,10000);
add(lblText);
}//end square
Square getNorthNeighbor()
{
return north;
}
Square getSouthNeighbor()
{
return south;
}
Square getEastNeighbor()
{
return east;
}
Square getWestNeighbor()
{
return west;
}
boolean getIsBlack()
{
return isBlack;
}
}
System.out.println("Panel 1 has been clicked and has the neighbors " + "North: " + square1.getNorthNeighbor() + "East: " + square1.getEastNeighbor() + "South: " + square1.getSouthNeighbor() + "West: " + square1.getWestNeighbor());