Archive for 12月 24th, 2012
-
Hello, Java 3D(Rhino) World!
Posted on 12月 24th, 2012 by cx20
Java 3D(Rhino)
Java 3D は Java による 3D グラフィックライブラリである。
以下は Rhino による Java 3D の使用例となっている。ソースコード
importPackage(java.awt); importPackage(javax.swing); importPackage(javax.media.j3d); importPackage(javax.vecmath); importPackage(com.sun.j3d.utils.universe); var frame = new JFrame(); frame.setTitle("Hello, World"); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); frame.setSize(640,480); var canvas3D = createCanvas3D(frame); var scene = createSceneGraph(); connect(canvas3D, scene); frame.setVisible(true); function createCanvas3D( frame ) { frame.getContentPane().setLayout(new BorderLayout()); var config = SimpleUniverse.getPreferredConfiguration(); var canvas3D = new Canvas3D(config); frame.getContentPane().add(canvas3D); return canvas3D; } function createSceneGraph() { var objRoot = new BranchGroup(); var mover = moveTextBack(); var spinner = createSpinner(); var rotator = new RotationInterpolator(new Alpha(-1, 4000), spinner, new Transform3D(), 0.0, Math.PI * 2); rotator.setSchedulingBounds( new BoundingSphere() ); spinner.addChild(rotator); objRoot.addChild(mover); mover.addChild(spinner); spinner.addChild( createTextShape()); setLighting(mover); return objRoot; } function createSpinner() { var spinner = new TransformGroup(); spinner.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); return spinner; } function moveTextBack() { var transform3D = new Transform3D(); transform3D.setTranslation(new Vector3f(0.0, 0.0, -10.0)); return new TransformGroup(transform3D); } function createTextShape() { var textAppear = new Appearance(); textAppear.setMaterial(new Material()); var font3D = new Font3D(new Font("MS Pゴシック", Font.PLAIN, 1), new FontExtrusion()); var textGeom = new Text3D(font3D, new String("Hello, Java 3D World!")); textGeom.setAlignment(Text3D.ALIGN_CENTER); var textShape = new Shape3D(); textShape.setGeometry(textGeom); textShape.setAppearance(textAppear); return textShape; } function setLighting( objMove ) { var light = new DirectionalLight(); light.setInfluencingBounds(new BoundingSphere()); light.setDirection(new Vector3f(0.0,0.0,-1.0)); light.setColor(new Color3f(1.0, 1.0, 1.0)); objMove.addChild(light); } function connect( canvas3D, scene ) { var universe = new SimpleUniverse(canvas3D); universe.getViewingPlatform().setNominalViewingTransform(); universe.addBranchGraph(scene); } while(1) {java.lang.Thread.sleep(1000);}
実行方法
C:¥> jrunscript Hello.js
実行結果
+------------------------------------------+ |Hello, World! [_][~][X]| +------------------------------------------+ |Hello, Java 3D World! | | | | | | | | | | | | | | | | | | | +------------------------------------------+