I have a gridlayout on a JPanel and add JButtons each with an icon and together they form a picture when in proper order.
I then shuffle the array of images (its actually single image cropped using java code into pieces and stored in the array) and then set each JButton icon to a new image. Now this gives us a nice “puzzle” type of picture where the end-user can swap tiles to put it back in order.
My only problem, is I have no idea how to check to see if they have 1) put an image in its correct location (button) and b) if all images are in the proper location (buttons), to form the original picture.
I do store the buttons in an array, and each image in a separate array. Only the images get sorted, and only the image icon get set when user swaps two different tiles.
My thought is that I need to compare button[0].image == image[0], etc. However, I dont know how to do that.
Perhaps my approach is wrong and there are other properties or venues to persue?
If I were implementing this, I would keep an unshuffled array (or list) of images along with an array (or list) of indices into that array. Initialize the second array/list with
index[i] = i. Then shuffle the array/list of indices. Setting the JButton icons takes an extra level of indirection:button[i].setIcon(image[index[i]]). But it makes checking that an image is in the correct location much easier:if (index[i] == i).