go0d day every0ne..

..Can you please help me create a 'Picture Puzzle' using GUI..
its just like playing a picture puzzle like we d0,
it just like this..
it has a pieces of scrambled pictures that you will arrange it.. it has a 0ne empty block to be able for you to move the puzzle..

..thankz.
..God Bless you.
^_^

Dani AI

Generated

Short plan based on the thread: split the source image into tiles, keep a separate board model that records tile IDs (0 = blank), implement move logic that uses grid coordinates, make sure shuffled boards are solvable, and keep long work off the UI thread. gave the right high-level direction; below are concrete, small pieces missing from earlier replies and a couple of checks to avoid common bugs.

Model + adjacency check (map 1D index to row/col and use Manhattan distance):

private boolean canMove(int fromIndex, int blankIndex, int cols) {
  int fr = fromIndex / cols;
  int fc = fromIndex % cols;
  int br = blankIndex / cols;
  int bc = blankIndex % cols;
  return Math.abs(fr - br) + Math.abs(fc - bc) == 1;
}

Solvability (count inversions). For odd grid widths, inversions must be even. For even widths, solvability depends on the blank row from the bottom — see the 15‑puzzle rules for details.

private boolean isSolvable(int[] tiles, int cols) {
  int inv = 0;
  for (int i=0;i<tiles.length;i++)
    for (int j=i+1;j<tiles.length;j++)
      if (tiles[i]!=0 && tiles[j]!=0 && tiles[i]>tiles[j]) inv++;
  if (cols % 2 == 1) return inv % 2 == 0;
  int blankRowFromBottom = cols - (indexOf(tiles,0) / cols);
  return (blankRowFromBottom % 2 == 0) ? (inv % 2 == 1) : (inv % 2 == 0);
}

Alternative: start from the solved position and perform N random legal moves to shuffle; that always produces a solvable position and is simpler for beginners. Read the math behind parity on the 15 puzzle for more context: 15 puzzle and this practical check: GeeksforGeeks solvable check.

Image slicing (BufferedImage.getSubimage) and UI tips: slice by tile width/height, scale the source first if dimensions are not divisible, keep the model separate from the view, do image loading/slicing on a background thread (SwingWorker) and update the UI on the EDT. For debugging, print the tile array after shuffling and test canMove + isSolvable independently before wiring the click/drag handlers. This approach keeps problems small and easy to fix.

Recommended Answers

All 6 Replies

Well, what do you have so far and what, exactly, are you having problems with.

..hard f0r me to do that..
/n0obz me.

/can u please give me some idea?..

That is the same question you asked in one of your previous threads:
http://www.daniweb.com/forums/thread305019.html

You got answers to that thread. If you didn't like the answers don't start a new thread.

In that thread we asked what a "picture puzzle" was and got no answers. Instead you created a new thread with the answer of our question. It is like you ignored our attempts to help you.

As for an idea.
Use JLabels or JButtons that display different images and have one button with a blank image. Once a button is clicked, check if it has an empty space next to it (the button with no image). If yes, swap the buttons images.

Check the JButton class API to see how to add images to a button.
Also read some tutorials about swing, on how to write ActionListeners.

sorry about the previous one//

//hope you don't mind me asking.. if where can i read some tutorial in that case?..

..thanks much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.