java3D魔方程序的源代码的详细介绍
java3D魔方程序的源代码的详细介绍
1,本程序由2001级中南大学计算机科学与技术本科学生 李霄 所写,其他人可进行研究和学习。未经作者本人同意,任何人不得以任何理由将其中的任意一段代码用作任意与商业有关的行为。你可以进行二次开发,但在发布前要通过本人允许。
有需要请与作者本人联系,本人愿意作出回复和解答疑问。
2,本程序为放出的测试用魔方(MoFang.java),包含所有的核心代码及注释,调试,测试代码。你可以用鼠标转动魔方,用键盘上的方向键移动魔方位置,除此以外,代码中还有大量功能,如智能搜索求解魔方,目前正在制作图形控制界面,也可用于applet嵌入网页中,Frame,也可做成java bean组件,有java bean需要的,请与作者本人联系。
3,先双击运行Java 3D for Windows (OpenGL Version) SDK for the JDK (includes Runtime) (java3d-1_3_1-windows-i586-opengl-sdk.exe, 6.43 MB) .exe安装java3D开发环境,再双击start.bat开始运行,运行后敲击键盘开始30次随机转动,要他进行其他转动,可以加代码,或等待本人的最终图形用户界面版本。
4,本人目前是应届本科毕业生,希望有机会加入软件制作行业。联系电话:2005年6月以前联系电话0731-8710325,之后未定,QQ:77690139
//存为MoFang.java
import java.applet.Applet ;
import java.awt.*;
import com.sun.j3d.utils.applet.MainFrame ;
import java.awt.BorderLayout ;
import com.sun.j3d.utils.universe.SimpleUniverse ;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.behaviors.mouse.*;
import com.sun.j3d.utils.behaviors.keyboard.*;
import com.sun.j3d.utils.picking.behaviors.*;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.image.TextureLoader ;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame.*;
import javax.swing.*;
class mySimpleUniverse extends Applet
{
BranchGroup createSceneGraph(Canvas3D canvas)
{
//System.out.print("**1**");
//创建变换组,无用的t3D
Transform3D t3d=new Transform3D();
TransformGroup trans=new TransformGroup(t3d);
trans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
trans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
//创建分枝组
BranchGroup objRoot=new BranchGroup();
//测试
//SomeShape3D.book3D( this, trans);
SomeShape3D.addText3DDonghua(trans,"魔方",new Point3f(-7.0f,6.0f,6.0f),0.1f,new Color3f(1.0f,0.0f,0.0f),1);
//初始化数据结构
System.out.println("/n/n载入方块,并向变换组中加入每个方块的坐标系和方块...");
for(int i=0;i<=2;i++)
for(int j=0;j<=2;j++)
for(int k=0;k<=2;k++)
{
int[] p;
p=Position.getPxyzFromPositionAy(i,j,k,MoFang.positionArray);
MoFang.blockArray[i][j][k]=new Block(i,j,k,p[0],p[1],p[2],trans,t3d,objRoot,this);
}
System.out.println("加入每个方块的坐标系和方块,完成./n");
//创建大坐标轴,自动加到主坐标系
SomeShape3D.zuoBiaoZhuBigXShape3D(trans);
SomeShape3D.zuoBiaoZhuBigYShape3D(trans);
SomeShape3D.zuoBiaoZhuBigZShape3D(trans);
//创建边界对象
BoundingSphere bounds=new BoundingSphere(new Point3d(0.0,0.0,0.0),100);
//创建鼠标pick行为,加到分支组objRoot
PickRotateBehavior pickRotate=new PickRotateBehavior(objRoot,canvas,bounds);
PickTranslateBehavior pickTranslate=new PickTranslateBehavior(objRoot,canvas,bounds);
PickZoomBehavior pickZoom=new PickZoomBehavior(objRoot,canvas,bounds);
//objRoot.addChild(pickRotate);
objRoot.addChild(pickTranslate);
//objRoot.addChild(pickZoom);
//创建鼠标旋转行为
MouseRotate behavior=new MouseRotate();
behavior.setTransformGroup(trans);
behavior.setSchedulingBounds(bounds);
//创建鼠标平移行为
//MouseTranslate myMouseRotate=new MouseTranslate();
//myMouseRotate.setTransformGroup(trans);
//myMouseRotate.setSchedulingBounds(bounds);
//创建鼠标缩放行为
MouseZoom myMouseZoom=new MouseZoom();
myMouseZoom.setTransformGroup(trans);
myMouseZoom.setSchedulingBounds(bounds);
//创建键盘默认行为
KeyNavigatorBehavior keyNavBeh=new KeyNavigatorBehavior(trans);
keyNavBeh.setSchedulingBounds(bounds);
objRoot.addChild(keyNavBeh);
//白色背景
Background bg=new Background(new Color3f(0.0f,0.0f,0.0f));
bg.setApplicationBounds(bounds);
objRoot.addChild(bg);
//创建带材质的背景
//TextureLoader bgTexture=new TextureLoader("bg3.jpg",this);
//Background bg=new Background(bgTexture.getImage());
//bg.setApplicationBounds(bounds);
//trans.addChild(shape1);//3D物体 加到 变换组
//trans.addChild(shape2);//3D物体 加到 变换组
objRoot.addChild(trans);
//变换组 加到 分枝组
objRoot.addChild(behavior);
//鼠标行为 加到 分枝组
//objRoot.addChild(myMouseRotate);
//objRoot.addChild(myMouseZoom);
//objRoot.addChild(bg);//背景 加到 分枝组
//编译
objRoot.compile();
//回送创建好的带3D物体的分枝组
return objRoot ;
}
mySimpleUniverse()
{
//创建带控制的画布
GraphicsConfiguration config=SimpleUniverse.getPreferredConfiguration();
Canvas3D c=new Canvas3D(config);
//创建以画布为依托的简单场景图对象,没有多个Locale
SimpleUniverse u=new SimpleUniverse(c);
u.getViewingPlatform().setNominalViewingTransform();
//创建分支组对象
BranchGroup scene=createSceneGraph(c);
//组装,分支组 对象加到 场景图
u.addBranchGraph(scene);
//带场景图的画布 加到 本applet中
setLayout(new BorderLayout());
add("Center",c);
}
//测试码
//public static void main(String aregs[])
//{new MainFrame(new mySimpleUniverse(),200,200);//加applet到应用程序界面
//}
}
class SomeShape3D
{
public static float zuoBiaoZhouSmallDingDian=0.09f;//小坐标顶点位置
public static float zuoBiaoZhouSmallDingXi=0.02f;//小坐标顶点伞的半径
public static float zuoBiaoZhouSmallDingChang=0.07f;//小坐标顶点伞的长度
public static float zuoBiaoZhouSmallWeiDian=-0.09f;//小坐标尾巴的位置
public static float zuoBiaoZhouBigDingDian=1.0f;//大坐标顶点位置
public static float zuoBiaoZhouBigDingXi=0.04f;//大坐标顶点伞的半径
public static float zuoBiaoZhouBigDingChang=0.8f;//大坐标顶点伞的长度
public static float zuoBiaoZhouBigWeiDian=-1.0f;//大坐标尾巴的位置
public static float fangKuaiBanJing=0.18f;//每个方块的半径
public static void zuoBiaoZhuBigXShape3D(TransformGroup trans)
{
//创建大坐标轴对象
int i ;
float x1,x2,y1,y2,z1,z2 ;
SomeShape3D.addText3DDonghua(trans,"X",new Point3f(zuoBiaoZhouBigDingDian*10,0.0f,0.0f),0.1f,Block.mianColor[0],0);
Point3f[]vert=new Point3f[41];
Color3f[]colors=new Color3f[41];
for(i=0;i<27;i++)
{
if(i==0)
{
vert[i]=new Point3f(zuoBiaoZhouBigDingDian,0.0f,0.0f);
colors[i]=Block.mianColor[0];
}
else
{
z1=(float)(zuoBiaoZhouBigDingXi*Math.cos(i*2*Math.PI/25));
x1=zuoBiaoZhouBigDingChang;
y1=(float)(zuoBiaoZhouBigDingXi*Math.sin(i*2*Math.PI/25));
vert[i]=new Point3f(x1,y1,z1);
colors[i]=Block.mianColor[0];
}
}
for(i=0;i<14;i++)
{
if(i==0)
{
vert[27+i]=new Point3f(zuoBiaoZhouBigDingDian,0.0f,0.0f);
colors[27+i]=Block.mianColor[0];
}
else
{
z1=(float)(0.01f*Math.cos(i*2*Math.PI/12));
x1=zuoBiaoZhouBigWeiDian ;
y1=(float)(0.01f*Math.sin(i*2*Math.PI/12));
vert[27+i]=new Point3f(x1,y1,z1);
colors[27+i]=Block.mianColor[1];
}
}
int count[]=new int[2];
count[0]=27 ;
count[1]=14 ;
TriangleFanArray tri=new TriangleFanArray(
vert.length,
TriangleFanArray.COORDINATES|TriangleStripArray.COLOR_3,
count
);
tri.setCoordinates(0,vert);
tri.setColors(0,colors);
Appearance app=new Appearance();
PolygonAttributes polyAttrib=new PolygonAttributes();
polyAttrib.setCullFace(PolygonAttributes.CULL_NONE);
//polyAttrib.setPolygonMode(PolygonAttributes.POLYGON_LINE);
app.setPolygonAttributes(polyAttrib);
Shape3D shape=new Shape3D(tri,app);
System.out.print("zuoBiaoZhuBigX 创建 完成/n");
trans.addChild(shape);
//到这里,大坐标轴对象创建完成
}
public static void zuoBiaoZhuBigYShape3D(TransformGroup trans)
{
//创建大坐标轴对象
int i ;
float x1,x2,y1,y2,z1,z2 ;
SomeShape3D.addText3DDonghua(trans,"Y",new Point3f(-1.0f,zuoBiaoZhouBigDingDian*10,0.0f),0.1f,Block.mianColor[2],0);
Point3f[]vert=new Point3f[41];
Color3f[]colors=new Color3f[41];
for(i=0;i<27;i++)
{
if(i==0)
{
vert[i]=new Point3f(0.0f,zuoBiaoZhouBigDingDian,0.0f);
colors[i]=Block.mianColor[2];
}
else
{
x1=(float)(zuoBiaoZhouBigDingXi*Math.cos(i*2*Math.PI/25));
y1=zuoBiaoZhouBigDingChang;
z1=(float)(zuoBiaoZhouBigDingXi*Math.sin(i*2*Math.PI/25));
vert[i]=new Point3f(x1,y1,z1);
colors[i]=Block.mianColor[2];
}
}
for(i=0;i<14;i++)
{
if(i==0)
{
vert[27+i]=new Point3f(0.0f,zuoBiaoZhouBigDingDian,0.0f);
colors[27+i]=Block.mianColor[2];
}
else
{
x1=(float)(0.01f*Math.cos(i*2*Math.PI/12));
y1=zuoBiaoZhouBigWeiDian ;
z1=(float)(0.01f*Math.sin(i*2*Math.PI/12));
vert[27+i]=new Point3f(x1,y1,z1);
colors[27+i]=Block.mianColor[3];
}
}
int count[]=new int[2];
count[0]=27 ;
count[1]=14 ;
TriangleFanArray tri=new TriangleFanArray(
vert.length,
TriangleFanArray.COORDINATES|TriangleStripArray.COLOR_3,
count
);
tri.setCoordinates(0,vert);
tri.setColors(0,colors);
Appearance app=new Appearance();
PolygonAttributes polyAttrib=new PolygonAttributes();
polyAttrib.setCullFace(PolygonAttributes.CULL_NONE);
//polyAttrib.setPolygonMode(PolygonAttributes.POLYGON_LINE);
app.setPolygonAttributes(polyAttrib);
Shape3D shape=new Shape3D(tri,app);
System.out.print("zuoBiaoZhuBigY 创建 完成/n");
trans.addChild(shape);
//到这里,大坐标轴对象创建完成
}
public static void zuoBiaoZhuBigZShape3D(TransformGroup trans)
{
//创建大坐标轴对象
int i ;
float x1,x2,y1,y2,z1,z2 ;
SomeShape3D.addText3DDonghua(trans,"Z",new Point3f(-1.0f,0.0f,zuoBiaoZhouBigDingDian*10),0.1f,Block.mianColor[4],0);
Point3f[]vert=new Point3f[41];
Color3f[]colors=new Color3f[41];
for(i=0;i<27;i++)
{
if(i==0)
{
vert[i]=new Point3f(0.0f,0.0f,zuoBiaoZhouBigDingDian);
colors[i]=Block.mianColor[4];
}
else
{
y1=(float)(zuoBiaoZhouBigDingXi*Math.cos(i*2*Math.PI/25));
z1=zuoBiaoZhouBigDingChang;
x1=(float)(zuoBiaoZhouBigDingXi*Math.sin(i*2*Math.PI/25));
vert[i]=new Point3f(x1,y1,z1);
colors[i]=Block.mianColor[4];
}
}
for(i=0;i<14;i++)
{
if(i==0)
{
vert[27+i]=new Point3f(0.0f,0.0f,zuoBiaoZhouBigDingDian);
colors[27+i]=Block.mianColor[4];
}
else
{
y1=(float)(0.01f*Math.cos(i*2*Math.PI/12));
z1=zuoBiaoZhouBigWeiDian ;
x1=(float)(0.01f*Math.sin(i*2*Math.PI/12));
vert[27+i]=new Point3f(x1,y1,z1);
colors[27+i]=Block.mianColor[5];
}
}
int count[]=new int[2];
count[0]=27 ;
count[1]=14 ;
TriangleFanArray tri=new TriangleFanArray(
vert.length,
TriangleFanArray.COORDINATES|TriangleStripArray.COLOR_3,
count
);
tri.setCoordinates(0,vert);
tri.setColors(0,colors);
Appearance app=new Appearance();
PolygonAttributes polyAttrib=new PolygonAttributes();
polyAttrib.setCullFace(PolygonAttributes.CULL_NONE);
//polyAttrib.setPolygonMode(PolygonAttributes.POLYGON_LINE);
app.setPolygonAttributes(polyAttrib);
Shape3D shape=new Shape3D(tri,app);
System.out.print("zuoBiaoZhuBigZ 创建 完成/n");
trans.addChild(shape);
//到这里,大坐标轴对象创建完成
}
public static void zuoBiaoZhuSmallXShape3D(TransformGroup trans)
{
//创建小坐标轴对象
int i ;
float x1,x2,y1,y2,z1,z2 ;
Point3f[]vert=new Point3f[41];
Color3f[]colors=new Color3f[41];
for(i=0;i<27;i++)
{
if(i==0)
{
vert[i]=new Point3f(zuoBiaoZhouSmallDingDian,0.0f,0.0f);
colors[i]=Block.mianColor[0];
}
else
{
z1=(float)(zuoBiaoZhouSmallDingXi*Math.cos(i*2*Math.PI/25));
x1=zuoBiaoZhouSmallDingChang ;
y1=(float)(zuoBiaoZhouSmallDingXi*Math.sin(i*2*Math.PI/25));
vert[i]=new Point3f(x1,y1,z1);
colors[i]=Block.mianColor[0];
}
}
for(i=0;i<14;i++)
{
if(i==0)
{
vert[27+i]=new Point3f(zuoBiaoZhouSmallDingDian,0.0f,0.0f);
colors[27+i]=Block.mianColor[0];
}
else
{
z1=(float)(0.005f*Math.cos(i*2*Math.PI/12));
x1=zuoBiaoZhouSmallWeiDian ;
y1=(float)(0.005f*Math.sin(i*2*Math.PI/12));
vert[27+i]=new Point3f(x1,y1,z1);
colors[27+i]=Block.mianColor[1];
}
}
int count[]=new int[2];
count[0]=27 ;
count[1]=14 ;
TriangleFanArray tri=new TriangleFanArray(
vert.length,
TriangleFanArray.COORDINATES|TriangleStripArray.COLOR_3,
count
);
tri.setCoordinates(0,vert);
tri.setColors(0,colors);
Appearance app=new Appearance();
PolygonAttributes polyAttrib=new PolygonAttributes();
polyAttrib.setCullFace(PolygonAttributes.CULL_NONE);
//polyAttrib.setPolygonMode(PolygonAttributes.POLYGON_LINE);
app.setPolygonAttributes(polyAttrib);
Shape3D shape=new Shape3D(tri,app);
//System.out.print("zuoBiaoZhuSmallX 创建 完成");
trans.addChild(shape);
//到这里,小坐标轴对象创建完成
}
public static void zuoBiaoZhuSmallYShape3D(TransformGroup trans)
{
//创建小坐标轴对象
int i ;
float x1,x2,y1,y2,z1,z2 ;
Point3f[]vert=new Point3f[41];
Color3f[]colors=new Color3f[41];
for(i=0;i<27;i++)
{
if(i==0)
{
vert[i]=new Point3f(0.0f,zuoBiaoZhouSmallDingDian,0.0f);
colors[i]=Block.mianColor[2];
}
else
{
x1=(float)(zuoBiaoZhouSmallDingXi*Math.cos(i*2*Math.PI/25));
y1=zuoBiaoZhouSmallDingChang ;
z1=(float)(zuoBiaoZhouSmallDingXi*Math.sin(i*2*Math.PI/25));
vert[i]=new Point3f(x1,y1,z1);
colors[i]=Block.mianColor[2];
}
}
for(i=0;i<14;i++)
{
if(i==0)
{
vert[27+i]=new Point3f(0.0f,zuoBiaoZhouSmallDingDian,0.0f);
colors[27+i]=Block.mianColor[2];
}
else
{
x1=(float)(0.005f*Math.cos(i*2*Math.PI/12));
y1=zuoBiaoZhouSmallWeiDian ;
z1=(float)(0.005f*Math.sin(i*2*Math.PI/12));
vert[27+i]=new Point3f(x1,y1,z1);
colors[27+i]=Block.mianColor[3];
}
}
int count[]=new int[2];
count[0]=27 ;
count[1]=14 ;
TriangleFanArray tri=new TriangleFanArray(
vert.length,
TriangleFanArray.COORDINATES|TriangleStripArray.COLOR_3,
count
);
tri.setCoordinates(0,vert);
tri.setColors(0,colors);
Appearance app=new Appearance();
PolygonAttributes polyAttrib=new PolygonAttributes();
polyAttrib.setCullFace(PolygonAttributes.CULL_NONE);
//polyAttrib.setPolygonMode(PolygonAttributes.POLYGON_LINE);
app.setPolygonAttributes(polyAttrib);
Shape3D shape=new Shape3D(tri,app);
//System.out.print("zuoBiaoZhuSmallY 创建 完成");
trans.addChild(shape);
//到这里,小坐标轴对象创建完成
}
public static void zuoBiaoZhuSmallZShape3D(TransformGroup trans)
{
//创建小坐标轴对象
int i ;
float x1,x2,y1,y2,z1,z2 ;
Point3f[]vert=new Point3f[41];
Color3f[]colors=new Color3f[41];
for(i=0;i<27;i++)
{
if(i==0)
{
vert[i]=new Point3f(0.0f,0.0f,zuoBiaoZhouSmallDingDian);
colors[i]=Block.mianColor[4];
}
else
{
y1=(float)(zuoBiaoZhouSmallDingXi*Math.cos(i*2*Math.PI/25));
z1=zuoBiaoZhouSmallDingChang ;
x1=(float)(zuoBiaoZhouSmallDingXi*Math.sin(i*2*Math.PI/25));
vert[i]=new Point3f(x1,y1,z1);
colors[i]=Block.mianColor[4];
}
}
for(i=0;i<14;i++)
{
if(i==0)
{
vert[27+i]=new Point3f(0.0f,0.0f,zuoBiaoZhouSmallDingDian);
colors[27+i]=Block.mianColor[4];
}
else
{
y1=(float)(0.005f*Math.cos(i*2*Math.PI/12));
z1=zuoBiaoZhouSmallWeiDian ;
x1=(float)(0.005f*Math.sin(i*2*Math.PI/12));
vert[27+i]=new Point3f(x1,y1,z1);
colors[27+i]=Block.mianColor[5];
}
}
int count[]=new int[2];
count[0]=27 ;
count[1]=14 ;
TriangleFanArray tri=new TriangleFanArray(
vert.length,
TriangleFanArray.COORDINATES|TriangleStripArray.COLOR_3,
count
);
tri.setCoordinates(0,vert);
tri.setColors(0,colors);
Appearance app=new Appearance();
PolygonAttributes polyAttrib=new PolygonAttributes();
polyAttrib.setCullFace(PolygonAttributes.CULL_NONE);
//polyAttrib.setPolygonMode(PolygonAttributes.POLYGON_LINE);
app.setPolygonAttributes(polyAttrib);
Shape3D shape=new Shape3D(tri,app);
//System.out.print("zuoBiaoZhuSmallZ 创建 完成");
trans.addChild(shape);
//到这里,小坐标轴对象创建完成
}
public static Shape3D shapeMaker(Component observer,String filename,Point3f[] p)
{
//用材质,四顶点数组创建一个四边面,需要applet对象observer
//创建贴图和外观
TextureLoader loader=new TextureLoader(filename,observer);
ImageComponent2D myImage=loader.getImage();
Texture myTex=loader.getTexture();
myTex.setImage(0,myImage);
Appearance appear=new Appearance();
appear.setTexture(myTex);
//四边形对象
//QuadArray tri=new QuadArray(dingdian.length,QuadArray.COORDINATES|QuadArray.COLOR_3|QuadArray.TEXTURE_COORDINATE_2);
QuadArray tri=new QuadArray(4,QuadArray.COORDINATES|QuadArray.TEXTURE_COORDINATE_2);//GeometryArray
tri.setCoordinates(0,p);
//tri.setColors(0,color);
//给四边形对象配材质
TexCoord2f texCoords=new TexCoord2f();//材质坐标
texCoords.set(0.0f,1.0f);//取左下角
tri.setTextureCoordinate(0,0,texCoords);//为左上角
texCoords.set(0.0f,0.0f);//
tri.setTextureCoordinate(0,1,texCoords);//
texCoords.set(1.0f,0.0f);//
tri.setTextureCoordinate(0,2,texCoords);//
texCoords.set(1.0f,1.0f);//
tri.setTextureCoordinate(0,3,texCoords);//
Shape3D shape=new Shape3D(tri,appear);
return shape ;
//到这里,6个面对象创建完成
}
public static void addText3DDonghua(TransformGroup parentTrg,String textString,Point3f myPoint3f,float sl,Color3f ambientColor,int donghua)
{
//s1定scale,myPoint3f定位置,daxiao是大小
//字的左下角默认左下角在中点,当tl=0.1时,要向左移10才到左端
//自定义trg
Transform3D trgtra=new Transform3D();
TransformGroup trg=new TransformGroup(trgtra);
trg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
trg.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
//trg.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
double tessellation=-0.0 ;
String fontName="vadana" ;
// Create the root of the branch graph
// Create a Transformgroup to scale all objects so they
// appear in the scene.
TransformGroup objScale=new TransformGroup();
Transform3D t3d=new Transform3D();
// Assuming uniform size chars, set scale to fit string in view
t3d.setScale(sl);
objScale.setTransform(t3d);
trg.addChild(objScale);
// Create the transform group node and initialize it to the
// identity. Enable the TRANSFORM_WRITE capability so that
// our behavior code can modify it at runtime. Add it to the
// root of the subgraph.
TransformGroup objTrans=new TransformGroup();
objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
objScale.addChild(objTrans);
Font3D f3d ;
if(tessellation>0.0)
{
f3d=new Font3D(new Font(fontName,Font.PLAIN,2),
tessellation,
new FontExtrusion());
}
else
{
f3d=new Font3D(new Font(fontName,Font.PLAIN,2),
new FontExtrusion());
}
Text3D txt=new Text3D(f3d,textString,
myPoint3f);
Shape3D sh=new Shape3D();
Appearance app=new Appearance();
Material mm=new Material();
mm.setLightingEnable(true);
app.setMaterial(mm);
sh.setGeometry(txt);
sh.setAppearance(app);
objTrans.addChild(sh);
BoundingSphere bounds=
new BoundingSphere(new Point3d(0.0,0.0,0.0),100.0);
// Set up the ambient light
AmbientLight ambientLightNode=new AmbientLight(ambientColor);
ambientLightNode.setInfluencingBounds(bounds);
trg.addChild(ambientLightNode);
// Set up the directional lights
Color3f light1Color=new Color3f(1.0f,1.0f,0.9f);
Vector3f light1Direction=new Vector3f(1.0f,1.0f,1.0f);
Color3f light2Color=new Color3f(1.0f,1.0f,0.9f);
Vector3f light2Direction=new Vector3f(-1.0f,-1.0f,-1.0f);
DirectionalLight light1
=new DirectionalLight(light1Color,light1Direction);
light1.setInfluencingBounds(bounds);
trg.addChild(light1);
DirectionalLight light2
=new DirectionalLight(light2Color,light2Direction);
light2.setInfluencingBounds(bounds);
trg.addChild(light2);
if(donghua==1)
{
//给trg(自定义),加上旋转插件
Alpha alpha1=new Alpha(-1,Alpha.INCREASING_ENABLE|Alpha.DECREASING_ENABLE,0,0,5000,300,100000,5000,300,100000);
RotationInterpolator myRoTate=new RotationInterpolator(alpha1,trg,trgtra,0.0f,(float)Math.PI*30);
myRoTate.setSchedulingBounds(bounds);
trg.addChild(myRoTate);
trgtra.rotZ(Math.PI/2);
trg.setTransform(trgtra);
System.out.println("/n文本 动画 方案:"+donghua);
}
parentTrg.addChild(trg);
}
}
//由于java数组的局限,整个程序使用0,1,2空间,为了方便计算,计算时要进行坐标变换
public class MoFang
{
//该类含魔方的数据表示,blockArray为计算单步必需的,Position为便于人的视觉(用于操作和
//显示输出)而添加的辅助结构,实际上,我们可以总是从blockArray算出来,但计算量大得惊人
public static Block[][][]blockArray=new Block[3][3][3];
//设魔方27块
public static Position[][][]positionArray=new Position[3][3][3];
//魔方27个绝对位置
//用于处理对魔方的操作,共27种操作
//doType:'X','Y','Z'
//Floor:-1,0,1
//totateArg:90 180 -90
public static void doIt(char doType,int Floor,int totateArg)
{
System.out.println("/n处理操作:"+doType);
System.out.println("层数:"+(Floor-1));
System.out.println("角度:"+totateArg+"/n");
if(!Block.closeDonghua) //动画打开才等待
{
while(Block.yunXingThread!=0)
{System.out.print('.');
}
}
switch(doType)
{
case 'Z' :
for(int i=0;i<=2;i++)
for(int j=0;j<=2;j++)
//for(int k=0;k<=2;k++)
{
if(Block.closeDonghua)//动画关闭才变换中间块
{
if(i==1&&j==1)
{
continue ;
}
}
int changBlockX=positionArray[i][j][Floor].x ;
int changBlockY=positionArray[i][j][Floor].y ;
int changBlockZ=positionArray[i][j][Floor].z ;
Block changBlock=blockArray[changBlockX][changBlockY][changBlockZ];
changBlock.xyzChange('Z',totateArg);
}
break ;
case 'Y' :
for(int i=0;i<=2;i++)
//for(int j=0;j<=2;j++)
for(int k=0;k<=2;k++)
{
if(Block.closeDonghua)
{
if(i==1&&k==1)
{
continue ;
}
}
int changBlockX=positionArray[i][Floor][k].x ;
int changBlockY=positionArray[i][Floor][k].y ;
int changBlockZ=positionArray[i][Floor][k].z ;
Block changBlock=blockArray[changBlockX][changBlockY][changBlockZ];
changBlock.xyzChange('Y',totateArg);
}
break ;
case 'X' :
//for(int i=0;i<=2;i++)
for(int j=0;j<=2;j++)
for(int k=0;k<=2;k++)
{
if(Block.closeDonghua)
{
if(j==1&&k==1)
{
continue ;
}
}
int changBlockX=positionArray[Floor][j][k].x ;
int changBlockY=positionArray[Floor][j][k].y ;
int changBlockZ=positionArray[Floor][j][k].z ;
Block changBlock=blockArray[changBlockX][changBlockY][changBlockZ];
changBlock.xyzChange('X',totateArg);
}
break ;
default :
System.out.println("无效的操作");
}
someBlockNewToOld();
}
//开始人工智能计算解法
//select:选择电脑的计算方案
public static void autoStart(int select)
{
}
//输出每个位置上的块号
public static void showPosition()
{
System.out.println("/n每个位置上的块号:");
for(int i=0;i<=2;i++)
for(int j=0;j<=2;j++)
for(int k=0;k<=2;k++)
{
//System.out.println("Block"+i+","+j+","+k+" "+blockArray[i][j][k].x+","+blockArray[i][j][k].y+","+blockArray[i][j][k].z);
System.out.println("Position:"+(i-1)+","+(j-1)+","+(k-1)+" 的块号是: "+(positionArray[i][j][k].x-1)+","+(positionArray[i][j][k].y-1)+","+(positionArray[i][j][k].z-1));
}
}
//把刚才记录的有变化(来了新的块)的位置 存储到 没个position的块号中
public static void someBlockNewToOld()
{
for(int i=0;i<=2;i++)
for(int j=0;j<=2;j++)
for(int k=0;k<=2;k++)
{
if(positionArray[i][j][k].haveNew)
{
positionArray[i][j][k].newToOld();
}
}
}
public static void myWait()
{
try
{
System.in.read();
System.in.read();
//暂停
}
catch(Exception e)
{
}
}
//测试用代码
public static void someTest()
{
doIt('Y',0+1,90);
// //new MoFang().showPosition();
MoFang.myWait();
doIt('Y',0+1,-90);
MoFang.myWait();
// new MoFang().showPosition();
doIt('Z',-1+1,-90);
//new MoFang().showPosition();
MoFang.myWait();
doIt('Z',-1+1,90);
// new MoFang().showPosition();
//MoFang.myWait();
//new MoFang().showPosition();
doIt('X',1+1,90);
MoFang.myWait();
//new MoFang().showPosition();
doIt('X',1+1,-90);
// new MoFang().showPosition();
MoFang.myWait();
//showPosition();
}
//魔方自动随机变化多少条