java拼图游戏代码_第1页
java拼图游戏代码_第2页
java拼图游戏代码_第3页
java拼图游戏代码_第4页
java拼图游戏代码_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Puzzle类:package com.eavan;public class Puzzle public static void main(String args) / TODO Auto-generated method stubnew PuzzleWin();PuzzleWin类:/* * 可以为游戏添加一个计时的功能,让时间成为一个判定标准 * 可以分析一下为什么图片不清楚 * 可以向怎么能够让选择图片和选择难度没有顺序性(较容易) * */package com.eavan;import java.awt.Color;import java.awt.FileDialog;import

2、java.awt.Graphics;import java.awt.GraphicsConfiguration;import java.awt.GraphicsDevice;import java.awt.GraphicsEnvironment;import java.awt.GridLayout;import java.awt.HeadlessException;import java.awt.Image;import java.awt.Transparency;import java.awt.event.ActionEvent;import java.awt.event.ActionLis

3、tener;import java.awt.image.BufferedImage;import java.util.ArrayList;import java.util.Random;import javax.swing.BorderFactory;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.

4、swing.JMenuItem;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JTextArea;public class PuzzleWin extends JFrame implements ActionListenerint dif = 0; /设置难度级数int k = 0; /标记是随机数list中的第几个随机数(从而用于标记图像list中的第几个图片)String filename="a.jpg" /设置的图片打开路径。默认路径其实是没用的int step

5、= 0; /用于记录总共用了多少步完成拼图JMenuBar mBar = new JMenuBar();JMenu jmSysten = new JMenu("系统");JMenu jmGame = new JMenu("游戏");JMenuItem restart = new JMenuItem("重新开始");JMenuItem quit = new JMenuItem("退出");JMenuItem choosepic = new JMenuItem("选择图片");JMenu choos

6、edif = new JMenu("选择难度");JMenuItem easy = new JMenuItem("3*3");JMenuItem hard = new JMenuItem("4*4");JPanel mainPanel = new JPanel();JButton btn = null; /用于显示被分割的图片ImageIcon checkIcon = null; /用于存放一个正确顺序放置被分割后的图片的数组,最后与btn的icon对比检测是否完成拼图JLabel piclab = new JLabel(); /用于

7、显示对照图片JLabel namelab = new JLabel("对照图片:"); /用于在对照图片上面给出提醒JLabel steplab = new JLabel(); /用于记录步数信息 JTextArea helpArea = new JTextArea(); /用于显示操作提示信息JLabel designLabel = new JLabel(); /用于显示者设计信息public PuzzleWin()this.setTitle("拼图游戏");this.setSize(600, 500);this.setLayout(null);thi

8、s.setLocation(200,120);this.setResizable(false); /因使用的大多是绝对布局,还是不要更改窗体大小吧this.setVisible(true);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);/添加menu,用于显示各种菜单mBar.add(jmSysten);mBar.add(jmGame);setJMenuBar(mBar);jmSysten.add(restart);jmSysten.add(quit);jmGame.add(choosedif);jmGame.add(choosepic

9、);choosedif.add(easy);choosedif.add(hard);/设置分割图片显现的位置,使用的都是绝对布局mainPanel.setBounds(30,30,360,360);mainPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK);this.add(mainPanel);/设置对照图片的大小坐标(用一个label作为盛放pic的容器)/显示对照图片namelab.setBounds(450, 10, 80, 15);steplab.setBounds(450, 160, 80, 15);piclab.

10、setBorder(BorderFactory.createLineBorder(Color.BLACK);steplab.setText("步数:"+step);piclab.setBounds(450, 30, 110, 110);helpArea.setBounds(420, 220, 150, 120);helpArea.setBackground(new Color(192, 192, 192);helpArea.setText("由于本人水平有限,请按照rn先在游戏菜单中选择游戏难rn度,再选择图片的顺序进行rn游戏。rnrn祝玩得开心-.-"

11、;);helpArea.setEditable(false);designLabel.setBounds(30, 370, 200, 100);designLabel.setText("Design By Eavan In Haust");this.add(namelab);this.add(piclab);this.add(steplab);this.add(helpArea);this.add(designLabel);/对menu里的各种按钮注册监听restart.addActionListener(this);quit.addActionListener(this)

12、;choosepic.addActionListener(this);easy.addActionListener(this);hard.addActionListener(this);/实现从Image对象转化为BufferedImage对象的方法public static BufferedImage toBufferedImage(Image image) if (image instanceof BufferedImage) return (BufferedImage)image; image = new ImageIcon(image).getImage(); BufferedImag

13、e bimage = null; GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); try int transparency = Transparency.OPAQUE; GraphicsDevice gs = ge.getDefaultScreenDevice(); GraphicsConfiguration gc = gs.getDefaultConfiguration(); bimage = gc.createCompatibleImage( image.getWidth(null),

14、image.getHeight(null), transparency); catch (HeadlessException e) if (bimage = null) int type = BufferedImage.TYPE_INT_RGB; bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type); Graphics g = bimage.createGraphics(); g.drawImage(image, 0, 0, null); g.dispose(); return bimage;

15、/实现将图片分割的方法,返回的对象是一个存放Image对象的数组。public ArrayList<Image> splitImage(ImageIcon i,int n)Image im = i.getImage();BufferedImage bi = toBufferedImage(im);int baseHeight = bi.getHeight();int basewidth = bi.getWidth();int x = 0;int y = 0;ArrayList<Image> image = new ArrayList<Image>();if

16、(n = 3) int a = basewidth/n;int b = baseHeight/n;for (int j = 0; j < n*n; j+) Image tempImage = bi.getSubimage(x, y, a, b);image.add(tempImage);x = x+a;if(x = 3*a) x = 0; y = y+b; /int j = 0;/while (j<n*n) /image.add(bi.getSubimage(x, y, a, b);/x = x+a;/if(x = 3*a)/x = 0;/y = y+b;/if (n = 4) i

17、nt a = basewidth/n;int b = baseHeight/n;for (int j = 0; j < n*n; j+) Image tempImage = bi.getSubimage(x, y, a, b);image.add(tempImage);x = x+a;if(x = n*a) x = 0; y = y+b; return image;/产生一个长度为n的不重复随机数arraylistpublic ArrayList<Integer> randomNum(int n) ArrayList<Integer> list = new Arr

18、ayList<Integer>(); Random rand = new Random(); boolean bool = new booleann; int num =0; for (int i = 0; i<n; i+) do /如果产生的数相同继续循环 num = rand.nextInt(n); while(boolnum); boolnum =true; list.add(num); return list;public boolean isOver()int mark = 0;boolean b = false;for (int i = 0; i < dif

19、; i+) for (int j = 0; j < dif; j+) if (i = dif-1&&j = dif-1) break;if (btnij.getIcon() = checkIconij) mark+;if (mark = dif*dif-1) b = true;return b;public void actionPerformed(ActionEvent e) / TODO Auto-generated method stubif (e.getSource()=restart)dif = 0; k = 0; step = 0;filename="

20、;a.jpg"for (int i = 0; i < 3; i+) for (int j = 0; j < 3; j+) btnij = null;mainPanel.removeAll();piclab.setIcon(null);steplab.setText("步数:"+step);repaint();else if (e.getSource()=quit) System.exit(0);else if (e.getSource()=choosepic) /实现图片选择功能FileDialog df=new FileDialog(this,&qu

21、ot;图片选择",FileDialog.LOAD);df.setVisible(true);if(df.getFile()=null)return;/获取文件的路径filename=df.getDirectory()+df.getFile();/文件路径+文件名/System.out.println(filename); /测试路径是否正确ImageIcon image = new ImageIcon(filename);ImageIcon labImage = image;labImage.setImage(image.getImage().getScaledInstance(11

22、0, 110, Image.SCALE_DEFAULT);piclab.setIcon(labImage);if (dif = 3) ArrayList<Image> iList = splitImage(image, 3);/实际上只会用到list中的前8张图片,故只需产生07这八个数就行ArrayList<Integer> numList = randomNum(8);ImageIcon tempIcon = new ImageIcon8;checkIcon = new ImageIcon33; for (int j = 0; j < 3; j+)for (i

23、nt i = 0; i < 3; i+) /用于消除数组越界异常:这里的btn会有9个,而image只有8个,故最后一个btn是没有对象可以给它赋图片的。/所以当最后一个ImageIcon对象要出来时,breakif(i = 2&&j = 2) break; checkIconij = new ImageIcon(iList.get(k).getScaledInstance(120, 120, Image.SCALE_AREA_AVERAGING); tempIconk = checkIconij; / ImageIcon changedIcon = null;/ cha

24、ngedIcon = new ImageIcon33;/ ImageIcon smallIcon = new ImageIcon(iList.get(numList.get(k).getScaledInstance(120, 120, Image.SCALE_AREA_AVERAGING);/ ImageIcon changedIcon = new ImageIcon(iList.get(k).getScaledInstance(120, 120, Image.SCALE_AREA_AVERAGING);/btnij.setIcon(changedIconij);/btnij.setIcon(

25、changedIcon);/changedIcon = null; k = k+1;int t = 0;for (int j = 0; j < 3; j+)for (int i = 0; i < 3; i+) if(i = 2&&j = 2) break;btnij.setIcon(tempIconnumList.get(t);t+;if (dif = 4) ArrayList<Image> iList = splitImage(image, 4);/实际上只会用到list中的前8张图片,故只需产生07这八个数就行ArrayList<Integer

26、> numList = randomNum(15);ImageIcon tempIcon = new ImageIcon15;checkIcon = new ImageIcon44;for (int j = 0; j < 4; j+)for (int i = 0; i < 4; i+) /用于消除数组越界异常:这里的btn会有16个,而image只有15个,故最后一个btn是没有对象可以给它赋图片的。/所以当最后一个ImageIcon对象要出来时,breakif(i = 3&&j = 3) break; checkIconij = new ImageIcon(

27、iList.get(k).getScaledInstance(90, 90, Image.SCALE_AREA_AVERAGING); tempIconk = checkIconij; / ImageIcon changedIcon = null;/ changedIcon = new ImageIcon33;/ ImageIcon smallIcon = new ImageIcon(iList.get(numList.get(k).getScaledInstance(120, 120, Image.SCALE_AREA_AVERAGING);/ ImageIcon changedIcon =

28、 new ImageIcon(iList.get(k).getScaledInstance(120, 120, Image.SCALE_AREA_AVERAGING);/btnij.setIcon(changedIconij);/btnij.setIcon(changedIcon);/changedIcon = null; k = k+1;int t = 0;for (int j = 0; j < 4; j+)for (int i = 0; i < 4; i+) if(i = 3&&j = 3) break;btnij.setIcon(tempIconnumList

29、.get(t);t+;else if (e.getSource()=easy) dif = 3; btn = new JButton33;/mainPanel.setLayout(new GridLayout(3,3);mainPanel.setLayout(null);for (int i = 0; i < 3; i+) for (int j = 0; j < 3; j+) btnij = new JButton(); btnij.setBounds(i*120, j*120, 120, 120); btnij.setBackground(Color.WHITE);/将按钮添加到

30、mainpanel面板上。for (int i = 0; i < 3; i+) for (int j = 0; j < 3; j+) mainPanel.add(btnij);btnij.addActionListener(this);repaint(); /利用reapint()方法重回mainpanel,使得新加的按钮显示出来 else if (e.getSource()=hard) dif = 4; btn = new JButton44;/mainPanel.setLayout(new GridLayout(3,3);mainPanel.setLayout(null);for (int i = 0; i < 4; i+) for (int j = 0; j < 4; j+) btnij = new JButton(); btnij.setBounds(i*90, j*90, 90, 90); btnij.setBackground(Color.WHITE);/将按钮添加到mainpanel面板上。for (int i = 0; i < 4; i+) for (int j = 0; j < 4; j+) mainPanel.add(btnij);btnij.addActionListener(this);repaint();else fo

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论