Use java to realize a simple jigsaw puzzle for your reference. The specific contents are as follows
First, let’s take a look at the renderings of this jigsaw puzzle:
Create a class named medleygame, which inherits the JFrame class; Then declare a panel object and a button object respectively in this class. The panel object is used to add puzzle buttons. The button object is the button that currently displays a blank picture; Finally, write a main () method and a construction method medleygame () for this class, and set the relevant properties of the form in the construction method, such as the title, display position and size of the form.
The Java project structure is as follows:
The specific java code is as follows:
package pac;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
public class MedleyGame extends JFrame{
private JPanel centerPanel;// Puzzle button panel
private JButton emptyButton;// Object blank button
public static void main(String[] args) {
try {
MedleyGame frame = new MedleyGame();// Create objects of this class
frame. setVisible(true);// Make the form visible
}catch(Exception e) {
e.printStackTrace();
}
}
public MedleyGame() {
super();// Inherit the construction method of JFrame class
setResizable(false);// The form size cannot be changed
Settitle ("jigsaw puzzle")// Sets the title of the form
setBounds(100, 100, 354, 435);// Display size and position of form
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// Set to exit the program when the form is closed
final JPanel topPanel = new JPanel();// Create panel objects
topPanel.setBorder(new TitledBorder(null, "", TitledBorder.DEFAULT_JUSTIFICATION,
TitledBorder. DEFAULT_ POSITION, null, null));// Add borders to panels
topPanel. setLayout(new BorderLayout());// The setting panel adopts boundary layout
getContentPane(). add(topPanel,BorderLayout.NORTH);// Add panel to top of form
final JLabel modelLabel = new JLabel();// Create a label object that displays a reference picture
modelLabel. setIcon(new ImageIcon("img/model.jpg"));// Set the reference picture displayed by the label
topPanel. add(modelLabel, BorderLayout.WEST);// Add a label to the left side of the panel
final JButton startButton = new JButton();// Create next game button object
startButton. Settext ("next game")// Sets the label text for the button
startButton. addActionListener(new StartButtonAction());// Add listener for button
topPanel. add(startButton, BorderLayout.CENTER);// Add a button to the middle of the panel
centerPanel = new JPanel();// Create a jigsaw button panel object
centerPanel.setBorder(new TitledBorder(null, "",
TitledBorder.DEFAULT_JUSTIFICATION,
TitledBorder. DEFAULT_ POSITION, null, null));// Add borders to panels
centerPanel. setLayout(new GridLayout(0, 3));// Set the puzzle button panel to adopt a grid layout of 3 columns
getContentPane(). add(centerPanel, BorderLayout.CENTER);// Add a panel to the middle of the form
String[][] stochasticOrder = reorder();// Get the random placement order of grid pictures
For (int row = 0; row < 3; row + +) {// routine
// COL < 0; col + + column (example)
final JButton button = new JButton();// Create puzzle button object
button. setName(row + "" + col);// Set the name of the button
button. setIcon(new ImageIcon(stochasticOrder[row][col])); // Set picture for puzzle button
If (stochasticorder [row] [col]. Equals ("img / 22. JPG") // judge whether it is a blank button
emptyButton = button;
button. addActionListener(new ImgButtonAction()); // Add a listener to the puzzle button
centerPanel. add(button);// Add a button to the jigsaw puzzle button panel
}
}
//
}
Private string [] [] reorder() {// used to obtain the random placement order of grid pictures
String[][] exactnessOrder = new String[3][3];// Correct placement order of grid pictures
For (int row = 0; row < 3; row + +) {// routine
// COL < 0; col + + column (example)
exactnessOrder[row][col] = "img/" + row + col + ".jpg";
}
}
String[][] stochasticOrder = new String[3][3];// Random placement order of grid pictures
For (int row = 0; row < 3; row + +) {// routine
// COL < 0; col + + column (example)
While (stochasticorder [row] [col] = = null) {// the specified grid of random placement order is empty
int r = (int) (Math.random() * 3);// Take random row
int c = (int) (Math.random() * 3);// Take random column
If (exactnessorder [R] [C]! = null) {// the specified grid in the correct placement order is not empty
//Place the pictures in the specified grid in the correct placement order into the specified grid in the random placement order
stochasticOrder[row][col] = exactnessOrder[r][c];
//Set the specified grid in the correct order to empty
exactnessOrder[r][c] = null;
}
}
}
}
return stochasticOrder;
}
Class imgbuttonaction implements actionlistener {// puzzle button listener
public void actionPerformed(ActionEvent e) {
String emptyName = emptyButton. getName();// Gets the name of the blank button
char emptyRow = emptyName. charAt(0);// Get the row where the blank button is located
char emptyCol = emptyName. charAt(1);// Get the column where the blank button is located
JButton clickButton = (JButton) e.getSource();// Get the clicked button object
String clickName = clickButton. getName();// Get the name of the clicked button
char clickRow = clickName. charAt(0);// Get the row of the clicked button
char clickCol = clickName. charAt(1);// Get the column where the clicked button is located
//Judge whether the clicked button is adjacent to the blank button
if (Math.abs(clickRow - emptyRow) + Math.abs(clickCol - emptyCol) == 1) {
//Move the picture of the clicked button to the blank button
emptyButton.setIcon(clickButton.getIcon());
//Set the clicked button to display a blank picture
clickButton.setIcon(new ImageIcon("img/22.jpg"));
emptyButton = clickButton;// Set the clicked button as a blank button
}
}
}
Class startbuttonaction implements actionlistener {// next game button listener
public void actionPerformed(ActionEvent e) {
String[][] stochasticOrder = reorder();// Get the random placement order of grid pictures
int i = 0;// The index of the puzzle button in the puzzle button panel
For (int row = 0; row < 3; row + +) {// routine
// COL < 0; col + + column (example)
JButton button = (JButton) centerPanel. getComponent(i++); // Gets the puzzle button at the specified index
button. setIcon(new ImageIcon(stochasticOrder[row][col])); // Set picture for puzzle button
If (stochasticorder [row] [col]. Equals ("img / 22. JPG") // judge whether it is a blank button
emptyButton = button;
}
}
}
}
}
Here, the code program is finished. Let’s run it:
Follow the steps to save and then run:
The result is no problem. Click “next game” to refresh the game and arrange the pictures randomly:
So far, a simple puzzle game has been realized.
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support developpaer.