cube_master 发表于 2005-3-20 19:39:40

一个 java3D 魔方程序的源代码

<P>在网上找到的一个 java3D 魔方程序的源代码</P>
<P>本 java 程序据说是可“用鼠标转动魔方”、“用键盘上的方向键移动魔方位置”、“智能搜索求解魔方”等等,希望对想编魔方程序的魔友有用。</P>
<P>由于原程序长度大于论坛帖子长度,所以只好分段帖上。</P>
<P>摘自:<a href="http://blog.csdn.net/bazhonglixiao/archive/2004/12/06/205830.aspx" target="_blank" >http://blog.csdn.net/bazhonglixiao/archive/2004/12/06/205830.aspx</A></P>
<P>1,本程序由2001级中南大学计算机科学与技术本科学生 李霄 所写,其他人可进行研究和学习。未经作者本人同意,任何人不得以任何理由将其中的任意一段代码用作任意与商业有关的行为。你可以进行二次开发,但在发布前要通过本人允许。
     有需要请与作者本人联系,本人愿意作出回复和解答疑问。</P>
<P>2,本程序为放出的测试用魔方(MoFang.java),包含所有的核心代码及注释,调试,测试代码。你可以用鼠标转动魔方,用键盘上的方向键移动魔方位置,除此以外,代码中还有大量功能,如智能搜索求解魔方,目前正在制作图形控制界面,也可用于applet嵌入网页中,Frame,也可做成java bean组件,有java bean需要的,请与作者本人联系。</P>
<P>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次随机转动,要他进行其他转动,可以加代码,或等待本人的最终图形用户界面版本。</P>
<P>4,本人目前是应届本科毕业生,希望有机会加入软件制作行业。联系电话:2005年6月以前联系电话0731-8710325,之后未定,QQ:77690139



//存为MoFang.java</P>
<P>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.*;</P>
[此贴子已经被作者于2005-3-20 20:16:17编辑过]

cube_master 发表于 2005-3-20 19:40:43

class mySimpleUniverse extends Applet
{
    BranchGroup createSceneGraph(Canvas3D canvas)<P>    {</P><P>
        //System.out.print("**1**");</P><P>        //创建变换组,无用的t3D
        Transform3D t3d=new Transform3D();
        TransformGroup trans=new TransformGroup(t3d);
        trans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        trans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);</P><P>        //创建分枝组
        BranchGroup objRoot=new BranchGroup();</P><P>        //测试
        //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);</P><P>        //初始化数据结构
        System.out.println("\n\n载入方块,并向变换组中加入每个方块的坐标系和方块...");
        for(int i=0;i&lt;=2;i++)
        for(int j=0;j&lt;=2;j++)
        for(int k=0;k&lt;=2;k++)
        {
         int[] p;
         p=Position.getPxyzFromPositionAy(i,j,k,MoFang.positionArray);
            MoFang.blockArray=new Block(i,j,k,p,p,p,trans,t3d,objRoot,this);
        }
        System.out.println("加入每个方块的坐标系和方块,完成.\n");</P><P>        //创建大坐标轴,自动加到主坐标系
        SomeShape3D.zuoBiaoZhuBigXShape3D(trans);
        SomeShape3D.zuoBiaoZhuBigYShape3D(trans);
        SomeShape3D.zuoBiaoZhuBigZShape3D(trans);</P><P>        //创建边界对象
        BoundingSphere bounds=new BoundingSphere(new Point3d(0.0,0.0,0.0),100);</P><P>        //创建鼠标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);</P><P>        //创建鼠标旋转行为
        MouseRotate behavior=new MouseRotate();
        behavior.setTransformGroup(trans);
        behavior.setSchedulingBounds(bounds);</P><P>        //创建鼠标平移行为
        //MouseTranslate myMouseRotate=new MouseTranslate();
        //myMouseRotate.setTransformGroup(trans);
        //myMouseRotate.setSchedulingBounds(bounds);</P><P>        //创建鼠标缩放行为
        MouseZoom myMouseZoom=new MouseZoom();
        myMouseZoom.setTransformGroup(trans);
        myMouseZoom.setSchedulingBounds(bounds);</P><P>        //创建键盘默认行为
        KeyNavigatorBehavior keyNavBeh=new KeyNavigatorBehavior(trans);
        keyNavBeh.setSchedulingBounds(bounds);
        objRoot.addChild(keyNavBeh);</P><P>        //白色背景
        Background bg=new Background(new Color3f(0.0f,0.0f,0.0f));
        bg.setApplicationBounds(bounds);
        objRoot.addChild(bg);</P><P>        //创建带材质的背景
        //TextureLoader bgTexture=new TextureLoader("bg3.jpg",this);
        //Background bg=new Background(bgTexture.getImage());
        //bg.setApplicationBounds(bounds);</P><P>
        //trans.addChild(shape1);//3D物体 加到 变换组
        //trans.addChild(shape2);//3D物体 加到 变换组
        objRoot.addChild(trans);
        //变换组 加到 分枝组
        objRoot.addChild(behavior);
        //鼠标行为  加到 分枝组
        //objRoot.addChild(myMouseRotate);
        //objRoot.addChild(myMouseZoom);
        //objRoot.addChild(bg);//背景 加到 分枝组</P><P>
        //编译
        objRoot.compile();
        //回送创建好的带3D物体的分枝组
        return objRoot ;</P><P>    }</P><P>
    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);</P><P>        //带场景图的画布 加到 本applet中
        setLayout(new BorderLayout());
        add("Center",c);
    }</P><P>    //测试码
    //public static void main(String aregs[])
    //{new MainFrame(new mySimpleUniverse(),200,200);//加applet到应用程序界面
    //}
}</P>

cube_master 发表于 2005-3-20 19:41:52

<P>class SomeShape3D
{</P><P>    public static float zuoBiaoZhouSmallDingDian=0.09f;//小坐标顶点位置
    public static float zuoBiaoZhouSmallDingXi=0.02f;//小坐标顶点伞的半径
    public static float zuoBiaoZhouSmallDingChang=0.07f;//小坐标顶点伞的长度
    public static float zuoBiaoZhouSmallWeiDian=-0.09f;//小坐标尾巴的位置</P><P>    public static float zuoBiaoZhouBigDingDian=1.0f;//大坐标顶点位置
    public static float zuoBiaoZhouBigDingXi=0.04f;//大坐标顶点伞的半径
    public static float zuoBiaoZhouBigDingChang=0.8f;//大坐标顶点伞的长度
    public static float zuoBiaoZhouBigWeiDian=-1.0f;//大坐标尾巴的位置</P><P>    public static float fangKuaiBanJing=0.18f;//每个方块的半径</P><P>    public static void zuoBiaoZhuBigXShape3D(TransformGroup trans)
    {</P><P>
        //创建大坐标轴对象</P><P>        int i ;
        float x1,x2,y1,y2,z1,z2 ;</P><P>         SomeShape3D.addText3DDonghua(trans,"X",new Point3f(zuoBiaoZhouBigDingDian*10,0.0f,0.0f),0.1f,Block.mianColor,0);</P><P>        Point3f[]vert=new Point3f;
        Color3f[]colors=new Color3f;
        for(i=0;i&lt;27;i++)
        {
            if(i==0)
            {
                vert=new Point3f(zuoBiaoZhouBigDingDian,0.0f,0.0f);
                colors=Block.mianColor;
            }
            else
            {
                z1=(float)(zuoBiaoZhouBigDingXi*Math.cos(i*2*Math.PI/25));
                x1=zuoBiaoZhouBigDingChang;
                y1=(float)(zuoBiaoZhouBigDingXi*Math.sin(i*2*Math.PI/25));
                vert=new Point3f(x1,y1,z1);
                colors=Block.mianColor;
            }
        }</P><P>        for(i=0;i&lt;14;i++)
        {
            if(i==0)
            {
                vert=new Point3f(zuoBiaoZhouBigDingDian,0.0f,0.0f);
                colors=Block.mianColor;
            }
            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=new Point3f(x1,y1,z1);
                colors=Block.mianColor;
            }
        }</P><P>
        int count[]=new int;
        count=27 ;
        count=14 ;</P><P>        TriangleFanArray tri=new TriangleFanArray(
        vert.length,
        TriangleFanArray.COORDINATES|TriangleStripArray.COLOR_3,
        count
        );
        tri.setCoordinates(0,vert);
        tri.setColors(0,colors);</P><P>
        Appearance app=new Appearance();</P><P>        PolygonAttributes polyAttrib=new PolygonAttributes();
        polyAttrib.setCullFace(PolygonAttributes.CULL_NONE);
        //polyAttrib.setPolygonMode(PolygonAttributes.POLYGON_LINE);
        app.setPolygonAttributes(polyAttrib);</P><P>        Shape3D shape=new Shape3D(tri,app);</P><P>        System.out.print("zuoBiaoZhuBigX 创建 完成\n");</P><P>        trans.addChild(shape);</P><P>
        //到这里,大坐标轴对象创建完成</P><P>
    }</P><P>
    public static   void zuoBiaoZhuBigYShape3D(TransformGroup trans)
    {</P><P>
        //创建大坐标轴对象</P><P>        int i ;
        float x1,x2,y1,y2,z1,z2 ;</P><P>        SomeShape3D.addText3DDonghua(trans,"Y",new Point3f(-1.0f,zuoBiaoZhouBigDingDian*10,0.0f),0.1f,Block.mianColor,0);</P><P>        Point3f[]vert=new Point3f;
        Color3f[]colors=new Color3f;
        for(i=0;i&lt;27;i++)
        {
            if(i==0)
            {
                vert=new Point3f(0.0f,zuoBiaoZhouBigDingDian,0.0f);
                colors=Block.mianColor;
            }
            else
            {
                x1=(float)(zuoBiaoZhouBigDingXi*Math.cos(i*2*Math.PI/25));
                y1=zuoBiaoZhouBigDingChang;
                z1=(float)(zuoBiaoZhouBigDingXi*Math.sin(i*2*Math.PI/25));
                vert=new Point3f(x1,y1,z1);
                colors=Block.mianColor;
            }
        }</P><P>        for(i=0;i&lt;14;i++)
        {
            if(i==0)
            {
                vert=new Point3f(0.0f,zuoBiaoZhouBigDingDian,0.0f);
                colors=Block.mianColor;
            }
            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=new Point3f(x1,y1,z1);
                colors=Block.mianColor;
            }
        }</P><P>
        int count[]=new int;
        count=27 ;
        count=14 ;</P><P>        TriangleFanArray tri=new TriangleFanArray(
        vert.length,
        TriangleFanArray.COORDINATES|TriangleStripArray.COLOR_3,
        count
        );
        tri.setCoordinates(0,vert);
        tri.setColors(0,colors);</P><P>
        Appearance app=new Appearance();</P><P>        PolygonAttributes polyAttrib=new PolygonAttributes();
        polyAttrib.setCullFace(PolygonAttributes.CULL_NONE);
        //polyAttrib.setPolygonMode(PolygonAttributes.POLYGON_LINE);
        app.setPolygonAttributes(polyAttrib);</P><P>        Shape3D shape=new Shape3D(tri,app);</P><P>        System.out.print("zuoBiaoZhuBigY 创建 完成\n");</P><P>        trans.addChild(shape);</P><P>
        //到这里,大坐标轴对象创建完成</P><P>
    }</P>

cube_master 发表于 2005-3-20 19:42:46

<P>
    public static  void  zuoBiaoZhuBigZShape3D(TransformGroup trans)
    {</P><P>
        //创建大坐标轴对象</P><P>        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,0);</P><P>        Point3f[]vert=new Point3f;
        Color3f[]colors=new Color3f;
        for(i=0;i&lt;27;i++)
        {
            if(i==0)
            {
                vert=new Point3f(0.0f,0.0f,zuoBiaoZhouBigDingDian);
                colors=Block.mianColor;
            }
            else
            {
                y1=(float)(zuoBiaoZhouBigDingXi*Math.cos(i*2*Math.PI/25));
                z1=zuoBiaoZhouBigDingChang;
                x1=(float)(zuoBiaoZhouBigDingXi*Math.sin(i*2*Math.PI/25));
                vert=new Point3f(x1,y1,z1);
                colors=Block.mianColor;
            }
        }</P><P>        for(i=0;i&lt;14;i++)
        {
            if(i==0)
            {
                vert=new Point3f(0.0f,0.0f,zuoBiaoZhouBigDingDian);
                colors=Block.mianColor;
            }
            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=new Point3f(x1,y1,z1);
                colors=Block.mianColor;
            }
        }</P><P>
        int count[]=new int;
        count=27 ;
        count=14 ;</P><P>        TriangleFanArray tri=new TriangleFanArray(
        vert.length,
        TriangleFanArray.COORDINATES|TriangleStripArray.COLOR_3,
        count
        );
        tri.setCoordinates(0,vert);
        tri.setColors(0,colors);</P><P>
        Appearance app=new Appearance();</P><P>        PolygonAttributes polyAttrib=new PolygonAttributes();
        polyAttrib.setCullFace(PolygonAttributes.CULL_NONE);
        //polyAttrib.setPolygonMode(PolygonAttributes.POLYGON_LINE);
        app.setPolygonAttributes(polyAttrib);</P><P>        Shape3D shape=new Shape3D(tri,app);</P><P>        System.out.print("zuoBiaoZhuBigZ 创建 完成\n");</P><P>        trans.addChild(shape);</P><P>
        //到这里,大坐标轴对象创建完成</P><P>
    }</P><P>
    public static  void  zuoBiaoZhuSmallXShape3D(TransformGroup trans)
    {</P><P>
        //创建小坐标轴对象</P><P>        int i ;
        float x1,x2,y1,y2,z1,z2 ;</P><P>        Point3f[]vert=new Point3f;
        Color3f[]colors=new Color3f;
        for(i=0;i&lt;27;i++)
        {
            if(i==0)
            {
                vert=new Point3f(zuoBiaoZhouSmallDingDian,0.0f,0.0f);
                colors=Block.mianColor;
            }
            else
            {
                z1=(float)(zuoBiaoZhouSmallDingXi*Math.cos(i*2*Math.PI/25));
                x1=zuoBiaoZhouSmallDingChang ;
                y1=(float)(zuoBiaoZhouSmallDingXi*Math.sin(i*2*Math.PI/25));
                vert=new Point3f(x1,y1,z1);
                colors=Block.mianColor;
            }
        }</P><P>        for(i=0;i&lt;14;i++)
        {
            if(i==0)
            {
                vert=new Point3f(zuoBiaoZhouSmallDingDian,0.0f,0.0f);
                colors=Block.mianColor;
            }
            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=new Point3f(x1,y1,z1);
                colors=Block.mianColor;
            }
        }</P><P>
        int count[]=new int;
        count=27 ;
        count=14 ;</P><P>        TriangleFanArray tri=new TriangleFanArray(
        vert.length,
        TriangleFanArray.COORDINATES|TriangleStripArray.COLOR_3,
        count
        );
        tri.setCoordinates(0,vert);
        tri.setColors(0,colors);</P><P>
        Appearance app=new Appearance();</P><P>        PolygonAttributes polyAttrib=new PolygonAttributes();
        polyAttrib.setCullFace(PolygonAttributes.CULL_NONE);
        //polyAttrib.setPolygonMode(PolygonAttributes.POLYGON_LINE);
        app.setPolygonAttributes(polyAttrib);</P><P>        Shape3D shape=new Shape3D(tri,app);</P><P>        //System.out.print("zuoBiaoZhuSmallX 创建 完成");</P><P>        trans.addChild(shape);</P><P>
        //到这里,小坐标轴对象创建完成</P><P>
    }</P><P>
    public static  void  zuoBiaoZhuSmallYShape3D(TransformGroup trans)
    {</P><P>
        //创建小坐标轴对象</P><P>        int i ;
        float x1,x2,y1,y2,z1,z2 ;</P><P>        Point3f[]vert=new Point3f;
        Color3f[]colors=new Color3f;
        for(i=0;i&lt;27;i++)
        {
            if(i==0)
            {
                vert=new Point3f(0.0f,zuoBiaoZhouSmallDingDian,0.0f);
                colors=Block.mianColor;
            }
            else
            {
                x1=(float)(zuoBiaoZhouSmallDingXi*Math.cos(i*2*Math.PI/25));
                y1=zuoBiaoZhouSmallDingChang ;
                z1=(float)(zuoBiaoZhouSmallDingXi*Math.sin(i*2*Math.PI/25));
                vert=new Point3f(x1,y1,z1);
                colors=Block.mianColor;
            }
        }</P><P>        for(i=0;i&lt;14;i++)
        {
            if(i==0)
            {
                vert=new Point3f(0.0f,zuoBiaoZhouSmallDingDian,0.0f);
                colors=Block.mianColor;
            }
            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=new Point3f(x1,y1,z1);
                colors=Block.mianColor;
            }
        }</P><P>
        int count[]=new int;
        count=27 ;
        count=14 ;</P><P>        TriangleFanArray tri=new TriangleFanArray(
        vert.length,
        TriangleFanArray.COORDINATES|TriangleStripArray.COLOR_3,
        count
        );
        tri.setCoordinates(0,vert);
        tri.setColors(0,colors);</P><P>
        Appearance app=new Appearance();</P><P>        PolygonAttributes polyAttrib=new PolygonAttributes();
        polyAttrib.setCullFace(PolygonAttributes.CULL_NONE);
        //polyAttrib.setPolygonMode(PolygonAttributes.POLYGON_LINE);
        app.setPolygonAttributes(polyAttrib);</P><P>        Shape3D shape=new Shape3D(tri,app);</P><P>        //System.out.print("zuoBiaoZhuSmallY 创建 完成");</P><P>        trans.addChild(shape);</P><P>
        //到这里,小坐标轴对象创建完成</P><P>
    }</P>

cube_master 发表于 2005-3-20 19:43:38

    public static  void  zuoBiaoZhuSmallZShape3D(TransformGroup trans)
    {<P>
        //创建小坐标轴对象</P><P>        int i ;
        float x1,x2,y1,y2,z1,z2 ;</P><P>        Point3f[]vert=new Point3f;
        Color3f[]colors=new Color3f;
        for(i=0;i&lt;27;i++)
        {
            if(i==0)
            {
                vert=new Point3f(0.0f,0.0f,zuoBiaoZhouSmallDingDian);
                colors=Block.mianColor;
            }
            else
            {
                y1=(float)(zuoBiaoZhouSmallDingXi*Math.cos(i*2*Math.PI/25));
                z1=zuoBiaoZhouSmallDingChang ;
                x1=(float)(zuoBiaoZhouSmallDingXi*Math.sin(i*2*Math.PI/25));
                vert=new Point3f(x1,y1,z1);
                colors=Block.mianColor;
            }
        }</P><P>        for(i=0;i&lt;14;i++)
        {
            if(i==0)
            {
                vert=new Point3f(0.0f,0.0f,zuoBiaoZhouSmallDingDian);
                colors=Block.mianColor;
            }
            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=new Point3f(x1,y1,z1);
                colors=Block.mianColor;
            }
        }</P><P>
        int count[]=new int;
        count=27 ;
        count=14 ;</P><P>        TriangleFanArray tri=new TriangleFanArray(
        vert.length,
        TriangleFanArray.COORDINATES|TriangleStripArray.COLOR_3,
        count
        );
        tri.setCoordinates(0,vert);
        tri.setColors(0,colors);</P><P>
        Appearance app=new Appearance();</P><P>        PolygonAttributes polyAttrib=new PolygonAttributes();
        polyAttrib.setCullFace(PolygonAttributes.CULL_NONE);
        //polyAttrib.setPolygonMode(PolygonAttributes.POLYGON_LINE);
        app.setPolygonAttributes(polyAttrib);</P><P>        Shape3D shape=new Shape3D(tri,app);</P><P>        //System.out.print("zuoBiaoZhuSmallZ 创建 完成");</P><P>        trans.addChild(shape);</P><P>
        //到这里,小坐标轴对象创建完成</P><P>
    }</P><P>
       public static Shape3D shapeMaker(Component observer,String filename,Point3f[] p)
       {</P><P>       //用材质,四顶点数组创建一个四边面,需要applet对象observer</P><P>
            //创建贴图和外观
            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);</P><P>            //四边形对象
            //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);</P><P>            //给四边形对象配材质
            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);//</P><P>            Shape3D shape=new Shape3D(tri,appear);</P><P>        return shape ;</P><P>
        //到这里,6个面对象创建完成
    }</P><P>
    public static void addText3DDonghua(TransformGroup parentTrg,String textString,Point3f myPoint3f,float sl,Color3f ambientColor,int donghua)
    {
     //s1定scale,myPoint3f定位置,daxiao是大小
     //字的左下角默认左下角在中点,当tl=0.1时,要向左移10才到左端</P><P>     //自定义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);</P><P>
     double tessellation=-0.0 ;
     String fontName="vadana" ;
        // Create the root of the branch graph</P><P>
        // 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</P><P>        t3d.setScale(sl);</P><P>        objScale.setTransform(t3d);
        trg.addChild(objScale);</P><P>        // 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);</P><P>        objScale.addChild(objTrans);</P><P>
        Font3D f3d ;
        if(tessellation&gt;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);</P><P>        BoundingSphere bounds=
        new BoundingSphere(new Point3d(0.0,0.0,0.0),100.0);</P><P>        // Set up the ambient light</P><P>        AmbientLight ambientLightNode=new AmbientLight(ambientColor);
        ambientLightNode.setInfluencingBounds(bounds);
        trg.addChild(ambientLightNode);</P><P>        // 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);</P><P>        DirectionalLight light1
        =new DirectionalLight(light1Color,light1Direction);
        light1.setInfluencingBounds(bounds);
        trg.addChild(light1);</P><P>        DirectionalLight light2
        =new DirectionalLight(light2Color,light2Direction);
        light2.setInfluencingBounds(bounds);
        trg.addChild(light2);</P><P>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);
}</P><P>
parentTrg.addChild(trg);
    }</P><P>
}</P>

cube_master 发表于 2005-3-20 19:45:29

//由于java数组的局限,整个程序使用0,1,2空间,为了方便计算,计算时要进行坐标变换<P>public class MoFang
{
    //该类含魔方的数据表示,blockArray为计算单步必需的,Position为便于人的视觉(用于操作和
    //显示输出)而添加的辅助结构,实际上,我们可以总是从blockArray算出来,但计算量大得惊人
    public static Block[][][]blockArray=new Block;
    //设魔方27块
    public static Position[][][]positionArray=new Position;
    //魔方27个绝对位置</P><P>    //用于处理对魔方的操作,共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");</P><P>                   if(!Block.closeDonghua)  //动画打开才等待
{</P><P>  while(Block.yunXingThread!=0)
  {System.out.print('.');</P><P>  }</P><P>
}</P><P>
        switch(doType)
        {
            case 'Z' :
            for(int i=0;i&lt;=2;i++)
            for(int j=0;j&lt;=2;j++)
            //for(int k=0;k&lt;=2;k++)
            {
             if(Block.closeDonghua)//动画关闭才变换中间块
{
                  if(i==1&amp;&amp;j==1)
                {
                    continue ;
                }
}</P><P>                int changBlockX=positionArray.x ;
                int changBlockY=positionArray.y ;
                int changBlockZ=positionArray.z ;
                Block changBlock=blockArray;
                changBlock.xyzChange('Z',totateArg);</P><P>            }</P><P>            break ;
            case 'Y' :
            for(int i=0;i&lt;=2;i++)
            //for(int j=0;j&lt;=2;j++)
            for(int k=0;k&lt;=2;k++)
            {
                           if(Block.closeDonghua)
{
                if(i==1&amp;&amp;k==1)
                {
                    continue ;
                }
            }
                int changBlockX=positionArray.x ;
                int changBlockY=positionArray.y ;
                int changBlockZ=positionArray.z ;
                Block changBlock=blockArray;
                changBlock.xyzChange('Y',totateArg);
            }
            break ;</P><P>            case 'X' :
            //for(int i=0;i&lt;=2;i++)
            for(int j=0;j&lt;=2;j++)
            for(int k=0;k&lt;=2;k++)
            {
                          if(Block.closeDonghua)
{
                if(j==1&amp;&amp;k==1)
                {
                    continue ;
                }
}
                int changBlockX=positionArray.x ;
                int changBlockY=positionArray.y ;
                int changBlockZ=positionArray.z ;
                Block changBlock=blockArray;
                changBlock.xyzChange('X',totateArg);
            }
            break ;</P><P>            default :
            System.out.println("无效的操作");
        }</P><P>        someBlockNewToOld();
    }</P><P>    //开始人工智能计算解法
    //select:选择电脑的计算方案
    public static void autoStart(int select)
    {
    }</P><P>    //输出每个位置上的块号
    public static void showPosition()
    {
        System.out.println("\n每个位置上的块号:");
        for(int i=0;i&lt;=2;i++)
        for(int j=0;j&lt;=2;j++)
        for(int k=0;k&lt;=2;k++)
        {
            //System.out.println("Block"+i+","+j+","+k+"    "+blockArray.x+","+blockArray.y+","+blockArray.z);
            System.out.println("Position:"+(i-1)+","+(j-1)+","+(k-1)+"  的块号是:  "+(positionArray.x-1)+","+(positionArray.y-1)+","+(positionArray.z-1));</P><P>        }
    }</P><P>    //把刚才记录的有变化(来了新的块)的位置 存储到 没个position的块号中
    public static void someBlockNewToOld()
    {
        for(int i=0;i&lt;=2;i++)
        for(int j=0;j&lt;=2;j++)
        for(int k=0;k&lt;=2;k++)
        {
            if(positionArray.haveNew)
            {
                positionArray.newToOld();
            }
        }
    }</P><P> public static void myWait()
{
                      try
        {</P><P>            System.in.read();</P><P>                        System.in.read();
            //暂停
        }
        catch(Exception e)
        {
        }
}</P><P>    //测试用代码
      public static   void someTest()
    {</P><P>      doIt('Y',0+1,90);
      //        //new MoFang().showPosition();
      MoFang.myWait();
       doIt('Y',0+1,-90);
      MoFang.myWait();
             // new MoFang().showPosition();</P><P>       doIt('Z',-1+1,-90);
                     //new MoFang().showPosition();
       MoFang.myWait();</P><P>       doIt('Z',-1+1,90);
       //      new MoFang().showPosition();
       //MoFang.myWait();
              //new MoFang().showPosition();</P><P>       doIt('X',1+1,90);
       MoFang.myWait();
               //new MoFang().showPosition();
       doIt('X',1+1,-90);
        // new MoFang().showPosition();
       MoFang.myWait();</P><P>        //showPosition();</P><P>
    }</P><P>
    //魔方自动随机变化多少条
       public static void  ranGet(int num)
    {
     char selectChar='E';//'X','Y','Z'
     int layer;//-1,0,1
     int jiaoDu=0;//90,-19,180</P><P>     for(int i=0;i&lt;num;i++)
     {</P><P>
      //选xyz
      int select=(int)((Math.random()*10)%3);
      if(select==0){selectChar='X';}
      if(select==1){selectChar='Y';}
      if(select==2){selectChar='Z';}</P><P>      //
      layer=(int)((Math.random()*10)%3);//0,1,2
      layer-=1;//-1,0,1</P><P>      //
      int jiao=(int)((Math.random()*10)%3);//0,1,2
      if(jiao==0){jiaoDu=90;}
      if(jiao==1){jiaoDu=-90;}
      if(jiao==2){jiaoDu=180-90;}</P><P>
      System.out.println("\n*******************************\nRandom Generater:"+(i+1)+" of "+num);
      System.out.print(selectChar);
      System.out.print(","+layer+","+jiaoDu+"\n按任意键开始动画?\n");</P><P>      myWait();</P><P>      doIt(selectChar,layer+1,jiaoDu);
     }
    }</P><P>
    //从三维图形界面输入操作参数
    public static void graphicStart()
    {
     JFrame myframe =new JFrame();
        myframe.setVisible(true);</P><P>    }</P><P>    public static void main(String[]args)
    {</P><P>     //初始化位置数组,也可以从文件中加载,其实应该把所有关键信息从文件载入,关闭时保存
        for(int i=0;i&lt;=2;i++)
        for(int j=0;j&lt;=2;j++)
        for(int k=0;k&lt;=2;k++)
        {</P><P>            MoFang.positionArray=new Position(i,j,k);
        }
        System.out.println("初始化位置数组完成.最初全在原位置\n");</P><P>       //showPosition();</P><P>        new MainFrame(new mySimpleUniverse(),200,200);
        //加applet到应用程序界面</P><P>  //someTest();</P><P>        //graphicStart();</P><P>  myWait();</P><P>       ranGet(30);
        //</P><P>        //
    }</P><P>
}</P>

cube_master 发表于 2005-3-20 19:48:08

<P>class Block implements Runnable
{
    //计算已画的块数
    private static int whickBlockPainted=0 ;</P><P>    //计算生成的第几块
    private static int BlockCreated=0 ;</P><P>    //每面的颜色,排列为:+x,-x,+y,-y,+z,-z,no
    public static Color3f[] mianColor=
{new Color3f(1.0f,0.0f,0.0f),
new Color3f(0.0f,1.0f,0.0f),
new Color3f(0.0f,0.0f,1.0f),
new Color3f(1.0f,1.0f,0.0f),
new Color3f(1.0f,0.0f,1.0f),
new Color3f(0.0f,1.0f,1.0f),
new Color3f(0.2f,0.2f,0.2f)
};
    //每面的材质,排列为:+x,-x,+y,-y,+z,-z,no
    public static String[] mianImageFile=new String;</P><P>    //块偏移量
    private static float kuaiZhongXinWeizhi=0.4f ;</P><P>    //创建材质时要用的,仅用于他,他是一个applet对象
    Component observer;</P><P>    //该块的编号
    private int blockIdX ;
    private int blockIdY ;
    private int blockIdZ ;</P><P>    //该块的位置
    private int x ;
    private int y ;
    private int z ;</P><P>    //该块的坐标轴点,最初全为(1,1,1),表示与外坐标一致
    private MyPoint xvec;
    private MyPoint yvec;
    private MyPoint zvec;</P><P>    //position=new Verctor3f(0.0f,0.0f,0.0f);</P><P>    //该块的角度,限定为从-359到359,模360可以限定为一周,如果模180,则只能表示半个圆,不行
    //int anglex ;
    //int angley ;
    //int anglez ;</P><P>    //添加物品的变换组
    public TransformGroup transGroup ;
    public Transform3D trans ;</P><P>
    //用于位置,角度变动的变换组
    public TransformGroup transGroupx ;
    public Transform3D transx ;
    public TransformGroup transGroupy ;
    public Transform3D transy ;
    public TransformGroup transGroupz ;
    public Transform3D transz ;
    public TransformGroup transGroupp ;
    public Transform3D transp ;</P><P>    //新的方块位置
int[]  nexyz={0,0,0};</P><P>    //动画相关
    int totateArg;
    boolean canNew=false;
    char selectedC;
    Thread myThread;
public static boolean closeDonghua=false ; //为true使用线程动画,为false直接调用</P><P>
  private static int selectDonghuaId=0;//取ID变量,取值从0~8,每次从0开始取,正好9个,取完后变为10(每取一个自动加1),这时才让所有线程一起动
  private int myDonghuaId;
private static int donghuaDelay=50 ; //绝对延迟,
public static int whileDelay=20;//加快同步系统性能延迟,</P><P> //公共控制变量
  public static int yunXingThread=0;//运行的线程数,启动线程时加1,退出时减一。可以控制主线程等待,直到为0时才开始做所有的xyzChange,否则等待</P><P>//动画时的多线程要用它,因为主线程仍在跑,x,y,z会变化,只能用它保存
            int yuanx;
            int yuany;
            int yuanz;</P><P>//当前位置x点的偏移
MyPoint chaxvec;
//当前位置y点的偏移
MyPoint chayvec;
//当前位置z点的偏移
MyPoint chazvec;</P><P>
    //构造函数,给初值
    public Block(int i,int j,int k,int px,int py,int pz,TransformGroup parentTransGroup,Transform3D t3d,BranchGroup objRoot,Component obServer1)
    {</P><P>        blockIdX=i ;
        blockIdY=j ;
        blockIdZ=k ;</P><P>
        x=px ;
        y=py ;
        z=pz ;</P><P>//计算轴向量 :块坐标加坐标偏移
xvec=new MyPoint((blockIdX-1)+1,(blockIdY-1),(blockIdZ-1));
yvec=new MyPoint((blockIdX-1),(blockIdY-1)+1 ,(blockIdZ-1));
zvec=new MyPoint((blockIdX-1),(blockIdY-1),(blockIdZ-1)+1 );
//System.out.println("轴点:"+(zoux)+(zouy)+(zouz));
//System.out.println("轴向量:"+(zoux-(x-1))+(zouy-(y-1))+(zouz-(z-1)));</P><P>        observer=obServer1;</P><P>        //anglex=0 ;
        //angley=0 ;
        //anglez=0 ;</P><P>        trans=new Transform3D();
        transGroup=new TransformGroup(trans);
        transGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        transGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
        transGroup.setCapability(TransformGroup.ENABLE_PICK_REPORTING);</P><P>        transp=new Transform3D();
        transGroupp=new TransformGroup(transp);
        transGroupp.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        transGroupp.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
        //transGroupp.setCapability(TransformGroup.ENABLE_PICK_REPORTING);</P><P>        transx=new Transform3D();
        transGroupx=new TransformGroup(transx);
        transGroupx.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        transGroupx.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
        //transGroupx.setCapability(TransformGroup.ENABLE_PICK_REPORTING);</P><P>        transy=new Transform3D();
        transGroupy=new TransformGroup(transy);
        transGroupy.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        transGroupy.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
        //transGroupy.setCapability(TransformGroup.ENABLE_PICK_REPORTING);</P><P>        transz=new Transform3D();
        transGroupz=new TransformGroup(transz);
        transGroupz.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        transGroupz.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
        //transGroupz.setCapability(TransformGroup.ENABLE_PICK_REPORTING);</P><P>        //用已知信息组装变换组,,
        //注意这里的顺序,先平移,再旋转,或先x,先y的顺序是不一样的,这里
        //必须是先在原位置自转好方向,再平移,这样,平移不会改变它的自转方向,
        //如果先平移,再转,转动会带动平移,刚才的平移无任何意义。
        //还要注意是先绕x转,再绕y转,再绕z转
        //换了顺序,是不一样的,例如x,y,z和z,y,x的结果不一样
        //但是,可以证明x,y和y,x的顺序无关性,当都只左转或右转90度时,很容易证明
        //如果有180度,可以分成两次90度操作
        parentTransGroup.addChild(transGroupp);
        transGroupp.addChild(transGroupz);
        transGroupz.addChild(transGroupy);
        transGroupy.addChild(transGroupx);
        transGroupx.addChild(transGroup);</P><P>        // 测试,直接加到根,没有鼠标功能
        //objRoot.addChild(transGroup);</P><P>        //System.out.println("\n\n\n处理第"+BlockCreated+"块");
        //System.out.println("块"+(blockIdX-1)+(blockIdY-1)+(blockIdZ-1)+"的坐标系添加完成");</P><P>        BlockCreated++;</P><P>        //if (BlockCreated==14)//||BlockCreated==3//||BlockCreated==2||BlockCreated==20||BlockCreated==16</P><P>        add3DCube(x-1,y-1,z-1,transGroup);
        //建小坐标轴
        SomeShape3D.zuoBiaoZhuSmallXShape3D(transGroup);
        SomeShape3D.zuoBiaoZhuSmallYShape3D(transGroup);
        SomeShape3D.zuoBiaoZhuSmallZShape3D(transGroup);</P><P>        //测试主变换组的平移
        //Shape3D shape1=SomeShape3D.FlowerShape3D();
        //parentTransGroup.addChild(shape1);
        //t3d.setTranslation(new Vector3f(0.0f,1.0f,0.0f));
        //parentTransGroup.setTransform(t3d);</P><P>        //测试子变换组的平移(不行,不起作用)
        //Shape3D shape2=SomeShape3D.SanShape3D();
        //transGroup.addChild(shape2);
        //trans.setTranslation(new Vector3f(0.0f,-1f,0.0f));
        //transGroup.setTransform(t3d);</P><P>        //创建鼠标行为
        //MouseRotate behavior1=new MouseRotate();
        //behavior1.setTransformGroup(transGroup);
        //behavior1.setSchedulingBounds(new BoundingSphere(new Point3d(0.0,0.0,0.0),100));</P><P>        //objRoot.addChild(behavior1);//鼠标行为  加到 分枝组</P><P>        //设置初始值,显示输出
        float fx ;
        float fy ;
        float fz ;
        fx=(float)kuaiZhongXinWeizhi*(x-1);
        fy=(float)kuaiZhongXinWeizhi*(y-1);
        fz=(float)kuaiZhongXinWeizhi*(z-1);</P><P>        //变换量
        transx.rotX(Math.toRadians(0));//anglex
        transy.rotY(Math.toRadians(0));
        transz.rotZ(Math.toRadians(0));
        transp.setTranslation(new Vector3f(fx,fy,fz));</P><P>
        //生效
        transGroupp.setTransform(transp);
        transGroupx.setTransform(transx);
        transGroupy.setTransform(transy);
        transGroupz.setTransform(transz);</P><P>    }</P>

cube_master 发表于 2005-3-20 19:50:04

int[] jisuanNextXYZ(char doType,int totateArg,int oldx,int oldy,int oldz)
//需要变量x,y,z,doType,totateArg,输出返回到数组
{
//System.out.println("计算下一个点。。。");<P>//用于计算
int newz=0;
int newy=0;
int newx=0;</P><P>//返回时的标准格式
int[] nextXYZ={0,0,0};</P><P>//计算时接收调用外面函数的返回函数值
int[] myShunShiNext={0,0};</P><P>if(totateArg==0)
{
//当totateArg为0时,没有可用的if匹配,返回原位置
newx=oldx;
newy=oldy;
newz=oldz;</P><P>//返回结果
nextXYZ=newx;
nextXYZ=newy;
nextXYZ=newz;
//System.out.println("\n变为位置:"+newx+newy+newz);
return nextXYZ;
}</P><P>//
//System.out.println("\n原位置:"+oldx+oldy+oldz);
//System.out.println("绕:"+doType+"转"+totateArg);</P><P>switch(doType)
{
case 'Z' :
            newz=oldz ;
if(totateArg==90)
           {myShunShiNext=quXiaYiGe(oldx,oldy,1);
}</P><P>else if (totateArg==180)
           {myShunShiNext=quXiaYiGe(oldx,oldy,2);</P><P>}
else if(totateArg==-90)
           {myShunShiNext=quXiaYiGe(oldx,oldy,3);
}
newx=myShunShiNext;
newy=myShunShiNext;
break;</P><P>case 'Y' :</P><P>
            newy=oldy ;</P><P>if(totateArg==90)
           {myShunShiNext=quXiaYiGe(oldz,oldx,1);
}</P><P>else if(totateArg==180)
           {myShunShiNext=quXiaYiGe(oldz,oldx,2);
}
else if(totateArg==-90)
           {myShunShiNext=quXiaYiGe(oldz,oldx,3);
}</P><P>newz=myShunShiNext;
newx=myShunShiNext;
break;</P><P>case 'X' :</P><P>   newx=oldx ;</P><P>if(totateArg==90)
           {myShunShiNext=quXiaYiGe(oldy,oldz,1);
}</P><P>else if(totateArg==180)
           {myShunShiNext=quXiaYiGe(oldy,oldz,2);
}
else if(totateArg==-90)
           {myShunShiNext=quXiaYiGe(oldy,oldz,3);
}</P><P>
newy=myShunShiNext;
newz=myShunShiNext;
break;</P><P>}</P><P>//返回结果
nextXYZ=newx;
nextXYZ=newy;
nextXYZ=newz;
//System.out.println("\n变为位置:"+newx+newy+newz);
return nextXYZ;
}</P><P>boolean fangXiangCorrect(int argx,int argy,int argz)
{//用三个差向量判断自转
//System.out.println("方向判断。。。");
//(blockIdX-1),(blockIdY-1),(blockIdZ-1)为初始点位置,分别加1得到三个初始向量
//xvec,yvec,zvec为保存的变化
//减去当前的方块位置,得到每个方向点当前向量chaxvec,chayvec,chazvec</P><P>//X点
int[] p1X=jisuanNextXYZ('X',argx,1,0,0);//原始x点便移
int[] p2X=jisuanNextXYZ('Y',argy,p1X,p1X,p1X);
int[] p3X=jisuanNextXYZ('Z',argz,p2X,p2X,p2X);//新的x点便移</P><P>//Y点
int[] p1Y=jisuanNextXYZ('X',argx,0,1,0);
int[] p2Y=jisuanNextXYZ('Y',argy,p1Y,p1Y,p1Y);
int[] p3Y=jisuanNextXYZ('Z',argz,p2Y,p2Y,p2Y);</P><P>//Z点
int[] p1Z=jisuanNextXYZ('X',argx,0,0,1);
int[] p2Z=jisuanNextXYZ('Y',argy,p1Z,p1Z,p1Z);
int[] p3Z=jisuanNextXYZ('Z',argz,p2Z,p2Z,p2Z);
//System.out.println("坐标系:"+chaX+"   "+chaY+"   "+chaZ);</P><P>// 新的x点便移=当前位置x点的偏移
if(
((p3X==chaxvec.x)&amp;&amp;(p3X==chaxvec.y)&amp;&amp;(p3X==chaxvec.z))&amp;&amp;
((p3Y==chayvec.x)&amp;&amp;(p3Y==chayvec.y)&amp;&amp;(p3Y==chayvec.z))&amp;&amp;
((p3Z==chazvec.x)&amp;&amp;(p3Z==chazvec.y)&amp;&amp;(p3Z==chazvec.z))
)</P><P>{System.out.println("坐标轴到位了");
return true;
}
else
{
//System.out.println("坐标轴没到位");
return false;
}
}</P><P>boolean weiZhiCorrect(int aidx,int aidy,int aidz,int argx,int argy,int argz)
{
int[] p1=jisuanNextXYZ('X',argx,blockIdX-1,blockIdY-1,blockIdZ-1);
int[] p2=jisuanNextXYZ('Y',argy,p1,p1,p1);
int[] p3=jisuanNextXYZ('Z',argz,p2,p2,p2);</P><P>if((p3==aidx)&amp;&amp;(p3==aidy)&amp;&amp;(p3==aidz))
{System.out.println("位置对了");
return true;
}
else
{
System.out.println("位置不对");
return false;
}
}</P><P>
//仅计算逆时针旋转时的坐标变化,当转逆时针-90度时,转化为3个逆时针90度
int[] quXiaYiGe(int num1,int num2,int n)
{
int[][] xiangXianZhi={{1,1},{-1,1},{-1,-1},{1,-1}}; //一,二,三,四,0,1,2,3
int[][] zouShangZhi={{1,0},{0,1},{-1,0},{0,-1}}; //</P><P>int[] result={0,0};
//System.out.println("旋转次数为:"+n);
//System.out.println(num1+" , "+num2+"旋转到:");</P><P>int temp=0;
for(int i=0;i&lt;n;i++)
{</P><P>
//循环一次转一次
if(num1==0&amp;&amp;num2==0)//
{
num1=0;
num2=0;
//System.out.println("0");
}</P><P>else if(num1&gt;0&amp;&amp;num2&gt;0)//一象限转到二象限
{
temp=num1;
num1=-num2;
num2=temp;
//System.out.println("1");
}
else if(num1&lt;0&amp;&amp;num2&gt;0)//二象限转到三象限
{
temp=num1;
num1=-num2;
num2=temp;
////System.out.println("2");
}
else if(num1&lt;0&amp;&amp;num2&lt;0)//三象限转到四象限
{
temp=num1;
num1=-num2;
num2=temp;
////System.out.println("3");
}
else if(num1&gt;0&amp;&amp;num2&lt;0)//四象限转到一象限
{
temp=num1;
num1=-num2;
num2=temp;
//System.out.println("4");
}</P><P>else if(num1&gt;0&amp;&amp;num2==0)//X上到Y上
{
temp=num1;
num1=0;
num2=temp;
//System.out.println("5");
}
else if(num1==0&amp;&amp;num2&gt;0)//Y上到X上
{
temp=num1;
num1=-num2;
num2=0;
//System.out.println("6");
}
else if(num1&lt;0&amp;&amp;num2==0)//
{
temp=num1;
num1=0;
num2=temp;
//System.out.println("7");
}
else if(num1==0&amp;&amp;num2&lt;0)//
{
temp=num1;
num1=-num2;
num2=0;
//System.out.println("8");
}
else
{System.out.println("no");}</P><P>}</P><P>//System.out.println(num1+" , "+num2);</P><P>result=num1;
result=num2;</P><P>return result;
}</P>

cube_master 发表于 2005-3-20 19:50:52

public void run()
  {
   //System.out.println("我的动画开始了");<P>   //动画协调,取一个id,并放出令牌
myDonghuaId=selectDonghuaId++;
selectDonghuaId%=9;
yunXingThread++;</P><P>   switch(selectedC)
  {
   case 'Z':
   {startDonghuaZ();break;
   }
   case 'Y':
   {startDonghuaY();break;
   }
   case 'X':
   {startDonghuaX();break;
   }
  }</P><P>  //最终位置
  while(!canNew)//当canNew=true时,输出到3D界面,否则只计算
  {//System.out.println(" 我在等待计算出新值...");
  }</P><P>
        //变换生效
        transGroupp.setTransform(transp);
        transGroupx.setTransform(transx);
        transGroupy.setTransform(transy);
        transGroupz.setTransform(transz);</P><P>
  canNew=false;</P><P>  //System.out.println("我的动画完成了");
   yunXingThread--;</P><P>  }</P><P> //用来从一个原坐标和结果坐标来 搜索 3个x,y,z顺序的旋转操作,返回到needRotate中
//使用了int[] jisuanNextXYZ(char doType,int totateArg,int oldx,int oldy,int oldz)</P><P>
int[] shouSuoXYZRotate(int oldx,int oldy,int oldz,int aidx,int aidy,int aidz)
{//搜索自转,不看位置,只看三个向量对不对</P><P>System.out.println("块原始位置:"+oldx+","+oldy+","+oldz);
System.out.println("块目标位置:"+aidx+","+aidy+","+aidz);</P><P>//存放结果
int needRotatex=0;
int needRotatey=0;
int needRotatez=0;</P><P>//转换返回
int[] needRotate={0,0,0};</P><P>int num=0;</P><P>wancheng:
for(int j=0;j&lt;=3;j++)
{</P><P>int toArg=0;//j=3时取0</P><P>      if(j==1){toArg=90;}
      if(j==2){toArg=-90;}
      if(j==3){toArg=180;}</P><P>for(int jj=0;jj&lt;=3;jj++)
{</P><P>int ttoArg=0;
      if(jj==1){ttoArg=90;}
      if(jj==2){ttoArg=-90;}
      if(jj==3){ttoArg=180;}</P><P>for(int jjj=0;jjj&lt;=3;jjj++)
{</P><P>int tttoArg=0;
      if(jjj==1){tttoArg=90;}
      if(jjj==2){tttoArg=-90;}
      if(jjj==3){tttoArg=180;}</P><P>     //System.out.println("验证xyz旋转.."+toArg+"  "+ttoArg+"  "+tttoArg);</P><P>     // 这个打开 有的会找不到
//boolean myBoolean1=weiZhiCorrect(nexyz,nexyz,nexyz,toArg,ttoArg,tttoArg);
boolean myBoolean=fangXiangCorrect(toArg,ttoArg,tttoArg);</P><P>//MoFang.myWait();</P><P>
if (myBoolean)
{</P><P>
needRotatex=toArg;
needRotatey=ttoArg;
needRotatez=tttoArg;</P><P>
needRotate=needRotatex;
needRotate=needRotatey;
needRotate=needRotatez;</P><P>num++;
System.out.println("*****找到了x,y,z旋转即可*****"+toArg+" , "+ttoArg+" , "+tttoArg+"");
//return needRotate;
}</P><P>}
}
}</P><P>if(num==0)
{
System.out.println("没有适合的xyz旋转,不旋转..");
MoFang.myWait();
}
else if(num&gt;=2)
{System.out.println("###########################找到了num="+num+"个");
}
return needRotate;
}</P><P>
       void startDonghuaX()
  {</P><P>
            //动画模块
            int chuJiao ;
            int oneTime ;
            int bianJiao ;
            int nowJiao ;</P><P>
            //测试
            //totateArg=90;</P><P>            chuJiao=getChujiao(yuany,yuanz);
            if(totateArg==-90)
            oneTime=-15 ;
            else
            oneTime=15 ;</P><P>                //System.out.println("chuJiao="+chuJiao);
                //System.out.println("totateArg="+totateArg);
            //nowJiao=chuJiao+bianJiao ;
            //这是目前角度公式,可见,据坐标可以判断它的角度嘛</P><P>            for(bianJiao=0;bianJiao!=(totateArg+oneTime);bianJiao+=oneTime)
            {</P><P>            if(!closeDonghua)
            {</P><P>              while(myDonghuaId!=selectDonghuaId)//动画同步相关轮到自己时才执行,把令牌交给下一个
             {;//System.out.println("我是"+myDonghuaId+"号,而令牌现在是"+selectDonghuaId+"号,我要等...");
       try
         {
          //System.in.read();//暂停
               myThread.sleep(whileDelay);
           }
             catch(Exception e)
           {
            }
           }
          }</P><P>
                //计算x,y和角度输出
                float fx ;
                float fy ;
                float fz ;
                //半径
                float r;</P><P>
    if(yuany==0&amp;&amp;yuanz==0)
    r=0;//(x,y)不变,故不用计算,直接给(0,0),初角返回任何值都可;
    else
    {
    if(yuany==0||yuanz==0)
    r=1.0f;
    else
    r=1.414f;
             }</P><P>                nowJiao=chuJiao+bianJiao ;</P><P>                //使用初角直为了计算当前(x,y)位置,当前jiaodu1与他无关,只与老角有关
                //(-1,-1)距离原点为根2,约1.732
                //0.3指定了1点在0.3,-0.3处
                fy=(float)kuaiZhongXinWeizhi*r*(float)Math.cos(Math.PI*nowJiao/180);
                fz=(float)kuaiZhongXinWeizhi*r*(float)Math.sin(Math.PI*nowJiao/180);
                fx=(float)kuaiZhongXinWeizhi*(yuanx);
                //z坐标不变,这里的z早减过了1</P><P>
        //计算出了全部数据,OK,开始刷新
        //用变换量
        //transz.rotZ(Math.toRadians(anglez));
        //transy.rotY(Math.toRadians(angley));
        //transx.rotX(Math.toRadians(bianJiao+oldJiaoDu));
        transp.setTranslation(new Vector3f(fx,fy,fz));</P><P>        //生效
        //transGroupz.setTransform(transz);
        //transGroupy.setTransform(transy);
        //transGroupx.setTransform(transx);
        transGroupp.setTransform(transp);</P><P>    //停不了,只能使用多线程或定时</P><P>                //System.out.println("bianJiao="+bianJiao);</P><P>
                 try
              {
                //System.in.read();//暂停
                //
                myThread.sleep(donghuaDelay);
             }
              catch(Exception e)
            {
             }</P><P>                           //在这里才放出令牌
             selectDonghuaId++;
     selectDonghuaId%=9;</P><P>            }
}</P>

cube_master 发表于 2005-3-20 19:52:11

<P>   void startDonghuaY()
  {
             //动画模块
            int chuJiao ;
            int oneTime ;
            int bianJiao ;
            int nowJiao ;</P><P>
            //测试
            //totateArg=90;</P><P>            chuJiao=getChujiao(yuanz,yuanx);
            if(totateArg==-90)
            oneTime=-15 ;
            else
            oneTime=15 ;</P><P>                //System.out.println("chuJiao="+chuJiao);
                //System.out.println("totateArg="+totateArg);
            //nowJiao=chuJiao+bianJiao ;
            //这是目前角度公式,可见,据坐标可以判断它的角度嘛</P><P>            for(bianJiao=0;bianJiao!=(totateArg+oneTime);bianJiao+=oneTime)
            {</P><P>            if(!closeDonghua)
            {</P><P>              while(myDonghuaId!=selectDonghuaId)//动画同步相关轮到自己时才执行,把令牌交给下一个
             {;//System.out.println("我是"+myDonghuaId+"号,而令牌现在是"+selectDonghuaId+"号,我要等...");
       try
         {
          //System.in.read();//暂停
               myThread.sleep(whileDelay);
           }
             catch(Exception e)
           {
            }
           }
          }</P><P>
                //计算x,y和角度输出
                float fx ;
                float fy ;
                float fz ;
                //半径
                float r;</P><P>
    if(yuanz==0&amp;&amp;yuanx==0)
    r=0;//(x,y)不变,故不用计算,直接给(0,0),初角返回任何值都可;
    else
    {
    if(yuanz==0||yuanx==0)
    r=1.0f;
    else
    r=1.414f;
             }</P><P>                nowJiao=chuJiao+bianJiao ;</P><P>                //使用初角直为了计算当前(x,y)位置,当前jiaodu1与他无关,只与老角有关
                //(-1,-1)距离原点为根2,约1.732
                //0.3指定了1点在0.3,-0.3处
                fz=(float)kuaiZhongXinWeizhi*r*(float)Math.cos(Math.PI*nowJiao/180);
                fx=(float)kuaiZhongXinWeizhi*r*(float)Math.sin(Math.PI*nowJiao/180);
                fy=(float)kuaiZhongXinWeizhi*(yuany);
                //z坐标不变,这里的z早减过了1</P><P>
        //计算出了全部数据,OK,开始刷新
        //用变换量
        //transz.rotZ(Math.toRadians(anglez));
        //transy.rotY(Math.toRadians(bianJiao+oldJiaoDu));
        //transx.rotX(Math.toRadians(anglex));
        transp.setTranslation(new Vector3f(fx,fy,fz));</P><P>        //生效
        //transGroupz.setTransform(transz);
        //transGroupy.setTransform(transy);
        //transGroupx.setTransform(transx);
        transGroupp.setTransform(transp);</P><P>    //停不了,只能使用多线程或定时</P><P>                //System.out.println("bianJiao="+bianJiao);</P><P>
                 try
              {
                //System.in.read();//暂停
                //
                myThread.sleep(donghuaDelay);
             }
              catch(Exception e)
            {
             }</P><P>                           //在这里才放出令牌
             selectDonghuaId++;
     selectDonghuaId%=9;</P><P>            }</P><P> }</P><P>
   void startDonghuaZ()
  {</P><P>            //动画模块
            int chuJiao ;
            int oneTime ;
            int bianJiao ;
            int nowJiao ;</P><P>
            //测试
            //totateArg=90;</P><P>            chuJiao=getChujiao(yuanx,yuany);
            if(totateArg==-90)
            oneTime=-15 ;
            else
            oneTime=15 ;</P><P>                //System.out.println("chuJiao="+chuJiao);
                //System.out.println("totateArg="+totateArg);
            //nowJiao=chuJiao+bianJiao ;
            //这是目前角度公式,可见,据坐标可以判断它的角度嘛</P><P>            for(bianJiao=0;bianJiao!=(totateArg+oneTime);bianJiao+=oneTime)
            {</P><P>            if(!closeDonghua)
            {</P><P>              while(myDonghuaId!=selectDonghuaId)//动画同步相关轮到自己时才执行,把令牌交给下一个
             {;//System.out.println("我是"+myDonghuaId+"号,而令牌现在是"+selectDonghuaId+"号,我要等...");
       try
         {
          //System.in.read();//暂停
               myThread.sleep(whileDelay);
           }
             catch(Exception e)
           {
            }
           }
          }</P><P>
                //计算x,y和角度输出
                float fx ;
                float fy ;
                float fz ;
                //半径
                float r;</P><P>
    if(yuanx==0&amp;&amp;yuany==0)
    r=0;//(x,y)不变,故不用计算,直接给(0,0),初角返回任何值都可;
    else
    {
    if(yuanx==0||yuany==0)
    r=1.0f;
    else
    r=1.414f;
             }</P><P>                nowJiao=chuJiao+bianJiao ;</P><P>                //使用初角直为了计算当前(x,y)位置,当前jiaodu1与他无关,只与老角有关
                //(-1,-1)距离原点为根2,约1.732
                //0.3指定了1点在0.3,-0.3处
                fx=(float)kuaiZhongXinWeizhi*r*(float)Math.cos(Math.PI*nowJiao/180);
                fy=(float)kuaiZhongXinWeizhi*r*(float)Math.sin(Math.PI*nowJiao/180);
                fz=(float)kuaiZhongXinWeizhi*(yuanz);
                //z坐标不变,这里的z早减过了1</P><P>
        //计算出了全部数据,OK,开始刷新
        //用变换量
        //transz.rotZ(Math.toRadians());
        //transy.rotY(Math.toRadians());
        //transx.rotX(Math.toRadians());
        transp.setTranslation(new Vector3f(fx,fy,fz));</P><P>        //生效
        //transGroupz.setTransform(transz);
        //transGroupy.setTransform(transy);
        //transGroupx.setTransform(transx);
        transGroupp.setTransform(transp);</P><P>    //停不了,只能使用多线程或定时</P><P>                //System.out.println("bianJiao="+bianJiao);</P><P>
                 try
              {
                //System.in.read();//暂停
                //
                myThread.sleep(donghuaDelay);
             }
              catch(Exception e)
            {
             }</P><P>                           //在这里才放出令牌
             selectDonghuaId++;
     selectDonghuaId%=9;</P><P>            }</P><P>
  }</P>
页: [1] 2
查看完整版本: 一个 java3D 魔方程序的源代码