GameHuaRongDao.java 华容道
package game;import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;/*** 2023 * 2023 - 2022 * 2022* * 2023 * 2023 - 2022 * 2022* = (2022 + 1) * 2023 - 2022 * 2022* = 2022 * 2023 + 2023 - 2022 * 2022* = 2022 * (2023 - 2022) + 2023* = 2022 + 2023* = 4045* * @author ZengWenFeng* @email 117791303@qq.com* @mobile 13805029595*/
public class GameHuaRongDao extends JFrame
{private static final long serialVersionUID = 6877626593466163280L;private int[][] datas = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 0}};private int[][] winArr = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 0}};//定义两个元素获取到0号元素的位置int x0;int y0;private JButton btnWx;private JButton btnUp;private JButton btnDown;private JButton btnLeft;private JButton btnRight;private JButton btnHelp;private JButton btnReset;private JLabel lblInfo;private JPanel jPanel;public static String PATH_IMAGE;//构造方法public GameHuaRongDao(){//E:/PRJ_J2EE/PrjFlexJava/WebContent/WEB-INF/classes/game20230226_2/String pathImage = GameHuaRongDao.class.getResource("").getPath();pathImage = pathImage.substring(1);
// pathImage = "D:/jre/";PATH_IMAGE = pathImage + "image/";System.out.println(pathImage);//窗体初始化设置initApp();//打乱图片reset();//窗体上的组件设置显示initUi();//添加按钮事件addEvent();//this.setVisible(true);}//判断是否成功public boolean isWin(){//for (int i = 0; i < datas.length; i++){for (int j = 0; j < datas[i].length; j++){if (datas[i][j] != winArr[i][j]){return false;}}}//return true;}//成功消息方法public void View(){JOptionPane.showMessageDialog(this, "游戏胜利!!!");}//成功方法public void sucess(){//datas = new int[][]{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}};//btnUp.setEnabled(false);btnDown.setEnabled(false);btnLeft.setEnabled(false);btnRight.setEnabled(false);}//重新绘制面板方法public void rePaint(){//把面板上的组件清空jPanel.removeAll();//重新绘制for (int i = 0; i < datas.length; i++){for (int j = 0; j < datas[i].length; j++){JLabel picture = new JLabel(new ImageIcon(PATH_IMAGE + datas[i][j] + ".png"));picture.setBounds(90 * j, 90 * i, 90, 90);jPanel.add(picture);}}//this.add(jPanel);//再添加上去jPanel.repaint();}//给按钮添加事件public void addEvent(){btnUp.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){
// System.out.println("上");//判断边界if (x0 == 3){return;}//交换元素位置datas[x0][y0] = datas[x0 + 1][y0];datas[x0 + 1][y0] = 0;x0 += 1;//判断是否成功if (isWin()){sucess();}//重新绘制面板rePaint();//for (int i = 0; i < datas.length; i++){for (int j = 0; j < datas[i].length; j++){System.out.print(datas[i][j] + " ");}}//System.out.println("");}});btnDown.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){//判断边界if (x0 == 0){return;}//交换元素位置datas[x0][y0] = datas[x0 - 1][y0];datas[x0 - 1][y0] = 0;x0 -= 1;//判断是否成功if (isWin()){sucess();}//重新绘制面板rePaint();//for (int i = 0; i < datas.length; i++){for (int j = 0; j < datas[i].length; j++){System.out.print(datas[i][j] + " ");}}//System.out.println("");}});btnLeft.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){//判断边界if (y0 == 3){return;}//交换元素位置datas[x0][y0] = datas[x0][y0 + 1];datas[x0][y0 + 1] = 0;y0 += 1;//if (isWin()){sucess();}//重新绘制面板rePaint();//判断是否成功for (int i = 0; i < datas.length; i++){for (int j = 0; j < datas[i].length; j++){System.out.print(datas[i][j] + " ");}}//System.out.println("");}});btnRight.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){//判断边界if (y0 == 0){return;}//交换元素位置datas[x0][y0] = datas[x0][y0 - 1];datas[x0][y0 - 1] = 0;y0 -= 1;//判断是否成功if (isWin()){sucess();}//重新绘制面板rePaint();for (int i = 0; i < datas.length; i++){for (int j = 0; j < datas[i].length; j++){System.out.print(datas[i][j] + " ");}}//System.out.println("");}});btnHelp.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){//直接成功sucess();//重绘面板rePaint();//成功消息框显示View();for (int i = 0; i < datas.length; i++){for (int j = 0; j < datas[i].length; j++){System.out.print(datas[i][j] + " ");}}//System.out.println("");}});btnReset.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){//datas = new int[][]{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 0}};reset();rePaint();//btnUp.setEnabled(true);btnDown.setEnabled(true);btnLeft.setEnabled(true);btnRight.setEnabled(true);}});}//编写打乱二维数组的方法public void reset(){//Random rd = new Random();//for (int i = 0; i < datas.length; i++){for (int j = 0; j < datas[i].length; j++){int x = rd.nextInt(datas.length);int y = rd.nextInt(datas[i].length);int temp = datas[i][j];datas[i][j] = datas[x][y];datas[x][y] = temp;}}//wc: for (int i = 0; i < datas.length; i++){for (int j = 0; j < datas[i].length; j++){if (datas[i][j] == 0){x0 = i;y0 = j;break wc;}}}System.out.println("datas ============");for (int i = 0; i < datas.length; i++){for (int j = 0; j < datas[i].length; j++){System.out.print(datas[i][j]);System.out.print(" ");}System.out.println();}}//组件设置public void initUi(){//标题设置JLabel title = new JLabel(new ImageIcon(PATH_IMAGE + "title.png"));title.setBounds(354, 27, 232, 57);this.add(title);//游戏面板设置jPanel = new JPanel();jPanel.setBounds(150, 114, 360, 360);jPanel.setLayout(null);//把图片放进去 【像素90x90】 1 - 15数字图片for (int i = 0; i < datas.length; i++){for (int j = 0; j < datas[i].length; j++){JLabel picture = new JLabel(new ImageIcon(PATH_IMAGE + datas[i][j] + ".png"));picture.setBounds(90 * j, 90 * i, 90, 90);jPanel.add(picture);}}this.add(jPanel);//btnWx = new JButton(new ImageIcon(PATH_IMAGE + "wx_150x150.png"));btnWx.setBounds(680, 10, 150, 150);this.add(btnWx);//上下左右按钮【 57 x 57的像素】btnUp = new JButton(new ImageIcon(PATH_IMAGE + "shang.png"));btnUp.setBounds(732, 165, 57, 57);this.add(btnUp);btnDown = new JButton(new ImageIcon(PATH_IMAGE + "xia.png"));btnDown.setBounds(732, 247, 57, 57);this.add(btnDown);btnLeft = new JButton(new ImageIcon(PATH_IMAGE + "zuo.png"));btnLeft.setBounds(650, 247, 57, 57);this.add(btnLeft);btnRight = new JButton(new ImageIcon(PATH_IMAGE + "you.png"));btnRight.setBounds(813, 247, 57, 57);this.add(btnRight);//求助、重置按钮 【 108 x 45的像素】btnHelp = new JButton(new ImageIcon(PATH_IMAGE + "qiuzhu.png"));btnHelp.setBounds(626, 344, 108, 45);this.add(btnHelp);btnReset = new JButton(new ImageIcon(PATH_IMAGE + "chongzhi.png"));btnReset.setBounds(786, 344, 108, 45);this.add(btnReset);lblInfo = new JLabel();lblInfo.setBounds(580, 400, 400, 120);lblInfo.setFont(new java.awt.Font("Default", 1, 27));lblInfo.setForeground(Color.red);lblInfo.setText("<html><body>" + "TEL&WX:13805029595" + "<br>" + "曾小帅" + "<br>" + "117791303@qq.com" + "<body></html>");this.add(lblInfo);//背景设置JLabel background = new JLabel(new ImageIcon(PATH_IMAGE + "background.png"));background.setBounds(0, 0, 960, 530);this.add(background);}//显示窗体方法public void initApp(){this.setTitle("Hua Rong Dao");this.setSize(960, 565);this.setDefaultCloseOperation(3);this.setLayout(null);this.setLocationRelativeTo(null);this.setAlwaysOnTop(true);this.setResizable(false);}public static void main(String[] args){new GameHuaRongDao();}}