Java3DPlover.java


Java3DPlover.java

★★ Java の宿題ここで答えます Part 61 ★★715

/* -classpath .;j3dutils.jar;j3dcore.jar;vecmath.jar */

import java.util.Random;

import javax.swing.*;
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.GraphicsConfiguration;
import java.awt.event.*;

import javax.media.j3d.*;
import javax.vecmath.*;

import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.behaviors.mouse.MouseRotate;
import com.sun.j3d.utils.behaviors.mouse.MouseTranslate;
import com.sun.j3d.utils.behaviors.mouse.MouseZoom;
import com.sun.j3d.utils.behaviors.vp.OrbitBehavior;

public class Java3DPlover extends Applet{
	public static void main(String[] args){
		Java3DPlover p = new Java3DPlover();
		p.run();
	}

	private class Plover{
		int elements = 0;
		Point3d[] vertex = null;
		Color3f[] colors = null;

		public Plover(int elements){
			this.elements = elements;
			vertex = new Point3d[elements];
			colors = new Color3f[elements];
		}

		public Shape3D getShape(){
			Float x = 0.0f, y = 0.0f, z = 0.0f;
			vertex[0] = new Point3d(x, y, z);
			colors[0] = new Color3f(1.0f, 1.0f, 1.0f);
			Random r = new Random();
			for(int i = 1; i < elements; i++){
				Double d = r.nextDouble();
				if(d < 1.0f / 6){                         x += 0.1f;
				}else if(d >= 1.0f / 6 && d < 2.0f / 6){ x -= 0.1f;
				}else if(d >= 2.0f / 6 && d < 3.0f / 6){ y += 0.1f;
				}else if(d >= 3.0f / 6 && d < 4.0f / 6){ y -= 0.1f;
				}else if(d >= 4.0f / 6 && d < 5.0f / 6){ z += 0.1f;
				}else if(d >= 5.0f / 6){                 z -= 0.1f;
				}
				vertex[i] = new Point3d(x, y, z);
				colors[i] = new Color3f(r.nextFloat(), r.nextFloat(), r.nextFloat());
			}

			LineStripArray geom = new LineStripArray(
				vertex.length,
				GeometryArray.COORDINATES | GeometryArray.COLOR_3,
				new int[]{elements}
			);
			geom.setCoordinates(0, vertex);
			geom.setColors(0, colors);

			return new Shape3D(geom);
		}
	}

	public BranchGroup createSceneGraph(){
		BranchGroup root = new BranchGroup();

		TransformGroup tg1 = new TransformGroup();
		tg1.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
		tg1.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);

		MouseRotate mrotate = new MouseRotate(tg1);
		MouseTranslate mtrans = new MouseTranslate(tg1);
		MouseZoom mzoom = new MouseZoom(tg1);

		BoundingSphere bsphere = new BoundingSphere();
		bsphere.setRadius(10.0f);
		mrotate.setSchedulingBounds(bsphere);
		mtrans.setSchedulingBounds(bsphere);
		mzoom.setSchedulingBounds(bsphere);

		tg1.addChild(mrotate);
		tg1.addChild(mtrans);
		tg1.addChild(mzoom);
		tg1.addChild((new Plover(100)).getShape());

		Transform3D transf1 = new Transform3D();
		transf1.set(new Vector3d(0.0f, 0.0f, -1.0f));
		tg1.setTransform(transf1);

		root.addChild(tg1);
		root.compile();
		return root;
	}

	public void run(){
		JFrame frm = new JFrame("Java3DPlover");
		frm.setBounds(10, 10, 240, 240);
		frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		frm.setLayout(new BorderLayout());
		GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
		Canvas3D canvas = new Canvas3D(config);
		frm.add(canvas, BorderLayout.CENTER);
		BranchGroup scene = createSceneGraph();
		SimpleUniverse universe = new SimpleUniverse(canvas);
		universe.getViewingPlatform().setNominalViewingTransform();
		universe.addBranchGraph(scene);

		OrbitBehavior orbit = new OrbitBehavior(canvas, OrbitBehavior.REVERSE_ALL);
		orbit.setSchedulingBounds(new BoundingSphere(new Point3d(0, 0, 0), 100.0));
		universe.getViewingPlatform().setViewPlatformBehavior(orbit);

		frm.setVisible(true);
	}

	public void init(){
		run();
	}
}

CONTENTS

最新の20件

2020-11-14 2005-12-06 2006-11-04 2012-07-15 2009-06-19 2011-03-03 2006-12-13 2007-11-05 2014-07-22 2014-07-19 2014-07-09 2014-01-14 2012-09-03 2012-03-28

今日の8件

人気の30件

  • counter: 2894
  • today: 1
  • yesterday: 0
  • online: 1