Now I need to write a 8-puzzle game, which looks [like this]
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.
Since this is homework I will not go into much detail, but this is how I would go about it:
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.
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.
Make EightPuzzlePanel also extend the JPanel class. This class takes in another JPanel (EightPuzzle) and appends any other buttons you might need.
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).
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.
Related
I am writing a chess program, and I am at the stage of implementing the game loop.
To implement player Turn in chess, I want to get some advice on how to do it efficiently.
So, this is how my chess with GUI works so far:
I generate a class called BoardGUI(JPanel) with a size of 8x8 and place a class called TileGUI(JPanel) on each grid.
I also added a MouseListener on TileGUI and wrote all functions regarding the Piece's movement on the board.
All tiles that have a Piece also contains the information of the Piece (color, type).
My initial thought of implementing the "Turn" is to create an integer called turn, initialized to 0, and make the Black Piece movable when the turn integer is an odd number and make the White Piece movable when the turn integer is an even number.
This is the best idea, or will there be a more clever way to do this?
I'm trying to make a building game (~Age Of Empire :). So, I want to write a program that divides a JFrame (containing the map of my game) to squares. Then allows every square of the frame to be modified (to contain an image). For example, I want to put an image in square (1,1), then add an image in square (4,2) and keep the image that I had in square (1,1).
How can this be done?
To do this inside the JFrame just use a GridLayout, create a control to match a square on the map (for now you could just use a JLabel or JButton) and add them to the screen.
I can't really recommend using Swing for developing a game though, you'd be better off looking into one of the many 2D (or 3D) for that matter game engines out there. They will allow you to get much more impressive results and do some of the work for you.
I'm extremely new JavaFX (and any GUI for that matter), and I'm having troubles with understanding some concepts. That said, I have 10x10 multi-dimensional array that stores random int's within a class that extends GridPane, and I'm having troubles understanding how I would display each random int value graphically in the center of a BorderPane of another class. I would like each int of every element to be shown in a shape of sorts uniformly. I don't want to know an exact solution. I just want to be aware of what options and steps I should take e.g. imports I should be aware of etc. Found this image from a different post, but this the kind of layout I imagine, except I don't need them to be buttons
Build a (probably nested) loop and traverse the 2d-array. On each cycle get the int value from the class and put into one of appropriate built-in layout panes of JavaFX. I suggest to use another general/master GridPane or TilePane. Then put this master pane to center of BorderPane.
I am creating a chess game, and I have now populated my graphic chessboard with all the pieces, now I need to use Mouselistner to move the pieces around. Before implementing the graphic version I created a 2D console version, that took in "player moves", so I do have all those methods, but I need to now use Mouselistener, I read up about the methods, however, do I need to implement mouselistener in each class?
I have 1 abstract Piece class as well as 7 subclasses (incl Dummy piece), and a ChessBoard class that populates all the pieces and provides methods for moving (from the console version..) so where do I put the mouselistener? In the Jcomponent extension, JFrame or ChessBoard class that contains the methods to populate the chessboard and moves?
Sorry for such a simple answer, but all you need to do is add the mouselistener to your ChessBoard class. From there I'm assuming you can access the Piece subclass objects you've instantiated and call methods on them (i.e. mouseClicked, piece.pickUp()). If you code is arranged in such a way that you need to implement a mouse listener in many of your classes, consider the following:
addMouseListener( new MouseAdapter() {
#Override
public void mouseClicked( MouseEvent e ) {
// Do something
}
} );
http://docs.oracle.com/javase/7/docs/api/java/awt/event/MouseAdapter.html
Also, if it were me, I would transfer the methods for moving your Pieces to your Piece class, preferably at a higher level and then you don't have to rewrite the same code twice. Then in your game, whenever mouseReleased is invoked, call some method like attemptToMove(BoardPoint p) that will check if your piece's current position and the new position, p are within your piece's means of travel. A BoardPoint could be something you set up with x, y coordinates for your own board in an 8 X 8 style, like a 2-dimensional integer array.
It depends somewhat on how you have your pieces implemented. If they are GUI objects themselves, such as buttons or panels, then putting the mouseListener on them will allow the Swing framework to figure out which one has been clicked on. If the pieces all extend a Piece class, then you can put a handler in that as long as the logic it needs to execute (such as moving a piece around) can be made the same for all pieces.
If, on the other hand, you are drawing graphic images on the board in your code, so there is no GUI component for Swing to detect being clicked, then it makes more sense to implement the mouseListener on the board. In this case, your code is going to have to figure out which square was clicked on, and whether it has a piece on it; after that the handling will be much like the previous case.
I've been looking around for a way to do this. I found some code to change the order of strings in a list, but i want to add buttons and such on each panel so that wouldn't work...
You may want to take a look at the docking frameworks. For example, Docking Frames has a good example of a chess board - basically, you can "dock" pieces on the chess board squares which server as the drop targets. Just launch the demonstration via the provided jnlp and then pick the Chess demonstration in the list on the left to see how it works. Here's a screenshoot of it:
You can see that each of the squares is a docking tile (i.e. a draggable object).
In your case, instead of 8x8 of squares, you can have Nx1 to simulate a one-column list instead of a table.
Use a JList. Look into the dragging behavior for the reordering ability.
See How to Use Lists for more details.