0

Now I need to write a 8-puzzle game, which looks [like this]

enter image description here

The instructor asked us to write three different classes, which are Piece.java, EightPuzzle.java, and EightPuzzlePanel.java. As you can see, Piece.java represents each individual piece like "1", "2" in this eight puzzle board; EightPuzzle.java represents the the game board to hold these 9 pieces/buttons. EightPuzzlePanel.java is the GUI stuff.

So my question is, since we need to create a Piece[][] piece = new Piece[][], a 2D array, and we also need to arrange these piece on the board. I thought I could create 9 JButtons and put the 2D array in link with the 9 JButtons (or there have a better way to sort the 2D-array), but I don't know how to do that.

Also the buttons need to be controlled by both mouse and keyboard. This is another challenge for me.

JavaLeave
  • 225
  • 1
  • 5
  • 14
  • 1
    Start with a look at [A Visual Guide to Layout Managers](http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) and then have a look at [Using Layout Managers](http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html). A little hint. For the actually puzzle board, I'd probably use a `GridLayout`. – MadProgrammer Oct 23 '12 at 06:08
  • *"So my question is,.."* ..what exactly? ('Challenges' are not 'questions'.) – Andrew Thompson Oct 23 '12 at 06:09
  • i already set for grid layout, but my question is how to link the 2d array with my JButton, but not directly set the 2D JButton. The challenge is 2nd part of question, i want to solve the 2d-array first. thank you – JavaLeave Oct 23 '12 at 06:12

2 Answers2

2

Since this is homework I will not go into much detail, but this is how I would go about it:

  1. Make Piece extends the JButton class. The Piece object takes the text to display and also the location of the image you would like it to render. You should be able to find plenty of examples online on how to add an image to a JButton.
  2. Make EightPuzzle extend the JPanel class and also use the Grid Layout to render the Pieces neatly in a grid. This class takes on a 2D array of Piece objects which it will then render.
  3. Make EightPuzzlePanel also extend the JPanel class. This class takes in another JPanel (EightPuzzle) and appends any other buttons you might need.
  4. Finally create a Main class which extends JFrame and then I append the EightPuzzlePanel to it (which should in turn contain the other panel with the other buttons).
npinti
  • 51,780
  • 5
  • 72
  • 96
  • Thank you. This make so much sense. I'll start follow your instruction! – JavaLeave Oct 23 '12 at 06:22
  • @KellyAnn: No worries. As a tip though (which has already been recommended) I would ditch the 2D Array and use a Collection. Displaying the buttons as a grid is something which will be taken care by the layout manager. Since you seem you need some shuffle functionality the collection will come handy later. Also, for event listeners please take a look [here](http://docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html) for mouse listeners and [here](http://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html) for key listeners. – npinti Oct 23 '12 at 06:31
  • hi, i have question about piece class. As you said, i need to extends JButton class for Piece, so I need implements ActionListener, too? I thought the EightPuzzlePanel include all GUI stuff, but other two class just as inner function. I am not really understand – JavaLeave Oct 23 '12 at 18:22
  • @KellyAnn: You can either do as you say (implement ActionListener) or else, you can work with nested classes, as shown [here](http://stackoverflow.com/questions/5451010/nested-class-vs-implements-actionlistener). The `EighPuzzlePanel` will include (1)the `EightPuzzle` (which contains the grid of buttons) and (2) any other buttons (such as `Scramble` and `Reset`). – npinti Oct 24 '12 at 04:58
0

For the mouse and key, you'll need to set up some action listeners.

For this problem you can just use a 1-D array. As long as you have the 9 pieces stored in you array you can use you layout manager to put them in the right place- then traversing through the array is simple.

SyntaxRules
  • 2,056
  • 23
  • 32
  • Thank you, I did set up the mouse, key and action listener. But this hw is require for 2-d array. For mouse and key listener, would you mind give me some tips about algorithm ? I really have no idea how to move the button by mouse and keys. – JavaLeave Oct 23 '12 at 06:22