服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服务器之家 - 编程语言 - JAVA教程 - java实现简单美女拼图游戏

java实现简单美女拼图游戏

2019-12-14 15:07hebedich JAVA教程

这篇文章主要介绍了java实现简单美女拼图游戏的相关资料,需要的朋友可以参考下

拼图小游戏,学习阶段。很多不足,改进了一下

演示图片

java实现简单美女拼图游戏

J_Puzzle.java

?
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
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Toolkit;
import java.io.File;
import java.io.IOException;
import java.util.Random;
 
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import 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;
      File f = new File("样图"+showpicture+".jpg");
      m_image = ImageIO.read(f);  
  }
  public void paintComponent(Graphics g)
  {     
    g.drawImage(m_image, 0, 0, 360, 360, this);
  }
}
public class J_Puzzle extends JFrame implements ActionListener
{
  /**
   *
   */
  private static final long serialVersionUID = 1L;
  int i,j;
  static int changetime = 0;
  Container c = getContentPane();
  JButton b[] = new JButton[16];
  ImageIcon ic[][] = new ImageIcon[2][15];
  public J_Puzzle() throws IOException
  {
       
    super("拼图小游戏");
     
    String pic_name[] = new String[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 = new JMenuBar();
    setJMenuBar(mBar);
     
    int 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)")}};
    for(i = 0;i < 2;i ++)
     {
      mBar.add(m[i]);
      m[i].setMnemonic(mC[0][i]);
      if(i==0)k = 0;
      else k = 1;
      for(j = 0;j < 4-i-k;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)"))
             {              
            int location[][] = {{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}};
            int rd_number[] = new int[16];        
            rd_number = randomnumber();
            for(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;
            }
            else if(mItem.getText().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);
                Container c1 = jj.getContentPane();
                 
                try {
                  c1.add(new J_JPanel(),BorderLayout.CENTER);
                }
                catch (IOException e1) {
                  // TODO Auto-generated catch block
                  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};
    int k1;
    Dimension d = new Dimension(90,90);
    String s_number;
    GridBagConstraints gc = new GridBagConstraints();
    for(i = 1;i < 5;i ++)
     {
      if(i==4)k1=4;
      else k1=5;
      for(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] = new 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 = null;
        for(j = -1;j < 2;j ++)
        {          
          if(p.y+j*90>283||p.y+j*90<13)
            continue;
          else
          {
            Component a = c.getComponentAt(p.x, p.y+j*90);
            if(a.getHeight()!=90)
              p1 = new Point(p.x,p.y+j*90);
          }  
        }
        for(j = -1;j < 2;j ++)
        {  
          if(p.x+j*90>287||p.x+j*90<17)
            continue;
                   
          else
          {
            Component a = c.getComponentAt(p.x+j*90, p.y);
            if(a.getHeight()!=90)       
               p1 = new Point(p.x+j*90,p.y);
          }       
        }
        if(p1!=null)
        b.setLocation(p1.x, p1.y);
       if(check()==true)
         JOptionPane.showMessageDialog(null, "恭喜您成功了");
   }
/*产生随机数*/
  public int[] randomnumber()
  {
     Random rd = new Random();
      int n[] = new 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--;
            break;
          }            
      }
    return n;
  }
  /*判断是否排序成功*/
  public boolean check()
  {
     Point location[] = new Point[16];
     boolean bo = false;
     int count=0;
     for(int i = 0;i < 4;i ++)
       for(int j = 0;j < 4;j ++)
        location[i*4+j] = new Point(17+j*90, 13+i*90);
     
     for(int i = 0;i < 15;i ++)
     {
       if(b[i+1].getLocation().x==location[i].x&&b[i+1].getLocation().y==location[i].y)
         count++;
       if(count==15)
         bo=true;
     }   
    return bo;
  }
   
  public static void main(String args[]) throws 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.setResizable(false);
  }
}

以上所述就是本文的全部内容了,希望对大家学习java能够有所帮助。

延伸 · 阅读

精彩推荐