⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fourshapes.java

📁 Java3D程序,实现各种立体变换.适于基础编程.
💻 JAVA
字号:
import com.sun.j3d.utils.universe.SimpleUniverse; 
import com.sun.j3d.utils.geometry.*; 

import javax.media.j3d.*;
import javax.vecmath.*;

public class fourShapes
{ public fourShapes()
   {SimpleUniverse su=new SimpleUniverse();
    BranchGroup bg=new BranchGroup();
    
    bg.addChild(shape("sphere",0.5f,0.5f,0.0f,0.3f,0f,1f,0f));
    bg.addChild(shape("cylinder",-0.5f,0.5f,0.0f,0.4f,1f,0f,0f));
    bg.addChild(shape("cone",0.5f,-0.5f,0.0f,0.2f,1f,1f,0f));
    bg.addChild(shape("box",-0.5f,-0.5f,0.0f,0.2f,0f,0f,1f));
    
    su.getViewingPlatform().setNominalViewingTransform();
    su.addBranchGraph(bg);
   }

  public static void main(String[] args)
   { new fourShapes();
   }
  
  private TransformGroup shape(String shape,float x, float y, float z,float size, float r, float g, float b)
  {TransformGroup theShape = new TransformGroup();

  Transform3D tf1 = new Transform3D();  // set the translation matrix
   Vector3f vect=new Vector3f(x,y,z);
   tf1.setTranslation(vect);
   
   Transform3D tf2= new Transform3D();  // set the rotation matrix
//   tf2.rotX(Math.PI/4);
   tf2.rotX(0);
   tf1.mul(tf2);                        // calculate the final transformation matrix
   
   theShape.setTransform(tf1);
   
   Appearance ap=new Appearance();      // set the color of the object
   ColoringAttributes ca= new ColoringAttributes();
   ca.setColor(r,g,b);
   ap.setColoringAttributes(ca);

   if (shape.equals("sphere"))          // pick the right geometry
	 {Sphere thing = new Sphere(size,ap);
	  theShape.addChild(thing);
	 }
   if (shape.equals("cone"))
     {Cone thing = new Cone(size,size,ap);
      theShape.addChild(thing);
     }
   if (shape.equals("cylinder"))
     {Cylinder thing = new Cylinder(size,size,ap);
      theShape.addChild(thing);
     }
   if (shape.equals("box"))
     {Box thing = new Box(size,size,size*2,ap);
      theShape.addChild(thing);
     }
   return theShape;
  }
} 

⌨️ 快捷键说明

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