Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the jigsaw puzzle in java

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to achieve the jigsaw puzzle in java, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

Directly on the effect picture:

1. Required technology

Java Foundation

GUI related Technology of java

two。 The concrete realization of 2.1 picture production

Choose a picture you like, (tip: try to keep the picture in the range of 400 to 600), and then split the picture into 12 equal-sized pieces through online segmentation, or other software such as your ps. As shown in the following figure

We recommend that you name the small picture according to the file name-serial number of the big picture, which is convenient for the program to read.

2.2 create a project

Create a java project using idea or Eclipse. Editor uses idea, and then under the project, create an image directory, and copy the images from the previous step to the image directory under the project:

2.3 coding implementation

Let's first create an entity class that dares to describe the pictures displayed in the game: the Picture class, with the following code

Public class Picture {private String alisa;// alias private String picPath;// Preview Picture path private String [] sliced Image path of subPicPaths;// Jigsaw area / / omitted the code of setter/getter construction method}

Here we apply java's OOP idea to abstract a puzzle in the game into a Picture class. The small picture on the left corresponds to the subPicPaths attribute of the Picture class, the preview image on the right corresponds to the picPath attribute of the Picture class, and we give the puzzle a name corresponding to the alisa attribute of the Picture.

We have packaged the Picture class, and then we need to write our game main interface class MainFrame. Our current program is not running on the console, but running an interface window. All our main interface classes must implement the JFrame class under the Swing package. JFrame is a game window.

Public class MainFrame extends JFrame {private String [] items = {"Little Bear", "Beauty"}; private Map picMap = new HashMap (); / / Picture in the game private static final long serialVersionUID =-3974891708686273202L; private JComboBox imgComboBox; / / Select the drop-down box of the picture private PictureCanvasPanle canvasPanle; / / the panel of the puzzle area private PicturePreviewPanel previewPanle; / / the panel private JTextField nameTxt; / / of the preview area shows the text field public static Picture selectedPicture of the picture noun / / selected picture private JButton startBtn; / / start button public static JTextField stepTxt; / / text field showing the number of steps / / add picture public void setMap () {for (int I = 0; I < items.length;i++) {String [] subPaths = new String [12]; for (int j = 0; j < subPaths.length) Jpg +) {subPaths [j] = "image/" + (iTun1) + "-" + (juni1) + ".jpg";} Picture pic = new Picture (items [I], "image/" + (iTun1) + ".jpg", subPaths); picMap.put (pic.getAlisa (), pic) }} / / No parameter construction method, initialize the interface public MainFrame () {setMap (); / / initialize init (); / / add component addComponent (); / / add jigsaw puzzle and preview addPreview (); / / add event addActionLisener () to the component } / / add event imgComboBox.addActionListener (new ActionListener () {@ SuppressWarnings ("unchecked") @ Override public void actionPerformed (ActionEvent e) {JComboBox cbx = (JComboBox) e.getSource (); selectedPicture = picMap.get (cbx.getSelectedItem ()) to the drop-down box PreviewPanle.setPath (selectedPicture.getPicPath ()); nameTxt.setText ("picture name:" + cbx.getSelectedItem ()); canvasPanle.changePictureCell (); previewPanle.repaint ();}}) / / add event listener startBtn.addActionListener to the start button (new ActionListener () {@ Override public void actionPerformed (ActionEvent e) {/ / clear the number of steps moved canvasPanle.stepNum = 0; stepTxt.setText ("steps:" + canvasPanle.stepNum) / / disturb the small squares of the puzzle area canvasPanle.start ();});} / add the preview image private void addPreview () {JPanel mainPanle = new JPanel (); mainPanle.setLayout (new GridLayout (1Magne2)); canvasPanle = new PictureCanvasPanle (); previewPanle = new PicturePreviewPanel () MainPanle.add (canvasPanle,BorderLayout.WEST); mainPanle.add (previewPanle,BorderLayout.EAST); this.add (mainPanle,BorderLayout.CENTER);} private void addComponent () {/ / contents of the upper section JPanel topPanel = new JPanel (); / / set the layout topPanel.setLayout (new GridLayout (1)) / / add left and right panel / / left panel JPanel leftPanel = new JPanel (); / / set background color leftPanel.setBackground (Color.PINK); leftPanel.setBorder (new TitledBorder (Game Settings area)); / / add other components / / set the layout leftPanel.setLayout on the left (new FlowLayout (FlowLayout.CENTER,10,10)) / / tag JLabel infoLabel = new JLabel ("Select picture:"); leftPanel.add (infoLabel); / / set the drop-down box imgComboBox = new JComboBox (items); imgComboBox.setBackground (Color.PINK); leftPanel.add (imgComboBox); / / set the add button startBtn = new JButton ("Start") StartBtn.setBackground (Color.PINK); leftPanel.add (startBtn); / / add topPanel.add (leftPanel, BorderLayout.WEST); / / right panel JPanel rightPanle = new JPanel (); rightPanle.setBackground (Color.PINK); rightPanle.setBorder (new TitledBorder); rightPanle.setLayout (new GridLayout NameTxt = new JTextField ("picture name: bear"); nameTxt.setEditable (false); nameTxt.setBackground (Color.PINK); stepTxt = new JTextField ("steps: 0"); stepTxt.setEditable (false); stepTxt.setBackground (Color.PINK); rightPanle.add (nameTxt,BorderLayout.WEST); rightPanle.add (stepTxt,BorderLayout.EAST) TopPanel.add (rightPanle, BorderLayout.EAST); / / add this panel to the window this.add (topPanel,BorderLayout.NORTH);} private void init () {/ / set the title of the form this.setTitle ("jigsaw puzzle"); / / set the size of the form this.setSize (1000) / / set the form to center on the screen this.setLocationRelativeTo (null); / / set the fixed size this.setResizable (false); / / set the default window closing operation this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); selectedPicture = this.picMap.get (items [0]);}}

At this time, we have built the main interface of the game, but the code of the jigsaw puzzle area and the game preview area of the main interface has not been implemented yet. And we also need to abstract each piece of puzzle in the jigsaw puzzle into a class, and we need to bind mouse events to each puzzle.

Thank you for reading this article carefully. I hope the article "how to achieve jigsaw puzzles in java" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report