返回顶部

收藏

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

相关聚客文章
  1. 博主 发表 2009-10-03 04:00:00 艰难的开垦java图形
  2. calvin 发表 2015-04-21 04:28:38 JDK数则
  3. 加多 发表 2018-06-22 07:56:46 分布式事务- 二阶段协议
  4. 博主 发表 2014-01-21 00:00:00 ubuntu编译jdk
  5. TiuVe2 发表 2018-06-24 04:01:52 Java:关于值传递你需要了解的事情
  6. 博主 发表 2013-02-22 08:00:00 Java非托管堆存储数据
  7. 邓龙华 发表 2018-06-27 15:11:43 Java 模板引擎 Beetl 2.8.5 发布,增加根对象支持
  8. weager 发表 2015-05-07 18:06:15 一点数据库访问优化的思考
  9. 方 腾飞 发表 2018-06-30 05:16:37 认知升级-成败取决于见识
  10. lixiangmaojack 发表 2015-02-25 14:00:01 The IoC container – Spring
  11. lishirong 发表 2012-09-21 01:59:37 用mapXtreme Java开发web gis应用
  12. 博主 发表 2015-04-03 15:13:17 Java编程思想笔记三

发表评论