JavaFx實(shí)現(xiàn)拼圖游戲
最近學(xué)習(xí)JavaFx,發(fā)現(xiàn)網(wǎng)上大概只有官方文檔可以查閱,學(xué)習(xí)資料較少,寫個(gè)拼圖游戲供記錄。。
大概說一下思路:
1.面板的構(gòu)建:面板采用GridPane,方便3*3的圖片布局。
2.每個(gè)小格子中的圖片當(dāng)然不是一張張手工切好的,利用imageview.setViewPort(Rectangle2D 2d)的方法進(jìn)行切割。
3.再來說鼠標(biāo)點(diǎn)擊時(shí)圖片的移動(dòng),這時(shí)候以里面的空格子為中心,不以鼠標(biāo)點(diǎn)擊的那個(gè)事件源為中心,這樣可以避免走彎路(當(dāng)時(shí)我是有一種柳暗花明的感覺。)。
4.鼠標(biāo)點(diǎn)擊后空格子和其周圍帶圖片格子的交換就比較簡(jiǎn)單了,重新設(shè)置位置即可。
5.每交換一次檢查是否拼圖成功。
關(guān)于界面初始化:定義一個(gè)ImageView的數(shù)組,長(zhǎng)度為9,將其按順序分別為第1,2,3....個(gè)格子,然后再產(chǎn)生8個(gè)0-8的不重復(fù)并且逆序數(shù)為偶數(shù)的隨機(jī)數(shù)的數(shù)組,然后將此隨機(jī)數(shù)作為ImageView數(shù)組的下標(biāo),將ImageView順序排列在格子中。為什么必須要逆序數(shù)為偶數(shù)呢?這是因?yàn)檫@樣圖才能拼成功!
關(guān)于判斷拼圖成功:有了上面的初始化方法,判斷就很簡(jiǎn)單了,只需要ImageView[0]對(duì)應(yīng)第一個(gè)格子,,后面類似,,這樣就拼成功了。需要注意我們只產(chǎn)生了8個(gè)隨機(jī)數(shù),而我們有9個(gè)格子,所以得把那個(gè)隨機(jī)數(shù)組中沒有的數(shù)字找出來,然后比較。有公式:n = 3 * r + c。其中n表示ImageView數(shù)組的下標(biāo),r表示此imageView的行號(hào),c表示列號(hào)。
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是不在隨機(jī)數(shù)組的那個(gè)數(shù)字 ImageView[] imageViews = new ImageView[9]; 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(); //自定義的函數(shù),產(chǎn)生逆序數(shù)為偶數(shù)的不重復(fù)數(shù)組 Image image = new Image("1.png"); GridPane gridPane = new GridPane(); for(int i = 0, k = 0; i <= 2; ++i) { for(int j = 0; j <= 2; ++j, ++k) { imageViews[k] = new ImageView(image); //初始化數(shù)組 imageViews[k].setOnMouseClicked(new myevent()); //設(shè)置點(diǎn)擊事件 imageViews[k].setViewport(new Rectangle2D(100 * j, 100 * i, 100, 100)); //切割圖片 } } gridPane.add(imageViews[n[0]], 0, 0); //按照產(chǎn)生的隨機(jī)數(shù)將imageView數(shù)組加入面板 gridPane.add(imageViews[n[1]], 1, 0); gridPane.add(imageViews[n[2]], 2, 0); gridPane.add(imageViews[n[3]], 0, 1); gridPane.add(imageViews[n[4]], 1, 1); gridPane.add(imageViews[n[5]], 2, 1); gridPane.add(imageViews[n[6]], 0, 2); gridPane.add(imageViews[n[7]], 1, 2); m = findnum(n); //找出那個(gè)不在隨機(jī)數(shù)組里面的數(shù)字 ImageView incomp = new ImageView(imageViews[m].getImage()); //用于顯示空格子的圖片 ImageView comp = new ImageView(image); //用于顯示完整的大圖 incomp.setViewport(imageViews[m].getViewport()); Image image2 = new Image("2.png"); //2.png為一個(gè)透明圖,放在空格子中 imageViews[m].setImage(image2); gridPane.add(imageViews[m], 2, 2); gridPane.setGridLinesVisible(true); BorderPane borderPane = new BorderPane(gridPane); VBox right = new VBox(20, incomp, comp); borderPane.setRight(right); Scene scene = new Scene(borderPane, 820, 420); stage.setScene(scene); stage.setResizable(false); stage.show(); } public int[] random() { //生成8個(gè)不重復(fù)的逆序數(shù)為偶數(shù)的數(shù)字 int[] ran = new int[8]; while(iso(ran) == false) { ran = random_num(); } return ran; } public int[] random_num() { //生成8個(gè)不重復(fù)數(shù) int r[] = new int[8]; Random random = new Random(); for(int i = 0; i < 8; ++i) { r[i] = random.nextInt(9); for(int j = 0;j < i; ++j) { while(r[i] == r[j]) { i--; break; } } } return r; } public boolean iso(int[] num) { //判斷逆序數(shù)是否為偶數(shù) int sum = 0; for(int i = 0; i <= 6; ++i) { for(int j = i; j <= 7; j++) { if(num[i] > num[j]) { sum++; } } } if((sum % 2) == 0 && sum != 0) { return true; } return false; } class myevent implements EventHandler<MouseEvent> { //點(diǎn)擊事件的實(shí)現(xiàn) @Override public void handle(MouseEvent arg0) { // TODO Auto-generated method stub ImageView img = (ImageView) arg0.getSource(); double sx = img.getLayoutX(); double sy = img.getLayoutY(); double dispx = sx - imageViews[m].getLayoutX(); double dispy = sy - imageViews[m].getLayoutY(); if((dispx == -100) && (dispy == 0 )) { //點(diǎn)擊的空格左邊的格子 swapimg(img, imageViews[m]); //交換imageView if(issucc(imageViews)) { //判斷是否拼成功 Alert alert = new Alert(AlertType.WARNING, "成功!"); alert.show(); } } else if ((dispx == 0) && (dispy == -100)) { //上面的格子 swapimg(img, imageViews[m]); if(issucc(imageViews)) { Alert alert = new Alert(AlertType.WARNING, "成功!"); alert.show(); } } else if((dispx == 100) && (dispy == 0)) { //右邊的格子 swapimg(img, imageViews[m]); if(issucc(imageViews)) { Alert alert = new Alert(AlertType.WARNING, "成功!"); alert.show(); } } else if((dispx == 0) && (dispy == 100)) { //下面的格子 swapimg(img, imageViews[m]); if(issucc(imageViews)) { Alert alert = new Alert(AlertType.WARNING, "成功!"); alert.show(); } } } public void swapimg(ImageView i1, ImageView i2) { //交換兩個(gè)imageView的實(shí)現(xiàn) int row1 = GridPane.getRowIndex(i1); int colu1 = GridPane.getColumnIndex(i1); int row2 = GridPane.getRowIndex(i2); int colu2 = GridPane.getColumnIndex(i2); GridPane.setRowIndex(i1, row2); GridPane.setColumnIndex(i1, colu2); GridPane.setRowIndex(i2, row1); GridPane.setColumnIndex(i2, colu1); } } public boolean issucc(ImageView[] imageViews) { //判斷是否拼成功 for(int i = 0; i <= 8; ++i) { if(i != 3 * GridPane.getRowIndex(imageViews[i]) + GridPane.getColumnIndex(imageViews[i])) { return false; } } return true; } public int findnum(int[] n) { //找出m for(int j = 0; j <= 8; ++j) { if((j == n[0]) || (j == n[1]) || (j == n[2]) || (j == n[3]) || (j == n[4]) || (j == n[5]) || (j == n[6]) || (j == n[7])) { } else { return j; } } return -1; } }
截圖如下:
說明:各位看官如果有更好的思路,歡迎留言~~大家共同進(jìn)步
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java數(shù)據(jù)結(jié)構(gòu)順序表從零基礎(chǔ)到精通進(jìn)階
程序中經(jīng)常需要將一組數(shù)據(jù)元素作為整體管理和使用,需要?jiǎng)?chuàng)建這種元素組,用變量記錄它們,傳進(jìn)傳出函數(shù)等。一組數(shù)據(jù)中包含的元素個(gè)數(shù)可能發(fā)生變化,順序表則是將元素順序地存放在一塊連續(xù)的存儲(chǔ)區(qū)里,元素間的順序關(guān)系由它們的存儲(chǔ)順序自然表示2022-03-03簡(jiǎn)單學(xué)習(xí)Java API 設(shè)計(jì)實(shí)踐
API(Application Programming Interface,應(yīng)用程序編程接口)是一些預(yù)先定義的函數(shù),目的是提供應(yīng)用程序與開發(fā)人員基于某軟件或硬件的以訪問一組例程的能力,而又無(wú)需訪問源碼,或理解內(nèi)部工作機(jī)制的細(xì)節(jié)。需要的可以了解一下2019-06-06SpringBoot快速搭建web項(xiàng)目詳細(xì)步驟總結(jié)
這篇文章主要介紹了SpringBoot快速搭建web項(xiàng)目詳細(xì)步驟總結(jié) ,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-12-12springboot登陸過濾功能的實(shí)現(xiàn)代碼
這篇文章主要介紹了springboot登陸過濾功能的實(shí)現(xiàn)代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-12-12基于RabbitMQ的簡(jiǎn)單應(yīng)用(詳解)
下面小編就為大家分享一篇基于RabbitMQ的簡(jiǎn)單應(yīng)用(詳解),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2017-11-11java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕節(jié)快樂)
這篇文章主要介紹了java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕節(jié)快樂),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12Java后臺(tái)實(shí)現(xiàn)微信支付和微信退款
這篇文章主要介紹了Java后臺(tái)實(shí)現(xiàn)微信支付和微信退款,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03