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 jigsaw puzzle with Java

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

Share

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

This article introduces the relevant knowledge of "how to achieve jigsaw puzzles in Java". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Give me a rough idea:

1. Construction of the panel: the panel adopts GridPane, which is convenient for the layout of 3x3 pictures.

two。 Of course, the pictures in each small grid are not cut by hand, but are cut by imageview.setViewPort (Rectangle2D 2d).

3. Let's talk about the movement of the picture when the mouse is clicked, which is centered on the space inside, not on the event source of the mouse click, so as to avoid detours (I had a feeling of willow and flowers at that time. ).

4. Click on the mouse after the empty grid and its surrounding with picture grid exchange is relatively simple, you can reset the location.

5. Check whether the puzzle is successful with each exchange.

About interface initialization: define an array of ImageView with a length of 9, and order them as 1, 2, 3, respectively. A grid, and then produce 8 0-8 non-repetitive and inversely even random number array, and then use this random number as the subscript of the ImageView array, and arrange the ImageView order in the grid. Why does it have to be even in reverse order? This is because the picture can be successfully put together in this way!

About judging the success of the jigsaw puzzle: with the initialization method above, the judgment is very simple, as long as ImageView [0] corresponds to the first grid, and the later is similar, so the puzzle is successful. It is important to note that we only generate eight random numbers, and we have nine squares, so we have to find out the numbers that are not in that random array and compare them. There is a formula: n = 3 * r + c. Where n represents the subscript of the ImageView array, r represents the line number of the imageView, and c represents the column number.

Import javafx.application.Application;import javafx.event.EventHandler;import javafx.geometry.Rectangle2D;import javafx.scene.Scene;import javafx.scene.control.Alert;import javafx.scene.control.Alert.AlertType;import javafx.scene.image.Image;import javafx.scene.image.ImageView;import javafx.scene.input.MouseEvent;import javafx.scene.layout.BorderPane;import javafx.scene.layout.GridPane;import javafx.scene.layout.VBox;import javafx.stage.Stage;import java.util.Random; public class myJigsaw extends Application {public int m / / m is the number ImageView [] imageViews = new ImageView [9] that is not in the random array; public static void main (String [] args) {Application.launch (args);} @ Override public void start (Stage arg0) throws Exception {init (arg0);} public void init (Stage stage) {int [] n = random () / / Custom function that produces a non-repeating array Image image = new Image ("1.png") whose reverse order is even; GridPane gridPane = new GridPane (); for (int I = 0, k = 0; I

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