Java 怎么创建三维树

2019年7月29日20:06:41Java 怎么创建三维树已关闭评论 474 4306字阅读14分21秒

1.简介

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

2.示例代码

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

import java.applet.Applet;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.geometry.Cylinder;

public class Tree3D extends Applet {

    BranchGroup branches;
    TransformGroup branchTranGroup;
    SimpleUniverse universe;

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

    public Tree3D() {

        universe = new SimpleUniverse();
        branches = new BranchGroup();
        branchTranGroup = new TransformGroup();
        branchTranGroup.addChild(createBase());
        branchTranGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);

        
        makeTree(4, 4, 0, 0.6, 0.05f);

        
        branchTranGroup.addChild(createRotation(branchTranGroup));

        branches.addChild(branchTranGroup);

        
        branches.addChild(createLight());

        
        universe.getViewingPlatform().setNominalViewingTransform();
        universe.addBranchGraph(branches);

    }

    private void makeTree(int B, int level, double x1, double y1,
            float radius) {
        if (level == 0) {
            return;
        }

        double theta = Math.PI / B;
        float length = level * radius * 5;
        

        for (int branchNum = 0; branchNum <= B - 1; branchNum++) {

            double beta = theta * branchNum;
            double x2 = x1 + length
                    * Math.cos(Math.random() * theta + beta);
            double y2 = y1 - length
                    * Math.sin(Math.random() * theta + beta);
            double midX = (x2 + x1) / 2;
            double midY = (y2 + y1) / 2;

            
            float height = (float) Math.sqrt(Math.pow((x1 - x2), 2)
                    + Math.pow((y1 - y2), 2));

            

            
            double TommyX = x1+((x2-x1)*Math.cos(alpha));
            double TommyY = y2+((x2-x1)*Math.sin(alpha));
                        
            
            Transform3D rotateY = new Transform3D();
            rotateY.rotX(alpha);

            Cylinder branch = new Cylinder(radius, height);

            
            double angle = Math.PI / 2 - Math.atan((y2 - y1) / (x2 - x1));
            Transform3D rotation = new Transform3D();
            rotation.rotZ(angle);

            Vector3f vector = new Vector3f((float) midX,
                    (float) (-1 * midY), 0);
            Transform3D move = new Transform3D();
            move.setTranslation(vector);

            move.mul(rotation);
            
            move.mul(rotateY);

            TransformGroup branchGrp = new TransformGroup();
            branchGrp.setTransform(move);
            branchGrp.addChild(branch);

            branchTranGroup.addChild(branchGrp);

            makeTree(B, level - 1, x2, y2, radius - .018f);
        }

    }

    private TransformGroup createBase() {
        Cylinder cylinderBase = new Cylinder(0.05f, .80f);

        Transform3D rotateY = new Transform3D();
        rotateY.rotY(Math.PI / 6);

        Transform3D rotateZ = new Transform3D();
        rotateZ.rotZ(Math.PI / 4);

        Transform3D rotateX = new Transform3D();
        rotateX.rotX(Math.PI / 4);
        
        Transform3D move = new Transform3D();
        move.setTranslation(new Vector3f(0.0f, -0.95f, 0.0f));
        move.mul(rotateX);
        move.mul(rotateY);
        move.mul(rotateZ);

        TransformGroup grp = new TransformGroup();
        grp.setTransform(move);
        grp.addChild(cylinderBase);

        return grp;

    }
    public BranchGroup createSceneGraph() {
        Cylinder cylinderBase = new Cylinder(0.4f, 1.5f);
        Cylinder cylinderBranch = new Cylinder(0.2f, 1.2f);
        Cylinder cylinderBranch2 = new Cylinder(0.2f, 1.2f);

        Transform3D rotateX = new Transform3D(); 
        rotateX.rotX(Math.PI / 8); 
        Transform3D rotateZCounterClk = new Transform3D();
        rotateZCounterClk.rotZ(Math.PI / -3); 
        Transform3D rotateZClkwise = new Transform3D();
        rotateZClkwise.rotZ(Math.PI / 3); 
        Transform3D moveRight = new Transform3D();
        moveRight.setTranslation(new Vector3f(0.6f, .0f, .0f));
        Transform3D moveLeft = new Transform3D();
        moveLeft.setTranslation(new Vector3f(-.6f, 0.0f, 0.0f));

        rotateX.setScale(.5);
        moveRight.mul(rotateZCounterClk); 
        moveLeft.mul(rotateZClkwise); 

        TransformGroup spinGroup = new TransformGroup();
        TransformGroup firstBranchGrp = new TransformGroup();
        TransformGroup secondBranchGrp = new TransformGroup();
        TransformGroup rotateGroup = new TransformGroup(rotateX);

        firstBranchGrp.setTransform(moveRight);
        firstBranchGrp.addChild(cylinderBranch);

        secondBranchGrp.setTransform(moveLeft);
        secondBranchGrp.addChild(cylinderBranch2);

        spinGroup.addChild(cylinderBase);
        spinGroup.addChild(firstBranchGrp);
        spinGroup.addChild(secondBranchGrp);

        rotateGroup.addChild(spinGroup);

        
        spinGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        spinGroup.addChild(createRotation(spinGroup));

        
        BranchGroup objRoot = new BranchGroup();
        objRoot.addChild(rotateGroup);

        return objRoot;
    }

    public RotationInterpolator createRotation(TransformGroup spinGroup) {
        Alpha rotationAlpha = new Alpha(-1, 10000);
        RotationInterpolator rotator = new RotationInterpolator(
                rotationAlpha, spinGroup);
        BoundingSphere bounds = new BoundingSphere();
        rotator.setSchedulingBounds(bounds);
        return rotator;
    }

    public DirectionalLight createLight() {
        
        Color3f light1Color = new Color3f(0.8f, 1.1f, 0.3f);
        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);

        return light1;
    }

}

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