In your picked(int x, int y) method of your "Box" class, you are referencing "boxes", but do not have a variable "boxes" defined anywhere. Your "GuessGame" class does have an instance variable "boxes", but that is not in anyway in scope for the outer class "Boxes".
Did you mean for "Box" to be an inner class?
posted 13 years ago
I don't want Box to be inner class, so how can call on boxes[i][j] and set its showColor variables from the picked() method? thanks
You should not have to reference your boxes variable from the box class. A key to good Object Oriented Design is that objects are responsible for themselves. In your game, a Box does not need to know about the game board it is being used in. That is a concern for the GuessGame to worry about. By not coupling the Box class to the game board (i.e. the two dimensional array), you are free to use the the Box class not just a square like the current Game, but in other ways, such as in a line , or a cube (can you say Rubic's Cube Game ). What this means is your Box class does not need to know (i.e. store in variables) its X and Y position in the game board. Doing so would prevent you from using the Box in a cube (where there is an X, Y & Z position), or whatever board layout we might want. So I would pull those instance variables out of the Box class. Again, let the GuessGame class keep track of the game board.
Now with that in mind, we want the GuessGame Class to finds the particular box clicked on, get it from the array, and then call a method on that particular box reference to tell that particular box "you've been picked". So in the mouseReleased(MouseEvent) method of the GuessGame, you have code to determine the X and Y position of the box that was clicked on. So use that to reference the box that was clicked on from your array. In the mouseReleased(MouseEvent) method, after you determine the row and column subscripts, do the following:
Then change your picked method to simply change the state of that box:
I did not look in detail at the algorithm you are using to determine the row and col subscript. Just remember that arrays are zero based. Does your algorithm take that into consideration?
Hold that thought. Tiny ad:
Devious Experiments for a Truly Passive Greenhouse!