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

📄 cylinderexample.java

📁 这个是JAVA3D 做的圆柱体的源代码。比较基本
💻 JAVA
字号:
import javax.media.j3d.*;

public class CylinderExample extends BasicSceneWithMouseControl{
  
  public static void main(String args[]){new CylinderExample();}
  
  private Geometry createGeometry() {
  
    // Define the attributes for the cylinder
    float radius = 0.3f;
    float height = 1.0f;
    int faces = 60;
    
    float angle = 0.0f;
    double angleIncrement = (2*Math.PI)/faces;
    
    // create an empty array of floats to hold the coordinates
    float coordinates[] = new float[faces*4*3];
    
    for(int f=0; f<faces; f++)
    {
      // Generate the four coordinates required for each face
      float x1 = (float)(radius*Math.cos(angle));
      float z1 = (float)(radius*Math.sin(angle));
      angle -= angleIncrement;
      float x2 = (float)(radius*Math.cos(angle));
      float z2 = (float)(radius*Math.sin(angle));
      
      // Populate the coordinates array
      coordinates[f*4*3] = x1;
      coordinates[f*4*3+1] = -height/2.0f;
      coordinates[f*4*3+2] = z1;
      
      coordinates[f*4*3+3] = x2;
      coordinates[f*4*3+4] = -height/2.0f;
      coordinates[f*4*3+5] = z2;
        
      coordinates[f*4*3+6] = x2;
      coordinates[f*4*3+7] = height/2.0f;
      coordinates[f*4*3+8] = z2;
        
      coordinates[f*4*3+9] = x1;
      coordinates[f*4*3+10] = height/2.0f;
      coordinates[f*4*3+11] = z1;       
    }
    
    QuadArray quadArray = new QuadArray(faces*4*3, 
        GeometryArray.COORDINATES);
    quadArray.setCoordinates(0, coordinates);
    return quadArray;
  
  }

  public BranchGroup createContentBranch() 
  {    
    BranchGroup objRoot = new BranchGroup();

    // Add the cylinder to the scene graph
    objRoot.addChild(new Shape3D(createGeometry()));

    return objRoot;
  }
}

⌨️ 快捷键说明

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