返回顶部

收藏

java3d sphere 球

更多
package test;

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GraphicsConfiguration;

import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.behaviors.mouse.MouseRotate;
import com.sun.j3d.utils.behaviors.mouse.MouseTranslate;
import com.sun.j3d.utils.behaviors.mouse.MouseWheelZoom;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3d;
import javax.vecmath.Vector3f;

public class UglyCubeReform extends Applet {

    private static final long serialVersionUID = 1L;

    public UglyCubeReform() {
        setLayout(new BorderLayout());
        GraphicsConfiguration configuration
                =SimpleUniverse.getPreferredConfiguration();
        Canvas3D canvas = new Canvas3D(configuration);

        add(canvas);
        SimpleUniverse u = new SimpleUniverse(canvas);

        u.addBranchGraph(getscene());
        }

    private BranchGroup getscene() {
         BranchGroup scene = new BranchGroup();
          BoundingSphere boundingSphere=new BoundingSphere(new Point3d(0,0,0),100.0f);

          Color3f bgcolor3f=new Color3f(Color.green);
          Background background=new Background(bgcolor3f);
          background.setApplicationBounds(boundingSphere);
          scene.addChild(background);

          Color3f dirColor3f=new Color3f(Color.ORANGE);
          DirectionalLight directionalLight=
              new DirectionalLight(dirColor3f,new Vector3f(3,0,-3.0f));
          directionalLight.setInfluencingBounds(boundingSphere);
          scene.addChild(directionalLight);

          Transform3D transform3d=new Transform3D();
          transform3d.setTranslation(new Vector3d(0,0,-7));

          TransformGroup transformGroup=new TransformGroup(transform3d);
          transformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
          transformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
          transformGroup.setTransform(transform3d);

          Transform3D sphereTransform3d =new Transform3D();
          sphereTransform3d.setTranslation(new Vector3d(0,0,-6));
          TransformGroup sphereGroup =new TransformGroup(sphereTransform3d);
          sphereGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
          sphereGroup.setTransform(sphereTransform3d);
          Sphere sphere=new Sphere(4.0f,-1,20);
          PolygonAttributes polygonAttributes=new PolygonAttributes();

          polygonAttributes.setPolygonMode(PolygonAttributes.POLYGON_LINE);
          polygonAttributes.setCullFace(PolygonAttributes.CULL_NONE);
          Appearance appearance=new Appearance();

          appearance.setPolygonAttributes(polygonAttributes);

          Material sphereMaterial=new Material();
          sphereMaterial.setEmissiveColor(new Color3f(Color.black));

          appearance.setMaterial(sphereMaterial);

          sphere.setAppearance(appearance);

          sphereGroup.addChild(sphere);

          Alpha sphereAlpha=new Alpha(-1, Alpha.INCREASING_ENABLE |
                  Alpha.DECREASING_ENABLE,
                  0, 0,
                  5000, 2500, 200,
                  5000, 2500, 200);

          PositionInterpolator positionInterpolator=
              new PositionInterpolator(sphereAlpha, sphereGroup, 
                      sphereTransform3d, -4.0f, 4.0f);
          positionInterpolator.setSchedulingBounds(boundingSphere);

          sphereGroup.addChild(positionInterpolator);

          transformGroup.addChild(sphereGroup);
          /***/

            MouseWheelZoom mouseWheelZoom=new MouseWheelZoom();
            mouseWheelZoom.setTransformGroup(transformGroup);
            mouseWheelZoom.setSchedulingBounds(boundingSphere);
            transformGroup.addChild(mouseWheelZoom);

            MouseRotate mouseRotate=new MouseRotate();
            mouseRotate.setTransformGroup(transformGroup);
            mouseRotate.setSchedulingBounds(boundingSphere);
            transformGroup.addChild(mouseRotate);

            MouseTranslate mouseTranslate=new MouseTranslate();
            mouseTranslate.setTransformGroup(transformGroup);
            mouseTranslate.setSchedulingBounds(boundingSphere);
            transformGroup.addChild(mouseTranslate);
          scene.addChild(transformGroup);

        return scene;
    }

    public static void main(String[] args) {
        new MainFrame(new UglyCubeReform(), 556, 556);
    }
}
//该片段来自于http://outofmemory.cn

标签:java,图形

收藏

0人收藏

支持

0

反对

0

发表评论