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