java版数独游戏界面实现(二)

时间:2021-11-27 22:14:31

本文实例为大家分享了java数独游戏界面实现的具体代码,供大家参考,具体内容如下

实现效果图:

这里写图片描述

主函数用于启动程序:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
package hlc.shudu.app;
 
import hlc.shudu.src.ShuduHelper;
import hlc.shudu.ui.ShuduMainFrame;
 
public class AppStart {
 
  public static void main(String[] args) {
    ShuduMainFrame mainFrame = new ShuduMainFrame();
    mainFrame.setVisible(true);
 
  }
}

主窗体类(包含消息区,时间区,游戏区):

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package hlc.shudu.ui;
 
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Time;
import java.text.SimpleDateFormat;
 
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.Timer;
import javax.swing.border.TitledBorder;
 
/*
 * 数独主窗体
 */
public class ShuduMainFrame extends JFrame {
 
  public static int pass = 1; // 关卡
  public static JLabel lbPass; // 显示关卡的lable
  public static long usedTime = 0; // 玩家用时
  private ShuduCanvers panelCanvers; // 主游戏区
  public static Timer userTimeAction;
 
  /*
   * 默认构造函数
   */
  public ShuduMainFrame() {
    // 初始化方法
    init();
    // 添加组件
    addComponent();
    // 添加主游戏区
    addCanvers();
 
  }
 
  /*
   * 添加主游戏区
   */
  private void addCanvers() {
    panelCanvers = new ShuduCanvers();
    panelCanvers.setBorder(new TitledBorder("游戏区"));
 
    // 将主游戏区添加到窗体中
    this.add(panelCanvers, BorderLayout.CENTER);
 
  }
 
  /*
   * 添加组件区
   */
  private void addComponent() {
    JPanel panelComponent = new JPanel();
    // 添加消息区
    addPanelMsg(panelComponent);
    // 添加时间区
    addPanelTime(panelComponent);
 
    // 将组件添加到窗体顶部
    this.add(panelComponent, BorderLayout.NORTH);
 
  }
 
  private void addPanelTime(JPanel panelComponent) {
    JPanel panelTime = new JPanel();
    panelTime.setBorder(new TitledBorder("时间"));
    panelTime.setLayout(new GridLayout(2, 1));
 
    final JLabel lbSysTime = new JLabel();
    final JLabel lbUserTime = new JLabel();
 
    panelTime.add(lbSysTime, BorderLayout.NORTH);
    panelTime.add(lbUserTime, BorderLayout.SOUTH);
 
    // 设置系统时间定时器
    Timer sysTimeAction = new Timer(500, new ActionListener() {
 
      @Override
      public void actionPerformed(ActionEvent e) {
        long timeMillis = System.currentTimeMillis();
        SimpleDateFormat df = new SimpleDateFormat(
            "yyyy-MM-dd HH:mm:ss");
        lbSysTime.setText("  系统时间: " + df.format(timeMillis));
      }
    });
    sysTimeAction.start();
    userTimeAction = new Timer(1000, new ActionListener() {
 
      @Override
      public void actionPerformed(ActionEvent e) {
        lbUserTime.setText("  您已用时: " + (++usedTime)+ " sec.");
      }
    });
    userTimeAction.start();
 
    panelComponent.add(panelTime, BorderLayout.EAST);
 
  }
 
  /*
   * 添加消息区
   */
  private void addPanelMsg(JPanel panelComponent) {
    // panelComponent.setBorder(new TitledBorder("消息区"));
    panelComponent.setLayout(new GridLayout(1, 3));
    Font font14 = new Font("", 4, 14);
    Font font28 = new Font("", 2, 28);
 
    JPanel panelMsg = new JPanel();
    panelMsg.setBorder(new TitledBorder("消息区"));
 
    JLabel lbPass1 = new JLabel("关卡:第");
    lbPass1.setFont(font14);
    panelMsg.add(lbPass1);
 
    // 显示关卡数
    lbPass = new JLabel("" + pass);
    lbPass.setForeground(Color.RED);
    lbPass.setFont(font28);
    panelMsg.add(lbPass);
 
    JLabel lbPass2 = new JLabel("关/总共10关");
    lbPass2.setFont(font14);
    panelMsg.add(lbPass2);
    panelComponent.add(panelMsg, BorderLayout.CENTER);
 
  }
 
  /*
   * 界面初始化
   */
  private void init() {
    ImageIcon image = new ImageIcon("icon/icon.png");
    this.setIconImage(image.getImage());
    // 设置窗口初始大小
    this.setSize(515, 600);
    // 设置窗口初始位置
    this.setLocation(500, 50);
    // 设置窗口标题
    this.setTitle("数独游戏(By:侯龙超)");
    // 设置窗体不允许改变大小
    this.setResizable(false);
    // 设置默认关闭操作
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}

游戏区画布:

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package hlc.shudu.ui;
 
import hlc.shudu.src.ShuduHelper;
 
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dialog.ModalExclusionType;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
 
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
 
public class ShuduCanvers extends JPanel implements MouseListener {
  ShuduCell[][] cells;
  // 得到数独数组
  int[][] maps = new int[9][9];
  private SelectNumFrame selectNum;
 
  /*
   * 默认构造函数
   */
  public ShuduCanvers() {
    ShuduMainFrame.usedTime = 0;
    maps = ShuduHelper.getMap();
    // 加载数独区
    this.setLayout(null);
    cells = new ShuduCell[9][9];
    for (int i = 0; i < 9; i++) {
      for (int j = 0; j < 9; j++) {
        // this.remove(cells[i][j]);
        // 创建单元格
        cells[i][j] = new ShuduCell();
        // 设置位置
        cells[i][j].setLocation(20 + i * 50 + (i / 3) * 5, 20 + j * 50
            + (j / 3) * 5);
        if (passRole(ShuduMainFrame.pass)) {
          cells[i][j].setText("" + maps[i][j]);
          // 设置背景颜色
          cells[i][j].setBackground(getColor(maps[i][j]));
          cells[i][j].setEnabled(false);
          cells[i][j].setForeground(Color.gray);
        } else {
          cells[i][j].addMouseListener(this);
        }
        this.add(cells[i][j]);
      }
    }
    checkFinish();
 
    // reLoadCanvers();
  }
 
  /*
   * 检查是否完成
   */
  private void checkFinish() {
    for (int i = 0; i < 9; i++) {
      for (int j = 0; j < 9; j++) {
          if (!check(i, j)) {
            return;
          }
        }
      }
 
    // 停止用户用时计时器
    ShuduMainFrame.userTimeAction.stop();
    // 清除所有cell监听
    clearAllListener();
    // 闯关数加一
    ShuduMainFrame.pass += 1;
    if (ShuduMainFrame.pass > 10) {
      int o = JOptionPane
          .showConfirmDialog(this, "您已经通关了,是否重头开始?", "", 0);
      if (o == 1) {
        System.exit(0);
      } else {
        ShuduMainFrame.pass = 1;
      }
    } else {
      JOptionPane.showMessageDialog(this, "恭喜你通过本关!用时:"
          + ShuduMainFrame.usedTime + "秒\n即将进入下一关!");
    }
    // 更新关卡提示
    ShuduMainFrame.lbPass.setText("" + ShuduMainFrame.pass);
    // 开始新的关卡
    reLoadCanvers();
    // 打开用户用时计时器
    ShuduMainFrame.userTimeAction.start();
 
  }
 
  /*
   * 检查指定坐标处的单元格
   */
 
  private boolean check(int i, int j) {
    if (cells[i][j].getText().isEmpty()) {
      return false;
    }
 
    for (int k = 0; k < 9; k++) {
      if (cells[i][j].getText().trim().equals(cells[i][k].getText().trim()) && j!=k) {
        return false;
      }
      if (cells[i][j].getText().trim().equals(cells[k][j].getText().trim()) && i != k) {
        return false;
      }
      int ii = (i / 3) * 3 + k / 3;
      int jj = (j / 3) * 3 + k % 3;
      if (cells[i][j].getText().trim().equals(cells[ii][jj].getText().trim()) &&!(i == ii && j == jj)) {
        return false;
      }
 
    }
    return true;
  }
 
  /*
   * 重新加载数独区
   */
  public void reLoadCanvers() {
    ShuduMainFrame.usedTime = 0;
    maps = ShuduHelper.getMap();
    for (int i = 0; i < 9; i++) {
      for (int j = 0; j < 9; j++) {
        this.remove(cells[i][j]);
        // 创建单元格
        cells[i][j] = new ShuduCell();
        // 设置位置
        cells[i][j].setLocation(20 + i * 50 + (i / 3) * 5, 20 + j * 50
            + (j / 3) * 5);
        if (passRole(ShuduMainFrame.pass)) {
          cells[i][j].setText("" + maps[i][j]);
          // 设置背景颜色
          cells[i][j].setBackground(getColor(maps[i][j]));
          cells[i][j].setEnabled(false);
          cells[i][j].setForeground(Color.gray);
        } else {
          cells[i][j].addMouseListener(this);
        }
        this.add(cells[i][j]);
      }
    }
    this.repaint();
    checkFinish();
 
  }
 
  /*
   * 根据关卡随机产生该位置是否显示数字
   */
  private boolean passRole(int pass) {
    // TODO Auto-generated method stub
    return Math.random() * 11 > pass;
  }
 
  /*
   * 根据数字获得颜色
   */
  private Color getColor(int i) {
    Color color = Color.pink;
    switch (i) {
    case 1:
      color = new Color(255, 255, 204);
      break;
    case 2:
      color = new Color(204, 255, 255);
      break;
    case 3:
      color = new Color(255, 204, 204);
      break;
    case 4:
      color = new Color(255, 204, 153);
      break;
    case 5:
      color = new Color(204, 255, 153);
      break;
    case 6:
      color = new Color(204, 204, 204);
      break;
    case 7:
      color = new Color(255, 204, 204);
      break;
    case 8:
      color = new Color(255, 255, 255);
      break;
    case 9:
      color = new Color(153, 255, 153);
      break;
    default:
      break;
    }
    return color;
  }
 
  @Override
  public void mouseClicked(MouseEvent e) {
    // TODO Auto-generated method stub
 
  }
 
  @Override
  public void mousePressed(MouseEvent e) {
    int modes = e.getModifiers();
    if ((modes & InputEvent.BUTTON3_MASK) != 0) {// 点击鼠标右键
      // 清空点击单元格上的内容
      ((ShuduCell) e.getSource()).setText("");
    } else if ((modes & InputEvent.BUTTON1_MASK) != 0) {// 点击鼠标左键
      // 如果选择数字窗口存在则销毁
      if (selectNum != null) {
        selectNum.dispose();
      }
      // 新建一个选择窗口
      selectNum = new SelectNumFrame();
      // 设置成模态窗口
      selectNum.setModal(true);
      // 设置选择窗口在显示器上的位置
      selectNum.setLocation(e.getLocationOnScreen().x,
          e.getLocationOnScreen().y);
      // 将点击的单元格传递给数字选择窗口
      selectNum.setCell((ShuduCell) e.getSource());
      // 显示数字选择窗口
      selectNum.setVisible(true);
    }
    checkFinish();
  }
 
  /*
   * 清除所有cell的点击监听
   */
  private void clearAllListener() {
    for (int i = 0; i < 9; i++) {
      for (int j = 0; j < 9; j++) {
        cells[i][j].removeMouseListener(this);
      }
    }
 
  }
 
  @Override
  public void mouseReleased(MouseEvent e) {
    // TODO Auto-generated method stub
 
  }
 
  @Override
  public void mouseEntered(MouseEvent e) {
    // TODO Auto-generated method stub
 
  }
 
  @Override
  public void mouseExited(MouseEvent e) {
    // TODO Auto-generated method stub
 
  }
 
}

数独单元格:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package hlc.shudu.ui;
 
import java.awt.Color;
import java.awt.Font;
 
import javax.swing.JButton;
 
public class ShuduCell extends JButton {
 public ShuduCell(){
  this.setSize(50,50);
  Font font = new Font("",2,24);
  this.setFont(font);
  this.setBackground(new Color(255,153,102));
  this.setForeground(Color.BLUE);
 }
}

数字选择框:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package hlc.shudu.ui;
 
import java.awt.Color;
import java.awt.Window;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
 
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
 
public class SelectNumFrame extends JDialog implements MouseListener {
 private ShuduCell cell;
 
 public void setCell(ShuduCell cell) {
  this.cell = cell;
 }
 
 public SelectNumFrame(){
  //隐藏界面上面的工具栏
  this.setUndecorated(true);
  this.setSize(150, 150);
  this.setBackground(new Color(255,204,153, 123));
  this.setLayout(null);
  addNum();
 }
 //添加数字1~9
 private void addNum() {
  for (int i = 0; i < 3; i++) {
   for (int j = 0; j < 3; j++) {
    JButton btn = new JButton();
    btn.setSize(50, 50);
    btn.setLocation(i*50,j*50);
    btn.setText(""+(j*3+i+1));
    btn.addMouseListener(this);
    this.add(btn);
   }
  }
 
 }
 
 @Override
 public void mouseClicked(MouseEvent e) {
  // TODO Auto-generated method stub
 
 }
 
 @Override
 public void mousePressed(MouseEvent e) {
  int modes = e.getModifiers();
  if ((modes & InputEvent.BUTTON1_MASK) != 0) {
   JButton btn = (JButton) e.getSource();
   cell.setText(btn.getText());
  }
  this.dispose();
 }
 
 @Override
 public void mouseReleased(MouseEvent e) {
  // TODO Auto-generated method stub
 
 }
 
 @Override
 public void mouseEntered(MouseEvent e) {
  // TODO Auto-generated method stub
 
 }
 
 @Override
 public void mouseExited(MouseEvent e) {
  // TODO Auto-generated method stub
 
 }
 
 
}

完整程序包可在GitHub上下载:https://github.com/houlongchao/shudu.git

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://blog.csdn.net/da_keng/article/details/47779141