拼图游戏来至开源中国社区
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import java.awt.BorderLayout; 2 import java.awt.Component; 3 import java.awt.Container; 4 import java.awt.Dimension; 5 import java.awt.Font; 6 import java.awt.Graphics; 7 import java.awt.GridBagConstraints; 8 import java.awt.GridBagLayout; 9 import java.awt.Image; 10 import java.awt.image.BufferedImage; 11 import java.awt.Point; 12 import java.awt.event.ActionEvent; 13 import java.awt.event.ActionListener; 14 import java.awt.Toolkit; 15 import java.io.*; 16 import java.io.IOException; 17 import java.util.Random; 18 19 import javax.imageio.ImageIO; 20 import javax.swing.ImageIcon; 21 import javax.swing.JButton; 22 import javax.swing.JFrame; 23 import javax.swing.JMenu; 24 import javax.swing.JMenuBar; 25 import javax.swing.JMenuItem; 26 import javax.swing.JOptionPane; 27 import javax.swing.JPanel; 28 import javax.swing.KeyStroke; 29 import java.net.URL; 30 31 32 class J_JPanel extends JPanel 33 { 34 /** 35 * 36 */ 37 private static final long serialVersionUID = 1L; 38 Image m_image; 39 int showpicture=0; 40 @SuppressWarnings("static-access") 41 public J_JPanel()throws IOException 42 { J_Puzzle a = new J_Puzzle(); 43 if(a.changetime==-1) 44 showpicture = 1; 45 //File f = new File("src/"+"样图"+showpicture+".jpg"); 46 URL is= this.getClass().getResource("sample"+showpicture+".jpg"); 47 new ImageIcon(is); 48 m_image = ImageIO.read(is); 49 } 50 public void paintComponent(Graphics g) 51 { 52 g.drawImage(m_image, 0, 0, 360, 360, this); 53 } 54 55 56 } 57 public class J_Puzzle extends JFrame implements ActionListener 58 { 59 /** 60 * 61 */ 62 private static final long serialVersionUID = 1L; 63 int i,j; 64 static int changetime = 0; 65 Container c = getContentPane(); 66 JButton b[] = new JButton[16]; 67 ImageIcon ic[][] = new ImageIcon[2][15]; 68 public J_Puzzle() throws IOException 69 { 70 71 super("拼图小游戏"); 72 73 String pic_name[] = new String[15]; 74 for(i = 0;i < 2;i ++) 75 for(j = 0;j < 15;j ++) 76 { 77 pic_name[j] = String.valueOf(j+1+i*15)+".jpg"; 78 URL is= this.getClass().getResource(pic_name[j]); 79 ic[i][j] = new ImageIcon(is); 80 } 81 82 JMenuBar mBar = new JMenuBar(); 83 setJMenuBar(mBar); 84 85 int k = 0; 86 87 JMenu []m = {new JMenu("菜单(M)"),new JMenu("帮助(H)")}; 88 char mC[][] = {{'M','H'},{'S','X','C','Z'},{'E','T'}}; 89 JMenuItem mItem[][] = {{new JMenuItem("开始(S)"),new JMenuItem("重置(X)"),new JMenuItem("背景更换(C)"),new JMenuItem("退出(Z)")},{new JMenuItem("查看样图(E)"),new JMenuItem("关于(T)")}}; 90 for(i = 0;i < 2;i ++) 91 { 92 mBar.add(m[i]); 93 m[i].setMnemonic(mC[0][i]); 94 if(i==0)k = 0; 95 else k = 1; 96 for(j = 0;j < 4-i-k;j ++) 97 { 98 m[i].add(mItem[i][j]); 99 mItem[i][j].setMnemonic(mC[i+1][j]); 100 mItem[i][j].setAccelerator(KeyStroke.getKeyStroke("ctrl"+mC[i+1][j])); 101 mItem[i][j].addActionListener(new ActionListener(){ 102 103 public void actionPerformed(ActionEvent e) 104 { 105 JMenuItem mItem = (JMenuItem)e.getSource(); 106 if(mItem.getText().equalsIgnoreCase("重置(X)")||mItem.getText().equalsIgnoreCase("开始(S)")) 107 { 108 int location[][] = {{17,13},{17,103},{17,193},{17,283},{107,13},{107,103},{107,193},{107,283} 109 ,{197,13},{197,103},{197,193},{197,283},{287,13},{287,103},{287,193},{287,283}}; 110 int rd_number[] = new int[16]; 111 rd_number = randomnumber(); 112 for(i = 1;i < 16;i ++) 113 b[i].setLocation(location[rd_number[i-1]-1][0],location[rd_number[i-1]-1][1]); 114 } 115 else if(mItem.getText().equalsIgnoreCase("背景更换(C)")) 116 { 117 118 changetime++; 119 for(i = 0;i < 15;i ++) 120 { 121 b[i+1].setIcon(null); 122 b[i+1].setIcon(ic[changetime][i]); 123 } 124 if(changetime==1) 125 changetime = -1; 126 } 127 else if(mItem.getText().equalsIgnoreCase("退出(Z)")) 128 { 129 int a = JOptionPane.showConfirmDialog(null, "您确定退出游戏?"); 130 if(a==0) 131 System.exit(0); 132 } 133 else if(mItem.getText().equalsIgnoreCase("查看样图(E)")) 134 { 135 136 JFrame jj = new JFrame("样图"); 137 jj.setSize(360, 360); 138 jj.setLocation(Toolkit.getDefaultToolkit().getScreenSize().width/3-360, 139 Toolkit.getDefaultToolkit().getScreenSize().height/4); 140 jj.setVisible(true); 141 Container c1 = jj.getContentPane(); 142 143 try { 144 c1.add(new J_JPanel(),BorderLayout.CENTER); 145 } 146 catch (IOException e1) { 147 // TODO Auto-generated catch block 148 e1.printStackTrace(); 149 } 150 } 151 if(mItem.getText().equalsIgnoreCase("关于(T)")) 152 { 153 JOptionPane.showMessageDialog(null, "简单拼图小游戏\n制作人:菜鸟"); 154 } 155 156 } 157 158 }); 159 } 160 161 } 162 m[0].insertSeparator(1);m[1].insertSeparator(1); 163 164 GridBagLayout gr = new GridBagLayout(); 165 c.setLayout(gr); 166 int gx[] = {0,1,2,3}; 167 int gy[] = {0,1,2,3}; 168 int k1; 169 Dimension d = new Dimension(90,90); 170 String s_number; 171 GridBagConstraints gc = new GridBagConstraints(); 172 for(i = 1;i < 5;i ++) 173 { 174 if(i==4)k1=4;//避免对最后一个按钮的操作 175 else k1=5; 176 for(j = 1;j < k1;j ++) 177 { 178 gc.gridx = gx[j-1]; 179 gc.gridy = gy[i-1]; 180 gc.fill = GridBagConstraints.NONE; 181 s_number = String.valueOf(j+(i-1)*4); 182 b[j+(i-1)*4] = new JButton(s_number,ic[0][j+(i-1)*4-1]); 183 b[j+(i-1)*4].setPreferredSize(d); 184 b[j+(i-1)*4].setFont(new Font("宋体",Font.PLAIN,0)); 185 gr.setConstraints(b[j+(i-1)*4],gc); 186 c.add(b[j+(i-1)*4]); 187 } 188 } 189 for(i = 1;i <16;i ++) 190 b[i].addActionListener(this); 191 } 192 public void actionPerformed(ActionEvent e) 193 { 194 int j; 195 JButton b = (JButton)e.getSource(); 196 Point p = b.getLocation(); 197 //System.out.print(p.x + " "); 198 //System.out.println(p.y); 199 Point p1 = null; 200 for(j = -1;j < 2;j ++) 201 { 202 if(p.y+j*90>283||p.y+j*90<13)//检测y方向前一个空格,当前空格,及后一个空格是否出界 203 continue; 204 else 205 { 206 Component a = c.getComponentAt(p.x, p.y+j*90); 207 if(a.getHeight()!=90)//在y方向,检测是不是空白区。如果是空白区,则a为整个画板 208 { 209 //System.out.println(a.getHeight()); 210 p1 = new Point(p.x,p.y+j*90); 211 } 212 } 213 } 214 for(j = -1;j < 2;j ++) 215 { 216 if(p.x+j*90>287||p.x+j*90<17) 217 continue; 218 219 else 220 { 221 Component a = c.getComponentAt(p.x+j*90, p.y); 222 if(a.getHeight()!=90) 223 p1 = new Point(p.x+j*90,p.y); 224 } 225 } 226 if(p1!=null) 227 b.setLocation(p1.x, p1.y);//改变按键的位置 228 if(check()==true) 229 JOptionPane.showMessageDialog(null, "恭喜您成功了"); 230 } 231 /*产生随机数*/ 232 public int[] randomnumber() 233 { 234 Random rd = new Random(); 235 int n[] = new int[15]; 236 for(int i = 0 ;i < 15;i ++) 237 { 238 int temp = rd.nextInt(15)+1; 239 n[i] = temp; 240 for(int j = 0;j < i;j ++) 241 if(n[j]==temp) 242 { 243 i--; 244 break; 245 } 246 } 247 return n; 248 } 249 /*判断是否排序成功*/ 250 public boolean check() 251 { 252 Point location[] = new Point[16]; 253 boolean bo = false; 254 int count=0; 255 for(int i = 0;i < 4;i ++) 256 for(int j = 0;j < 4;j ++) 257 location[i*4+j] = new Point(17+j*90, 13+i*90); 258 259 for(int i = 0;i < 15;i ++) 260 { 261 if(b[i+1].getLocation().x==location[i].x&&b[i+1].getLocation().y==location[i].y) 262 count++; 263 if(count==15) 264 bo=true; 265 } 266 return bo; 267 } 268 public static void main(String args[]) throws IOException 269 { 270 J_Puzzle app = new J_Puzzle(); 271 app.setDefaultCloseOperation(EXIT_ON_CLOSE); 272 app.setLocation(Toolkit.getDefaultToolkit().getScreenSize().width/3, Toolkit.getDefaultToolkit().getScreenSize().height/4); 273 app.setSize(400,440); 274 app.setVisible(true); 275 app.setResizable(false); 276 } 277 }