拼图小游戏,学习阶段。很多不足,改进了一下
Photo de :
J_Puzzle.java
importer java.awt.BorderLayout; importer java.awt.Component; importer java.awt.Container; importer java.awt.Dimension; importer java.awt.Font; importer java.awt.Graphics; importer java.awt.GridBagConstraints; importer java.awt.GridBagLayout; importer java.awt.Image; importer java.awt.Point; importer java.awt.event.ActionEvent; importer java.awt.event.ActionListener; importer java.awt.Toolkit; importer java.io.File; importer java.io.IOException; importer java.util.Random; importer javax.imageio.ImageIO; importer javax.swing.ImageIcon; importer javax.swing.JButton; importer javax.swing.JFrame; importer javax.swing.JMenu; importer javax.swing.JMenuBar; importer javax.swing.JMenuItem; importer javax.swing.JOptionPane; importer javax.swing.JPanel; importer javax.swing.KeyStroke;class J_JPanel extends JPanel{ /** * */ private static final long serialVersionUID = 1L; Image m_image ; int showpicture=0; @SuppressWarnings("static-access") public J_JPanel()throws IOException { J_Puzzle a = new J_Puzzle(); if(a.changetime==-1) showpicture = 1; Fichier f = new File("样图"+showpicture+".jpg"); m_image = ImageIO.read(f); } public void paintComponent(Graphics g) { g.drawImage(m_image, 0, 0, 360, 360, this); } }classe publique J_Puzzle étend JFrame implémente ActionListener{ /** * */ private static final long serialVersionUID = 1L; int je,j; temps de changement int statique = 0 ; Conteneur c = getContentPane(); JButton b[] = nouveau JButton[16]; ImageIcon ic[][] = new ImageIcon[2][15]; public J_Puzzle() throws IOException { super("拼图小游戏"); Chaîne pic_name[] = nouvelle chaîne[15]; for(i = 0;i < 2;i ++) for(j = 0;j < 15;j ++) { pic_name[j] = String.valueOf(j+1+i*15)+".jpg " ; ic[i][j] = new ImageIcon(pic_name[j]); } JMenuBar mBar = nouveau JMenuBar(); setJMenuBar(mBar); entier k = 0 ; JMenu []m = {new JMenu("菜单(M)"),new JMenu("帮助(H)")}; char mC[][] = {{'M','H'},{'S','X','C','Z'},{'E','T'}}; JMenuItem mItem[][] = {{new JMenuItem("开始(S)"),new JMenuItem("重置(X)"),new JMenuItem("背景更换(C)"),new JMenuItem("退出( Z)")},{new JMenuItem("查看样图(E)"),new JMenuItem("关于(T)")}}; pour(i = 0;i < 2;i ++) { mBar.add(m[i]); m[i].setMnemonic(mC[0][i]); si(i==0)k = 0; sinon k = 1 ; for(j = 0;j < 4-ik;j ++) { m[i].add(mItem[i][j]); mItem[i][j].setMnemonic(mC[i+1][j]); mItem[i][j].setAccelerator(KeyStroke.getKeyStroke("ctrl"+mC[i+1][j])); mItem[i][j].addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { JMenuItem mItem = (JMenuItem)e.getSource(); if(mItem.getText().equalsIgnoreCase("重置(X )")||mItem.getText().equalsIgnoreCase("开始(S)")) { emplacement int[][] = {{17,13},{17,103},{17,193},{17,283},{107,13},{107,103},{107,193},{107,283} ,{197,13},{197,103},{197,193},{197,283},{287,13},{287,103},{287,193},{287,283}} ; = nombre aléatoire (); pour (i = 1; i < 16;i ++) b[i].setLocation(location[rd_number[i-1]-1][0],location[rd_number[i-1]-1][1] } else if(mItem. getText().equalsIgnoreCase("背景更换(C)")) { changetime++; for(i = 0;i < 15;i ++) { b[i+1].setIcon(null); b[i+1].setIcon(ic[changetime][i]); if(changetime==1) changetime = -1; ().equalsIgnoreCase("出(Z)")) { int a = JOptionPane.showConfirmDialog(null, "您确定退出游戏?"); if(a==0) System.exit(0); else if(mItem.getText().equalsIgnoreCase("查看样图(E)")) { JFrame jj = new JFrame("样图"); jj.setSize(360, 360); jj.setLocation(Toolkit.getDefaultToolkit().getScreenSize().width/3-360, Toolkit.getDefaultToolkit().getScreenSize().height/4); jj.setVisible(true); Conteneur c1 = jj.getContentPane(); essayez { c1.add(new J_JPanel(),BorderLayout.CENTER); } catch (IOException e1) { // TODO Bloc catch généré automatiquement e1.printStackTrace(); } } if(mItem.getText().equalsIgnoreCase("关于(T)")) { JOptionPane.showMessageDialog(null, "简单拼图小游戏/n制作人:菜鸟"); } } }); } } m[0].insertSeparator(1);m[1].insertSeparator(1); GridBagLayout gr = new GridBagLayout(); c.setLayout(gr); int gx[] = {0,1,2,3}; int gy[] = {0,1,2,3}; entier k1 ; Dimension d = nouvelle Dimension (90,90) ; Chaîne s_number ; GridBagConstraints gc = new GridBagConstraints(); pour(i = 1;i < 5;i ++) { if(i==4)k1=4; sinon k1=5 ; pour(j = 1;j < k1;j ++) { gc.gridx = gx[j-1]; gc.gridy = gy[i-1]; gc.fill = GridBagConstraints.NONE; s_number = String.valueOf(j+(i-1)*4); b[j+(i-1)*4] = nouveau JButton(s_number,ic[0][j+(i-1)*4-1]); b[j+(i-1)*4].setPreferredSize(d); b[j+(i-1)*4].setFont(new Font("宋体",Font.PLAIN,0)); gr.setConstraints(b[j+(i-1)*4],gc); c.add(b[j+(i-1)*4]); } } for(i = 1;i <16;i ++) b[i].addActionListener(this); } public void actionPerformed(ActionEvent e) { int j; JButton b = (JButton)e.getSource(); Point p = b.getLocation(); Point p1 = nul ; for(j = -1;j < 2;j ++) { if(p.y+j*90>283||p.y+j*90<13) continuer ; else { Composant a = c.getComponentAt(px, p.y+j*90); if(a.getHeight()!=90) p1 = new Point(px,p.y+j*90); } } for(j = -1;j < 2;j ++) { if(p.x+j*90>287||p.x+j*90<17) continuer; else { Composant a = c.getComponentAt(p.x+j*90, py); if(a.getHeight()!=90) p1 = new Point(p.x+j*90,py); } } if(p1!=null) b.setLocation(p1.x, p1.y); if(check()==true) JOptionPane.showMessageDialog(null, "恭喜您成功了"); }/*产生随机数*/ public int[] randomnumber() { Random rd = new Random(); int n[] = nouveau int[15]; for(int i = 0 ;i < 15;i ++) { int temp = rd.nextInt(15)+1; n[i] = temp; for(int j = 0;j < i;j ++) if(n[j]==temp) { i--; casser; } } renvoie n; } /*判断是否排序成功*/ public boolean check() { Point location[] = new Point[16]; booléen bo = faux ; nombre int = 0 ; pour(int i = 0;i < 4;i ++) for(int j = 0;j < 4;j ++) emplacement[i*4+j] = nouveau Point(17+j*90, 13+ je*90); for(int i = 0;i < 15;i ++) { if(b[i+1].getLocation().x==location[i].x&&b[i+1].getLocation().y= =emplacement[i].y) count++; if(count==15) bo=true ; } retourne bo; } public static void main(String args[]) lance IOException { J_Puzzle app = new J_Puzzle(); app.setDefaultCloseOperation(EXIT_ON_CLOSE); app.setLocation (Toolkit.getDefaultToolkit().getScreenSize().width/3, Toolkit.getDefaultToolkit().getScreenSize().height/4); app.setSize(400 440); app.setVisible(true); app.setResizing(false); }}
以上所述就是本文的全部内容了,希望对大家学习java能够有所帮助。