Java 怎么创建三维球体

2019年7月29日20:04:27Java 怎么创建三维球体已关闭评论 477 1581字阅读5分16秒

1.简介

Java 怎么创建三维球体?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */


import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;

public class Position {

    public Position() {

        SimpleUniverse universe = new SimpleUniverse();
        BranchGroup group = new BranchGroup();

        
        for (float x = -1.0f; x <= 0.5f; x = x + 0.1f) {
            Sphere sphere = new Sphere(0.05f);
            TransformGroup tg = new TransformGroup();
            Transform3D transform = new Transform3D();
            Vector3f vector = new Vector3f(x, .0f, .0f);
            transform.setTranslation(vector);
            tg.setTransform(transform);
            tg.addChild(sphere);
            group.addChild(tg);
        }

        
        for (float y = -1.0f; y <= 1.0f; y = y + 0.1f) {
            TransformGroup tg = new TransformGroup();
            Transform3D transform = new Transform3D();
            Cone cone = new Cone(0.05f, 0.1f);
            Vector3f vector = new Vector3f(.0f, y, .0f);
            transform.setTranslation(vector);
            tg.setTransform(transform);
            tg.addChild(cone);
            group.addChild(tg);
        }

        
        for (float z = -1.0f; z <= 1.0f; z = z + 0.1f) {
            TransformGroup tg = new TransformGroup();
            Transform3D transform = new Transform3D();
            Cylinder cylinder = new Cylinder(0.05f, 0.1f);
            Vector3f vector = new Vector3f(.0f, .0f, z);
            transform.setTranslation(vector);
            tg.setTransform(transform);
            tg.addChild(cylinder);
            group.addChild(tg);
        }

        Color3f light1Color = new Color3f(.1f, 1.4f, .1f); 
        BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0,
                0.0), 100.0);
        Vector3f light1Direction = new Vector3f(4.0f, -7.0f, -12.0f);
        DirectionalLight light1 = new DirectionalLight(light1Color,
                light1Direction);
        light1.setInfluencingBounds(bounds);
        group.addChild(light1);
        universe.getViewingPlatform().setNominalViewingTransform();

        
        universe.addBranchGraph(group);

    }

    public static void main(String[] args) {
        new Position();
    }

}

 
继续阅读
编程十万个怎么办