1、该程序共包含着几个类源文件: CreayImage.java,ActionEvent.java ,ImageIcon.java ,CountTime.java ,Resize.java ,CutImage.java包含接口:IDealImage.java , Direction.java.。其中CreayImage为主类,主要布局整个游戏界面的GUI,以及各类事件,ActionEvent类为一个侦听器用来处理鼠标点击事件,实现了MouseListener接口,ImageIcon类继承了javax.swing.ImageIcon类,里面定义了id属性,用于最后判断游戏是否胜利完成。CountTi
2、me类为一个线程类,实现java.lang.Runnable接口,用于游戏开始时计时用。Resize类是一个工具类,用于处理加载进来的图片,将其处理成制定大小weidth * height像素的图像,便于将处理后的图片加载相应的空间之上。CutImage类也是一个工具类,用于将Resize类处理后的图片进行切割成制定的大小图片组,并将切割好的图片存放于容器之中。接口IDealImage中定义了getImageList方法,由CutImage来实现,调用接口可以减低程序的耦合度,加强程序的灵活性。Direction接口中指定义了几个方向属性UP,DOWN,LEFT,RIGHT分别用于指定鼠标点击
3、label时,图片移动的方向。另外:源文件中还包含一个img.ini二进制文件,里面存放一个预先写进去的图片二进制数。用于程序一开始运行时,从中读取图片。结构图如下:四、 主类说明(1分)用表格的形式说明主类中各成员变量(成员变量的名称、变量类型、变量的描述)、方法(名称、功能、备注)。1、 CreayImage类 成员变量成员变量的名称变量类型变量的描述rowsInt模板游戏矩阵行数cols模板游戏矩阵列数width游戏矩阵列每个小图像的宽度height游戏矩阵列每个小图像的高度steps图像移动的步数pathString加载图像图像的路径imagelistArrayList存放被切割的小图
4、像的容器所用方法方法名称功能备注Init()初始化魔方游戏的界面部分initLabelImage()用于处理图像,把图像切割后并加载到panel容器的label上initLabelScan()把加载的原图像加载到魔方游戏的右下角的区域中actionPerformed()监听界面发出的操作指令,实现各种操作(比如点击开始,选择图片等)五、 其他各类的描述和说明。(1分)1、 ActionEvent变量cutRow鼠标点击的图像所在列cutCol鼠标点击的图像所在行图像的列数Colsdirection移动的方向方法ActionEvent(int rows,int cols)带参数的构造方法,用于传
5、递参数initStep()设置移动图像的标签mouseClicked()鼠标点击事件checkSuccess()检查图像是否与原图匹配的方法checkImageCanMove()检查图像是否可以移动moveLabelImage()设置图像移动方向moveUp()图像上移的方法moveDown()图像下移的方法moveLeft()图像左移的方法moveRight()图像右移的方法2、CutImagegetImageList()返回加载的图片切割后的图像列表2、 ResizeResize()Rize()重置图像大小的方法,把图像的大小更改为400*3003. CountTime成员变量:IsSuc
6、cessBoolean定义游戏是否胜利Time定义游戏所进行的时间Run游戏点击开始时开始计时4.Direction接口变量UP方向向上DOWN方向向下LEFT方向向左RIGHT方向向右六、 附主要源代码(分)CreayImage类中主要代码: 1.初始化,游戏主面板代码:public void initLabelImage() this.imagePane.removeAll(); /先除去面板上的控件 this.imagePane.setLayout(new GridLayout(this.getRows(),this.getCols(),1,1); /对面板进行重新布局 / 定义 标签移
7、动步数 num this.total = this.getRows() * this.getCols(); this.imageLabel = new JLabelthis.total; try IDealImage dealImage = new CutImage();/定义接口,实例化CutImage类 this.imageList = dealImage.getImageList(this.getPaht(), this.rows, this.cols, this.weight, this.height); catch (Exception e) e.printStackTrace();
8、 /调用方法,获得对图片进行分割后的小图片容器for (int i = 0; i this.rows; i+) for (int j = 0; j this.cols; j+) / 用双重循环,将图片加载到面板的Label上 ImageIcon imgIcon = new ImageIcon(this.imageList.get(i * cols + j); imgIcon.setId(i * cols + j); this.imageLabeli * this.cols + j = new JLabel(imgIcon); this.imageLabeli * this.cols + j.s
9、etSize(imgIcon .getIconWidth(), imgIcon.getIconHeight(); this.imagePane.add(this.imageLabeli * this.cols + j); /给没个label添加一个鼠标单击事件侦听 this.imageLabeli * this.cols + j .addMouseListener(new ActionEvent(this.imageLabel, this .getRows(), this.getCols(), i, j, this.steps, this.labelStep, this.btnStart, t
10、his.ct); this.imageLabelthis.total - 1.setIcon(null); 2单击“选择图片”按钮响应代码: JFileChooser fileChoser = new JFileChooser(); /弹出选择文件对话框 fileChoser.setFileSelectionMode(JFileChooser.FILES_ONLY); int result = fileChoser.showOpenDialog(null); if (result = JFileChooser.OPEN_DIALOG) this.paht = fileChoser.getSel
11、ectedFile().getAbsolutePath(); /获取选择文件的路径 this.initLabelScan(); /重新布局面板,更爱图片 this.initLabelImage(); /重新布局小图片浏览面板3.单击开始按钮代码:Random r = new Random(); /定义随机类变量 int ranNum1; int ranNum2; /循环打乱面板上的图片 for (int i = 0; 30; i+) ranNum1 = r.nextInt(rows * cols - 1); ranNum2 = r.nextInt(rows * cols - 1); if (r
12、anNum1 = ranNum2) i-; continue; ImageIcon ii = (ImageIcon) this.imageLabelranNum1 .getIcon(); this.imageLabelranNum1 .setIcon(ImageIcon) imageLabelranNum2.getIcon(); this.imageLabelranNum2.setIcon(ii); this.btnStart.setText(重来); this.btnChooseImage.setEnabled(false); this.labelStep.setVisible(true);
13、 this.labelStep.setText(0 System.out.println(); / this.ct = new CountTime(this.labelTime); / ct = new CountTime(this.labelTime); ct.setTime(0); ct.setSuccess(false); Thread t = new Thread(ct); t.start();4.Resize.java 调整图像像素大小的代码:BufferedImage bufTarget = null; double sx = (double) width / srcBufImag
14、e.getWidth(); double sy = (double) height / srcBufImage.getHeight(); int type = srcBufImage.getType(); if(type = BufferedImage.TYPE_CUSTOM) ColorModel cm = srcBufImage.getColorModel(); WritableRaster raster = cm.createCompatibleWritableRaster(width, height); boolean alphaPremultiplied = cm.isAlphaPr
15、emultiplied(); bufTarget = new BufferedImage(cm, raster, alphaPremultiplied, null); else bufTarget = new BufferedImage(width, height, type); Graphics2D g = bufTarget.createGraphics(); g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); g.drawRenderedImage(srcBufIma
16、ge, AffineTransform.getScaleInstance(sx, sy); g.dispose();5.切割图片部分代码:BufferedImage bufferedImg2 = ImageIO .read(new FileInputStream(path); /加载图片 BufferedImage bufferedImg = Resize.rize(bufferedImg2, 400, 300); width = bufferedImg.getWidth() / cols; height = bufferedImg.getHeight() / rows; for (int i
17、 = 0; rows; for (int j = 0; cols; /将切割的图片存放到容器中 imageList.add(bufferedImg.getSubimage(width * j, height * i, width, height);七、 小结差不多做完了这个实验,才感觉到这个游戏其实还挺有意思的,以前一直在网上玩游戏,从没想过自己来把这个游戏给做出来,现在回过头来,真的感觉很不一样,从中学到不少东西。玩游戏时感觉很简单,只要鼠标点点,开始,移动即可,在做的时候却遇到了不少的问题,比如把一个很大的图片加载到面板上,又如何将它的大小进行转化,以及后面如何将转化后图片进行切割,再到后面,如何点击图片时发生移动,怎么移动,最终判断游戏胜利,一路走来,感觉经历了一番,现在回过头来想想其实挺简单的,也感觉到一点成就感。这学期也快结束了,java的基础学习也意味着将暂告一段落,但我知道java的博大之处,还有很多比如j2ee,所以还有很多知识等着我们去学。
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1