[Java] 拼图 →→→→→进入此内容的聊天室

来自 , 2020-04-24, 写在 Java, 查看 136 次.
URL http://www.code666.cn/view/b0f2ad44
  1. package Game;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.GridLayout;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7.  
  8. import javax.swing.ImageIcon;
  9. import javax.swing.JButton;
  10. import javax.swing.JFrame;
  11. import javax.swing.JLabel;
  12. import javax.swing.JPanel;
  13. import javax.swing.border.TitledBorder;
  14.  
  15. public class MedleyGame extends JFrame {
  16.  
  17.         private JPanel centerPanel;// 拼图按钮面板
  18.  
  19.         private JButton emptyButton;// 空白按钮对象
  20.  
  21.         public static void main(String args[]) {
  22.                 try {
  23.                         MedleyGame frame = new MedleyGame();
  24.                         frame.setVisible(true);
  25.                 } catch (Exception e) {
  26.                         e.printStackTrace();
  27.                 }
  28.         }
  29.  
  30.         public MedleyGame() {
  31.                 super();// 继承JFrame类的构造方法
  32.                 setResizable(false);// 设置窗体大小不可改变
  33.                 setTitle("拼图游戏");// 设置窗体的标题
  34.                 setBounds(100, 100, 354, 640);// 设置窗体的显示位置及大小
  35.                 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置关闭窗体时退出程序
  36.  
  37.                 final JPanel topPanel = new JPanel();// 创建面板对象
  38.                 topPanel.setBorder(new TitledBorder(null, "",
  39.                                 TitledBorder.DEFAULT_JUSTIFICATION,
  40.                                 TitledBorder.DEFAULT_POSITION, null, null));// 为面板添加边框
  41.                 topPanel.setLayout(new BorderLayout());// 设置面板采用边界布局
  42.                 getContentPane().add(topPanel, BorderLayout.NORTH);// 将面板添加到窗体顶部
  43.  
  44.                 final JLabel modelLabel = new JLabel();// 创建显示参考图片的标签对象
  45.                 modelLabel.setIcon(new ImageIcon("img/mode1.jpg"));// 设置标签显示的参考图片
  46.                 topPanel.add(modelLabel, BorderLayout.WEST);// 将标签添加到面板的左侧
  47.  
  48.                 final JButton startButton = new JButton();// 创建“下一局”按钮对象
  49.                 startButton.setText("下一局");// 设置按钮的标签文本
  50.                 startButton.addActionListener(new StartButtonAction());// 为按钮添加监听器
  51.                 topPanel.add(startButton, BorderLayout.CENTER);// 将按钮添加到面板的中间
  52.  
  53.                 centerPanel = new JPanel();// 创建拼图按钮面板对象
  54.                 centerPanel.setBorder(new TitledBorder(null, "",
  55.                                 TitledBorder.DEFAULT_JUSTIFICATION,
  56.                                 TitledBorder.DEFAULT_POSITION, null, null));// 为面板添加边框
  57.                 centerPanel.setLayout(new GridLayout(0, 3));// 设置拼图按钮面板采用3列的网格布局
  58.                 getContentPane().add(centerPanel, BorderLayout.CENTER);// 将面板添加到窗体的中间
  59.  
  60.                 String[][] stochasticOrder = reorder();// 获得网格图片的随机摆放顺序
  61.                 for (int row = 0; row < 3; row++) {// 遍例行
  62.                         for (int col = 0; col < 3; col++) {// 遍例列
  63.                                 final JButton button = new JButton();// 创建拼图按钮对象
  64.                                 button.setName(row + "" + col);// 设置按钮的名称
  65.                                 button.setIcon(new ImageIcon(stochasticOrder[row][col])); // 为拼图按钮设置图片
  66.                                 if (stochasticOrder[row][col].equals("img/22.jpg")) // 判断是否为空白按钮
  67.                                         emptyButton = button;
  68.                                 button.addActionListener(new ImgButtonAction()); // 为拼图按钮添加监听器
  69.                                 centerPanel.add(button);// 将按钮添加到拼图按钮面板中
  70.                         }
  71.                 }
  72.                 //
  73.         }
  74.  
  75.         private String[][] reorder() {// 用来获取网格图片的随机摆放顺序
  76.                 String[][] exactnessOrder = new String[3][3];// 网格图片的正确摆放顺序
  77.                 for (int row = 0; row < 3; row++) {// 遍例行
  78.                         for (int col = 0; col < 3; col++) {// 遍例列
  79.                                 exactnessOrder[row][col] = "img/" + row + col + ".jpg";
  80.                         }
  81.                 }
  82.                 String[][] stochasticOrder = new String[3][3];// 网格图片的随机摆放顺序
  83.                 for (int row = 0; row < 3; row++) {// 遍例行
  84.                         for (int col = 0; col < 3; col++) {// 遍例列
  85.                                 while (stochasticOrder[row][col] == null) { // 随机摆放顺序的指定网格为空
  86.                                         int r = (int) (Math.random() * 3);// 取随机行
  87.                                         int c = (int) (Math.random() * 3);// 取随机列
  88.                                         if (exactnessOrder[r][c] != null) { // 正确摆放顺序的指定网格不为空
  89.                                                 // 将位于正确摆放顺序的指定网格的图片摆放到位于随机摆放顺序的指定网格中
  90.                                                 stochasticOrder[row][col] = exactnessOrder[r][c];
  91.                                                 // 将位于正确顺序的指定网格设置为空
  92.                                                 exactnessOrder[r][c] = null;
  93.                                         }
  94.                                 }
  95.                         }
  96.                 }
  97.                 return stochasticOrder;
  98.         }
  99.  
  100.         class ImgButtonAction implements ActionListener {// 拼图按钮监听器
  101.                 public void actionPerformed(ActionEvent e) {
  102.                         String emptyName = emptyButton.getName();// 获得空白按钮的名称
  103.                         char emptyRow = emptyName.charAt(0);// 获得空白按钮所在的行
  104.                         char emptyCol = emptyName.charAt(1);// 获得空白按钮所在的列
  105.                         JButton clickButton = (JButton) e.getSource();// 获得被点击按钮对象
  106.                         String clickName = clickButton.getName();// 获得被点击按钮的名称
  107.                         char clickRow = clickName.charAt(0);// 获得被点击按钮所在的行
  108.                         char clickCol = clickName.charAt(1);// 获得被点击按钮所在的列
  109.                         // 判断被点击按钮与空白按钮是否相临
  110.                         if (Math.abs(clickRow - emptyRow) + Math.abs(clickCol - emptyCol) == 1) {
  111.                                 // 将被点击按钮的图片移动到空白按钮上
  112.                                 emptyButton.setIcon(clickButton.getIcon());
  113.                                 // 设置被点击的按钮显示空白图片
  114.                                 clickButton.setIcon(new ImageIcon("img/22.jpg"));
  115.                                 emptyButton = clickButton;// 将被点击的按钮设置为空白按钮
  116.                         }
  117.                 }
  118.         }
  119.  
  120.         class StartButtonAction implements ActionListener {// 下一局按钮监听器
  121.                 public void actionPerformed(ActionEvent e) {
  122.                         String[][] stochasticOrder = reorder();// 获得网格图片的随机摆放顺序
  123.                         int i = 0;// 拼图按钮在拼图按钮面板中的索引
  124.                         for (int row = 0; row < 3; row++) {// 遍例行
  125.                                 for (int col = 0; col < 3; col++) {// 遍例列
  126.                                         JButton button = (JButton) centerPanel.getComponent(i++); // 获得位于指定索引的拼图按钮
  127.                                         button.setIcon(new ImageIcon(stochasticOrder[row][col])); // 为拼图按钮设置图片
  128.                                         if (stochasticOrder[row][col].equals("img/22.jpg")) // 判断是否为空白按钮
  129.                                                 emptyButton = button;
  130.                                 }
  131.                         }
  132.                 }
  133.         }
  134.  
  135. }
  136.  

回复 "拼图"

这儿你可以回复上面这条便签

captcha