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)擊后空格子和其周圍帶圖片格子的交換就比較簡單了,重新設(shè)置位置即可。
5.每交換一次檢查是否拼圖成功。
關(guān)于界面初始化:定義一個(gè)ImageView的數(shù)組,長度為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)于判斷拼圖成功:有了上面的初始化方法,判斷就很簡單了,只需要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)步
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java實(shí)現(xiàn)curl調(diào)用帶參數(shù)接口方法
本文主要介紹了Java實(shí)現(xiàn)curl調(diào)用帶參數(shù)接口方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-04-04
java開發(fā)主流定時(shí)任務(wù)解決方案全橫評(píng)詳解
這篇文章主要為大家介紹了java開發(fā)主流定時(shí)任務(wù)解決方案全橫評(píng)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
MyBatis傳入?yún)?shù)的實(shí)例代碼
這篇文章主要介紹了MyBatis傳入?yún)?shù)的實(shí)例代碼的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06
java:程序包org.bouncycastle.jce.provider不存在問題及解決
這篇文章主要介紹了java:程序包org.bouncycastle.jce.provider不存在問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05
springboot中在非bean類中調(diào)用bean的實(shí)現(xiàn)方法
在Spring Boot中,非Bean類調(diào)用Bean方法通常需要通過靜態(tài)方法獲取Bean實(shí)例,然后調(diào)用相應(yīng)的方法,這種方法避免了直接在非Bean類中注入Bean,保持了代碼的簡潔和可維護(hù)性,通過這種方式,可以在不改變?cè)写a結(jié)構(gòu)的情況下,實(shí)現(xiàn)Bean方法的調(diào)用2025-02-02

