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