📄 gameframe.java
字号:
package com.sato.accp.game.jigsaw;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import java.awt.Dimension;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.GridLayout;
import javax.swing.ImageIcon;
public class GameFrame extends JFrame implements ActionListener {
protected JButton[][] buttons;
protected boolean[][] flags;
public GameFrame() {
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
/*
*拼图面板初始化
*/
public JPanel panelJigsawInit() {
//创建panelJigsaw面板
GridLayout gridLayout = new GridLayout(6, 4);
gridLayout.setHgap(0);
gridLayout.setVgap(0);
JPanel panelJigsaw = new JPanel(gridLayout);
//创建按纽组
buttons = new JButton[6][4];
//创建按纽状态组
flags = new boolean[6][4]; //true代表空单元格(既按纽上有白色图片),false代表非空单元格(既按纽上有彩色图片)
//设置按纽组的信息 与 状态组的信息,并将按纽逐个添加到panelJigsaw上
int count = 0;
ImageIcon[] imageIcons = RandImages.getImages("images/"); //获取被打乱顺序后的随机图片
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 4; j++) {
if (imageIcons[count].toString().equals("images/0.gif")) {
flags[i][j] = true;
} else {
flags[i][j] = false;
}
buttons[i][j] = new JButton(imageIcons[count]);
panelJigsaw.add(buttons[i][j]);
count++;
if (count == 21) {
break;
}
}
}
return panelJigsaw;
}
/*
*模型面板初始化
*/
public JPanel panelModelInit() {
//创建panelModel面板
GridLayout gridLayout = new GridLayout(1, 1);
JPanel panelModel = new JPanel(gridLayout);
//创建模型按纽
JButton buttonModel = new JButton(new ImageIcon("images/model.gif"));
buttonModel.setSize(300, 200);
buttonModel.addKeyListener(new Zuobi());
//将模型按纽添加到上
panelModel.add(buttonModel);
return panelModel;
}
/*
*增加panelJigSaw到内容面板上
*/
public void addPanelJigsaw() {
this.getContentPane().add(panelJigsawInit(), BorderLayout.CENTER);
}
/*
*增加panelModel到内容面板上
*/
public void addPanelModel() {
this.getContentPane().add(panelModelInit(), BorderLayout.EAST);
this.show();
}
/*
*注册监听器
*/
public void addListener() {
int count = 0;
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 4; j++) {
buttons[i][j].setActionCommand(i + "," + j); //设置按纽的动作命令
buttons[i][j].addActionListener(this); //注册到监听器
count++;
if (count == 21) {
break;
}
}
}
}
private void jbInit() throws Exception {
/*设置窗体的基本信息*/
this.setSize(new Dimension(702, 455)); //设置窗体的大小
this.setResizable(false); //设置窗体的大小为不可更改
this.show();
}
/*
*图象移动
*/
public void moveImage(int row, int col, int i, int j) {
ImageIcon spaceIcon = new ImageIcon("images/0.gif"); //白色图片
//交换图片
buttons[row][col].setIcon(buttons[i][j].getIcon());
buttons[i][j].setIcon(spaceIcon);
//重新设置按纽的状态信息
flags[row][col] = false;
flags[i][j] = true;
//播放提示音
if (gameOver()) {
Sound.playSuccess();
return;
}
Sound.playMove();
}
/*
*判断是否已经拼图成功
*/
public boolean gameOver() {
int count = 1;
boolean over = true;
for (int i = 0; i < 5 && over; i++) {
for (int j = 0; j < 4; j++) {
String url = "images/"; //存储图片的路径
String suffix = ".gif";
url = url + count + suffix;
if (!(buttons[i][j].getIcon().toString().equals(url))) {
over = false;
break;
}
count++;
url = null;
suffix = null;
}
}
return over;
}
/*
*事件处理代码
*/
public void actionPerformed(ActionEvent e) {
//获取动作命令
String actionCommand = e.getActionCommand();
//解析动作命令
String[] number = actionCommand.split(",");
int i = Integer.parseInt(number[0]); //被点击的按纽所在的行编号
int j = Integer.parseInt(number[1]); //被点击的按纽所在的列编号
operator(i, j);
}
/*
*操作控制
*/
public void operator(int sourceX, int sourceY) {
int targetX = 0; //目标按纽(空单元格)的行编号
int targetY = 0; //目标按纽(空单元格)的列编号
//确定目标按纽(空单元格)的位置后,做图片移动
if (sourceX - 1 >= 0) {
if (flags[sourceX - 1][sourceY]) {
targetX = sourceX - 1;
targetY = sourceY;
moveImage(sourceX, sourceY, targetX, targetY);
return;
}
}
if (sourceY + 1 < 4) {
if (flags[sourceX][sourceY + 1]) {
targetX = sourceX;
targetY = sourceY + 1;
moveImage(sourceX, sourceY, targetX, targetY);
return;
}
}
if (sourceY - 1 >= 0) {
if (flags[sourceX][sourceY - 1]) {
targetX = sourceX;
targetY = sourceY - 1;
moveImage(sourceX, sourceY, targetX, targetY);
return;
}
}
if (sourceX == 5) {
if (flags[sourceX - 1][sourceY]) {
targetX = sourceX - 1;
targetY = sourceY;
moveImage(sourceX, sourceY, targetX, targetY);
return;
}
}
Sound.playError();
}
/*
*作弊程序
*/
private class Zuobi implements java.awt.event.KeyListener {
public void keyPressed(java.awt.event.KeyEvent e) {
int count = 1;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 4; j++) {
buttons[i][j].setIcon(new ImageIcon("images/" + count +
".gif"));
count++;
if (count == 21) {
break;
}
flags[i][j] = false;
}
}
buttons[5][0].setIcon(new ImageIcon("images/0.gif"));
flags[5][0] = true;
}
public void keyTyped(java.awt.event.KeyEvent e) {
}
public void keyReleased(java.awt.event.KeyEvent e) {
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -