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 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
| package com.itheima.ui;
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 GameJFrame extends JFrame implements KeyListener, ActionListener { Random random = new Random(); // 这个数组用来表示一下图片的“位置” int[][] arr = new int[4][4];
// 记录一下空白格在二维数组里面的位置 int x = 0; int y = 0;
int[][] win = { {1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,0} };
String path = "/image/anime/anime3/";
// 记录一下步数 int step = 0;
// 更换图片是JMenu类,作为一级条目 JMenu changeItem = new JMenu("更换图片");
// 创建选项里面的分支对象(四个) JMenuItem fumo = new JMenuItem("Fumo"); JMenuItem anime = new JMenuItem("Anime"); JMenuItem replayItem = new JMenuItem("重新游戏"); // JMenuItem reloginItem = new JMenuItem("重新登录"); JMenuItem closeItem = new JMenuItem("关闭游戏");
JMenuItem accountItem = new JMenuItem("公众号");
public GameJFrame() { // 初始化页面 initFrame(); // 初始化菜单 initJMenuBar(); // 打乱一下数组顺序,方便生成乱序的图片 disruptArr(); // 初始化加载图片 initImage();
this.setVisible(true);
}
private void initFrame(){ this.setSize(680,740);
this.setTitle("拼图单机版 v1.0");
this.setAlwaysOnTop(true); // 设置默认页面居中 this.setLocationRelativeTo(null); // 设置只要关闭窗口,虚拟机就自动停止 this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); // 取消图片默认的居中放置,方便我们按照XY轴进行操作,否则图片的位置不会变化 this.setLayout(null); // 给整个页面添加键盘监听事件 this.addKeyListener(this); }
private void initJMenuBar(){ // 创建整个菜单的对象 JMenuBar jMenuBar = new JMenuBar();
// 创建菜单里面选项的对象(两个) JMenu functionMenu = new JMenu("功能"); JMenu aboutMenu = new JMenu("关于");
// 给菜单的条目绑定一下事件,包括一级条目和二级条目 replayItem.addActionListener(this); // reloginItem.addActionListener(this); closeItem.addActionListener(this); accountItem.addActionListener(this); changeItem.addActionListener(this); anime.addActionListener(this); fumo.addActionListener(this);
// 把fumo、anime等二级条目添加到一级条目里面 changeItem.add(anime); changeItem.add(fumo);
// 把它们都添加进对应的条目 functionMenu.add(changeItem); functionMenu.add(replayItem); // functionMenu.add(reloginItem); functionMenu.add(closeItem);
aboutMenu.add(accountItem);
jMenuBar.add(functionMenu); jMenuBar.add(aboutMenu);
// 把菜单添加到界面中 this.setJMenuBar(jMenuBar); }
private void initImage(){ // 清空所有图片
this.getContentPane().removeAll();
if(victory()){ JLabel winJLabel = new JLabel(new ImageIcon(GameJFrame.class.getResource("/image/win.jpg"))); winJLabel.setBounds(153,153,300,300); this.getContentPane().add(winJLabel); }
// 步数统计 JLabel stepCount = new JLabel("步数" + step); stepCount.setBounds(50,30,100,20); this.getContentPane().add(stepCount);
for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if(arr[i][j] == 0){ continue; } ImageIcon icon = new ImageIcon(GameJFrame.class.getResource(path + arr[i][j] + ".png")); JLabel jLabel = new JLabel(icon); // 指定图片位置 jLabel.setBounds(170*j,170*i,170,170); this.getContentPane().add(jLabel); } }
// 刷新一下页面 this.getContentPane().repaint(); }
private void disruptArr(){ int[] a = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; for (int i = 0; i < 16; i++) { int index = random.nextInt(16); int tmp = a[index]; a[index] = a[i]; a[i] = tmp; }
for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if(a[i*4+j] == 0){ x = i; y = j; } arr[i][j] = a[i * 4 + j]; } } }
@Override public void keyTyped(KeyEvent e) {
}
@Override public void keyPressed(KeyEvent e) { int code = e.getKeyCode(); if(code == 65){ this.getContentPane().removeAll(); JLabel all = new JLabel(new ImageIcon(GameJFrame.class.getResource(path + "all.jpg"))); all.setBounds(0,0,680,740); this.getContentPane().add(all); // 刷新页面 this.getContentPane().repaint(); } }
@Override public void keyReleased(KeyEvent e) {
int code = e.getKeyCode();
// 如果游戏胜利,应该直接结束方法,不能再移动,但要特别放行一下a操作,否则会出bug if(victory() && code != 65){ return; }
switch (code) { case 37: System.out.println("向左移动"); if(y == 3){ return; } arr[x][y] = arr[x][y+1]; arr[x][y+1] = 0; y++; step++; initImage(); break;
case 38: System.out.println("向上移动"); if(x == 3){ return; } arr[x][y] = arr[x+1][y]; arr[x+1][y] = 0; x++; step++; initImage(); break;
case 39: System.out.println("向右移动"); if(y == 0){ return; } arr[x][y] = arr[x][y-1]; arr[x][y-1] = 0; y--; step++; initImage(); break;
case 40: System.out.println("向下移动"); if(x == 0){ return; } arr[x][y] = arr[x-1][y]; arr[x-1][y] = 0; x--; step++; initImage(); break;
case 65: initImage(); break;
case 87: arr = new int[][]{ {1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,0} }; initImage(); break; } }
// 判断游戏是否胜利 public boolean victory(){ for(int i = 0; i < arr.length; i++){ for(int j = 0; j < arr[i].length; j++){ if(arr[i][j] != win[i][j]){ return false; } } } return true; }
@Override public void actionPerformed(ActionEvent e) { Object obj = e.getSource(); if(obj == replayItem){ System.out.println("重新游戏");
step = 0; disruptArr(); initImage(); }else if(obj == closeItem){ System.out.println("关闭游戏"); System.exit(0);
} else if(obj == accountItem){ System.out.println("关于我们");
// 创建一个弹窗对象 JDialog jDialog = new JDialog();
// 创建一个管理图片的容器对象JLabel JLabel jLabel = new JLabel(new ImageIcon(GameJFrame.class.getResource("/image/account.jpg")));
// 设置位置和宽高 jLabel.setBounds(0,0,258,258);
// 把图片添加到弹框里面 jDialog.getContentPane().add(jLabel);
// 给弹框设置大小 jDialog.setSize(344,344);
// 让弹框置顶展示 jDialog.setAlwaysOnTop(true);
// 让弹框居中 jDialog.setLocationRelativeTo(null);
// 弹框不关闭则无法操作底下的页面 jDialog.setModal(true);
// 展示弹框 jDialog.setVisible(true);
}else if(obj == anime){ int index = (random.nextInt(5) + 1); path = "/image/anime/anime" + index + "/"; disruptArr(); initImage(); }else if(obj == fumo){ int index = (random.nextInt(1) + 1); path = "/image/fumo/fumo" + index + "/"; disruptArr(); initImage(); } } }
|