mysimpleuniverse.java

来自「java2参考大全上的例子的源码和自己的理解.」· Java 代码 · 共 1,951 行 · 第 1/5 页

JAVA
1,951
字号
        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空间,为了方便计算,计算时要进行坐标变换

 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);
          }
        }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?