返回顶部

收藏

java 3d sphere 球

更多

演示java3d的基本图形

[Java]代码

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);
    }
}

UglyCubeReform.java

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);
    }
}

标签:java,画图,3d

收藏

0人收藏

支持

0

反对

0

相关聚客文章
  1. jabbany 发表 2012-12-12 06:43:06 在Java下用AWT原生绘图制作3D贪食蛇
  2. 喻名堂 发表 2013-01-01 14:53:17 Java100天从入门到精通学习笔记之20、21
  3. 赵 伊凡 发表 2015-11-05 09:25:28 记一次Redis使用超时时间异常
  4. 周亮 发表 2011-08-12 09:43:52 一步一步实现GEF – 3
  5. 博主 发表 2015-08-27 00:00:00 java几种方式实现斐波那契数列
  6. Hessian 发表 2013-03-19 04:27:16 [转]SMALI文件语法
  7. 博主 发表 2014-10-24 03:21:49 redis-toolkit:Java实现的redis工具(一)
  8. 夕水溪下 发表 2013-03-19 02:10:30 自己实现的一个锁
  9. hongjiang 发表 2015-11-19 17:14:41 jmap的bug
  10. Freewind 发表 2013-03-31 12:06:36 调用eclipse的JDT编译java源代码
  11. 博主 发表 2015-11-24 09:13:52 java 程序独立使用tomcat7的jdbc连接池的使用方式
  12. 朴人博客 发表 2013-03-31 02:32:15 JavaScript:世界上误解最深的语言

发表评论