魔方 3D 模型

来源:互联网 发布:淘宝搜索首页前三位置 编辑:程序博客网 时间:2024/04/29 00:26

一段时间,刚毕业的时候,想写一个可以玩的魔方3D模型,隔了很久都没有怎么精力去写,总以为这是要消耗挺多时间精力,去年一段时间,在周末时间还是难耐的想去实现自己的想法,最后用java swing写了一个简单的模型。

这边就记录下来。

当然现在已经是写好了,完事后,只是放在硬盘上一直都没有再去动,应为想安装个linux系统,这边准备格式化硬盘,就想着把他放到网上去,因为太懒,这边只是附上简单的东西。

当时的想法,整个模型的立体旋转,子模块的旋转,然后磁性粘合。

这边通过点击来进行整体的模块和子模块旋转的切换,旋转是一直按住鼠标左键滑动实现。

子模块的旋转,先单击整个模块,将鼠标移动到制定要滑动的子模块,然后程序会根据你滑动的方向判断要旋转的子模块。

如果再单击鼠标,又切换到整个模块的旋转,尽量可以人性化的去玩弄魔方:


最初的想法都是一样的,创建子模块,组建成一个整体。

代码是java实现,main方法在test.java中。

代码下载:http://download.csdn.net/detail/qungxue/9661698

代码没有分级,都是放在同一个包下面:


其中的思想是,一个魔方WholeCubeModel.java对象,有27块小方块(Cube.java)组成,LittleSquare.java就是小方块组装对象,每个小方块又有6个面组成,没个面抽象出来成SubFace.java,这边Axis3D.java是控件坐标点对象。RoateModel.java模型,主要是处理魔方的一些旋转和自动磁性粘合操作,CubeComparator.java是一个比较器,为了魔方旋转的时候旋转的人性化和吻合人类视觉感官对画出的模型组建进行排序。

这边附上SubFace.java的代码:

package com.ake.magiccube;import java.awt.Color;import java.awt.Polygon;/** * just write one class that for sub cube, and this properties of  * cube contains for points and one color. * if you want to ask me why this extends Polygon, it is just because  * I want to paint it on the panel. * @author Administrator * */public class SubFace{// define the face is exposing or not private boolean isExpose;//define the colorprivate Color color;//add four point of this subfaceprivate Axis3D[] points;//set center pointprivate Axis3D center;//add the polygon private Polygon polygon;//子立方块的边长大小public static double size=100;//标志是否是内部面private boolean isInside;public boolean isInside() {return isInside;}public void setInside(boolean isInside) {this.isInside = isInside;}public SubFace(){//set null color;isExpose = true;color = null;polygon= new Polygon();center = new Axis3D(0,0,0);}public Axis3D[] getPoints() {return points;}public void setPoints(Axis3D[] points) {this.points = points;center.setValue((points[0].getX()+points[2].getX())/2,(points[0].getY()+points[2].getY())/2,(points[0].getZ()+points[2].getZ())/2);this.flushPolygon();}public Polygon getPolygon() {return polygon;}public void setPolygon(Polygon polygon) {this.polygon = polygon;}public boolean isExpose() {return isExpose;}public void setExpose(boolean isExpose) {this.isExpose = isExpose;}public Color getColor() {return color;}public void setColor(Color color) {this.color = color;}//每次更新四个定点的坐标时候,记得更新多边形的信息。public void flushPolygon(){//如果多边形的长度大于4的话,我们就重置if (polygon.npoints>=4) {polygon.reset();}//添加多边形的边for (int i = 0; i < points.length; i++) {polygon.addPoint((int)points[i].getX(), (int)points[i].getY());//System.out.println("Polygon flush:["+points[i].getX()+","+points[i].getY()+"]");}}public Axis3D getCenter() {//center.setValue((points[0].getX()+points[2].getX())/2,(points[0].getY()+points[2].getY())/2,(points[0].getZ()+points[2].getZ())/2);return center;}public void setCenter(Axis3D center) {this.center = center;}}



0 0