直接调用MainFrame()方法
package com.bingyi.stonepuzzle;
import javax.swing.*;
public class Test {
public static void main(String[] args) {
new MainFrame();
}
}
执行以下代码:
package com.bingyi.stonepuzzle;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;
public class MainFrame extends JFrame implements KeyListener {
int[][] date = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 0}
};
int[][] win = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 0}
};
int row;
int colum;
int count;
public MainFrame() {
this.addKeyListener(this);
/*初始化窗体*/
initFrame();
/*初始化数据*/
initDate();
/*绘制游戏界面*/
paintView();
/*窗体可视化*/
setVisible(true);
}
/*初始化数据*/
private void initDate() {
Random r = new Random();
for (int j = 0; j < date.length; j++) {
for (int i = 0; i < date[j].length; i++) {
int randomX = r.nextInt(4);
int randomY = r.nextInt(4);
int temp = date[j][i];
date[j][i] = date[randomX][randomY];
date[randomX][randomY] = temp;
}
}
for (int j = 0; j < date.length; j++) {
for (int i = 0; i < date[j].length; i++) {
if (date[j][i] == 0) {
row = j;
colum = i;
}
}
}
}
/*此方法用于初始化窗体*/
public void initFrame() {
/*
* 设置窗体关闭模式
* */
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
/*
设置窗体大小
*/
setSize(514, 595);
setTitle("冰逸拼图V1.0");
/*
* 设置窗体最顶层
* */
setAlwaysOnTop(true);
/*
* 设置窗体剧中
* */
setLocationRelativeTo(null);
/*
* 取消默认布局
* */
setLayout(null);
}
/*此方法用于绘制界面*/
public void paintView() {
getContentPane().removeAll();
if (victory()) {
JLabel winLbel = new JLabel("牛逼");
winLbel.setBounds(124, 230, 266, 88);
getContentPane().add(winLbel);
}
JButton btn = new JButton("重新游戏");
btn.setBounds(350, 20, 100, 20);
getContentPane().add(btn);
btn.setFocusable(false);
btn.addActionListener(e -> {
count = 0;
initDate();
paintView();
});
JLabel scoreLabel = new JLabel("步数为:" + count);
scoreLabel.setBounds(50, 20, 100, 20);
getContentPane().add(scoreLabel);
for (int j = 0; j < 4; j++) {
for (int i = 0; i < 4; i++) {
JLabel imagLabel = new JLabel(new ImageIcon("F:\\拼图游戏\\" + date[j][i] + ".jpg"));
imagLabel.setBounds(50 + 100 * i, 90 + 100 * j, 100, 100);
getContentPane().add(imagLabel);
}
}
JLabel background = new JLabel(new ImageIcon("F:\\拼图游戏\\背景.jpg"));
background.setBounds(26, 30, 450, 484);
getContentPane().add(background);
getContentPane().repaint();
}
/*
* 判断游戏是否胜利
* */
public boolean victory() {
for (int j = 0; j < date.length; j++) {
for (int i = 0; i < date[j].length; i++) {
if (date[j][i] != win[j][i]) {
return false;
}
}
}
return true;
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
/* 用于处理移动业务*/
move(keyCode);
/*每一次移动重新绘制界面*/
paintView();
}
private void move(int keyCode) {
if (victory()) {
return;
}
if (keyCode == 37) {
if (colum == 3) {
return;
}
int temp = date[row][colum];
date[row][colum] = date[row][colum + 1];
date[row][colum + 1] = temp;
colum++;
count++;
} else if (keyCode == 38) {
if (row == 3) {
return;
}
int temp = date[row][colum];
date[row][colum] = date[row + 1][colum];
date[row + 1][colum] = temp;
row++;
count++;
} else if (keyCode == 39) {
if (colum==0) {
return;
}
int temp = date[row][colum];
date[row][colum] = date[row][colum - 1];
date[row][colum - 1] = temp;
colum--;
count++;
} else if (keyCode == 40) {
if (row == 0) {
return;
}
int temp = date[row][colum];
date[row][colum] = date[row - 1][colum];
date[row - 1][colum] = temp;
row--;
count++;
} else if (keyCode == 90) {
//作弊器
date = new int[][]{
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 0}
};
}
}
@Override
public void keyReleased(KeyEvent e) {
}
}
执行效果
作弊器为“z”