Hi I am trying to learn javaapplets.. trying to create a square puzzle in an applet. Here is the code I have written so far..
Essentially, I have created a grid layout of 4 / 4. Added 15 buttons and a blank label into this grid. On clicking a button, check if the button has blankLabel as one of the neighbors - if so, swap them.
This works to the extent that the grid is displayed, on clicking, it identifies the correct button and tries to swap them correctly. The elements in the Numbers array are always correct. But, the display is never updated. It continues to show the initial layout.
Anything I am missing out here? Can someone please help me with this?
Why are you using only AWT components, rather than Swing? Why are you using break; in that loop? Will that mean you only call drawButtons() once, before you have iterated the collection of buttons? Why don’t you call drawButtons() after the end of the loop?
I have not checked your algorithm. You ought to write it on a sheet of paper (or a word processor = WP) in words, if you have any difficulty with it. If you use a WP, you can post it here and ask for help.
First, as pointed out by Campbell Ritchie, here is the algorithm (I thought it was too trivial so did not want to waste space on it)
This is what the code does:
1. On Init, populate the ArrayList<Component> Numbers with one blank label and 15 buttons representing the 15 digits. The ArrayList is shuffled and then displayed in the GridLayout 2. On any button press.. check if that button has the blankLabel as a neighbor. If it is, just swap the position.
I found a solution to this problem.. posting it here to complete the thread..
The problem is that the components remembered their location. That is, even after call to
The location of the components did not reset. They were added in a different order every time, but since they remembered their location, they got placed in the same old place.
When I explicitly reset their location using setLocation(), the problem got solved.