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
| package Itluo;
import javax.swing.*; import javax.swing.border.BevelBorder; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.util.Arrays; import java.util.Objects; import java.util.Random;
public class game extends JFrame implements ActionListener { int [][]arr1=new int[4][4]; int x1; int y1; int[][]win1={ {1, 5, 9, 13}, {2, 6, 10, 14}, {2, 7, 11, 15}, {4, 8, 12, 0} }; int step=0; JMenuItem replay=new JMenuItem("重新游戏"); JMenuItem reLogin=new JMenuItem("重新登录"); JMenuItem Exsist=new JMenuItem("关闭游戏"); JMenuItem abouT=new JMenuItem("公众号"); public game(){ Thelooking(); Menue0(); changearr();
runimagin();
this.setVisible(true);
}
private void changearr() { int []arr={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; for (int i = 0; i < arr.length; i++) { Random rd=new Random(); int x= rd.nextInt(16); int x1=arr[i]; arr[i]=arr[x]; arr[x]=x1; }
int y=0; for (int i = 0; i < 4; i++) { for (int i1 = 0; i1 < 4; i1++) { arr1[i1][i]=arr[y]; if(arr[y]==0){ x1=i1; y1=i; } y++; } } }
private void runimagin() { this.getContentPane().removeAll(); if(win()){ arr1[3][3]=16; } for (int j = 0; j < 4; j++) { for (int i = 0; i < 4; i++) { int num=arr1[i][j]; JLabel run1=new JLabel(new ImageIcon("C:\\Users\\luo66\\Desktop\\作业\\blue cup_java\\untitled11\\image\\"+num+".jpg")); run1.setBounds(105*i+83,j*105+134,105,105); run1.setBorder(new BevelBorder(0)); this.getContentPane().add(run1); this.getContentPane().repaint(); } } JLabel s=new JLabel("步数:"+step); s.setBounds(10,0,100,50); this.getContentPane().add(s); }
private void printfall(){ this.getContentPane().removeAll(); JLabel run1=new JLabel(new ImageIcon("C:\\Users\\luo66\\Desktop\\作业\\blue cup_java\\untitled11\\image\\all.jpg")); run1.setBounds(83,134,420,420); this.getContentPane().add(run1); this.getContentPane().repaint(); }
private void Menue0() { JMenuBar jb=new JMenuBar();
JMenu funcation=new JMenu("功能"); JMenu about=new JMenu("关于"); replay.addActionListener(this); reLogin.addActionListener(this); Exsist.addActionListener(this); abouT.addActionListener(this); funcation.add(replay); funcation.add(reLogin); funcation.add(Exsist);
about.add(abouT);
jb.add(funcation); jb.add(about);
this.setJMenuBar(jb); }
private void Thelooking() { this.setSize(603,680); this.setTitle("丰哥拼图单机模式 v-1.0"); this.setAlwaysOnTop(true); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(3); this.setLayout(null); KeyListener a=new KeyListener() { @Override public void keyTyped(KeyEvent e) {
}
@Override public void keyPressed(KeyEvent e) { int p=e.getKeyCode(); if(p==65){ printfall();
} }
@Override public void keyReleased(KeyEvent e) { if(win()){ System.out.println("胜利啦"); return; } int w=e.getKeyCode(); System.out.println(w); if (w==37){ if(x1==3)return; arr1[x1][y1]=arr1[x1+1][y1]; arr1[x1+1][y1]=0; x1++;step++; runimagin(); }else if(w==38){ if(y1==3)return; arr1[x1][y1]=arr1[x1][y1+1]; arr1[x1][y1+1]=0; y1++;step++; runimagin(); }else if(w==39){ if(x1==0)return; arr1[x1][y1]=arr1[x1-1][y1]; arr1[x1-1][y1]=0; x1--;step++; runimagin(); }else if(w==40){ if(y1==0)return; arr1[x1][y1]=arr1[x1][y1-1]; arr1[x1][y1-1]=0; y1--;step++; runimagin(); }else if(w==65){ runimagin(); }else if(w==81){ arr1= new int[][]{ {1, 5, 9, 13}, {2, 6, 10, 14}, {2, 7, 11, 15}, {4, 8, 12, 0} }; runimagin(); } } }; this.addKeyListener(a); } private boolean win(){ for (int i = 0; i < 4; i++) { for (int i1 = 0; i1 < 4; i1++) { if(i1==3&&i==3)continue; if(arr1[i1][i]!=win1[i1][i])return false; } } return true; }
@Override public void actionPerformed(ActionEvent e) { Object luo=e.getSource(); if(luo==replay){ System.out.println("重新游戏"); step=0; changearr(); runimagin(); }else if(luo==reLogin){ System.out.println("重新登录"); this.setVisible(false); JFrame Login1=new JFrame(); Login1.setTitle("登录"); Login1.setVisible(true); } else if (luo==Exsist) { System.exit(0); } else if (luo==abouT) { System.out.println("公众号"); JDialog t=new JDialog(); JLabel s=new JLabel(new ImageIcon("C:\\Users\\luo66\\Desktop\\作业\\blue cup_java\\untitled11\\image\\pay.jpg")); s.setBounds(0,0,451,484); t.getContentPane().add(s); t.setSize(500,500); t.setAlwaysOnTop(true); t.setLocationRelativeTo(null); t.setModal(true); t.setVisible(true);
} } }
|