Hello, Java 3D(BeanShell) World!

Posted on 12月 12th, 2012 by cx20

Java 3D(BeanShell)

Java 3D は Java による 3D グラフィックライブラリである。
以下は BeanShell による Java 3D の使用例となっている。

ソースコード

import javax.media.j3d.*;
import javax.swing.JFrame;
import javax.vecmath.Vector3f;
import javax.vecmath.Color3f;
import java.awt.BorderLayout;
import java.awt.GraphicsConfiguration;
import java.awt.Font;
import com.sun.j3d.utils.universe.SimpleUniverse;
 
class Hello extends JFrame {
    Hello( title ) {
        super(title);
        setSize(640, 480);
        setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        canvas3D = createCanvas3D();
        scene = createSceneGraph();
        connect(canvas3D, scene);
    }
 
    Canvas3D createCanvas3D() {
        getContentPane().setLayout(new BorderLayout());
        config = SimpleUniverse.getPreferredConfiguration();
        canvas3D = new Canvas3D(config);
        getContentPane().add(canvas3D);
        return canvas3D;
    }
 
    BranchGroup createSceneGraph() {
        objRoot = new BranchGroup();
        mover = moveTextBack();
        spinner = createSpinner();
        rotator = new RotationInterpolator(new Alpha(-1, 4000), spinner, new Transform3D(), 0.0f, (float)Math.PI * 2);
        rotator.setSchedulingBounds( new BoundingSphere() );
        spinner.addChild(rotator);
        objRoot.addChild(mover);
        mover.addChild(spinner);
        spinner.addChild( createTextShape());
        setLighting(mover);
        return objRoot;
    }
 
    TransformGroup createSpinner() {
        spinner = new TransformGroup();
        spinner.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        return spinner;
    }
 
    TransformGroup moveTextBack() {
        transform3D = new Transform3D();
        transform3D.setTranslation(new Vector3f(0.0f, 0.0f, -10.0f));
        return new TransformGroup(transform3D);
    }
 
    Shape3D createTextShape() {
        textAppear = new Appearance();
        textAppear.setMaterial(new Material());
        font3D = new Font3D(new Font("MS Pゴシック", Font.PLAIN, 1), new FontExtrusion());
        textGeom = new Text3D(font3D, new String("Hello, Java 3D World!"));
        textGeom.setAlignment(Text3D.ALIGN_CENTER);
        textShape = new Shape3D();
        textShape.setGeometry(textGeom);
        textShape.setAppearance(textAppear);
        return textShape;
    }
 
    void setLighting(objMove) {
        light = new DirectionalLight();
        light.setInfluencingBounds(new BoundingSphere());
        light.setDirection(new Vector3f(0.0f,0.0f,-1.0f));
        light.setColor(new Color3f(1.0f, 1.0f, 1.0f));
        objMove.addChild(light);
    }
 
    void connect(canvas3D, scene) {
        universe = new SimpleUniverse(canvas3D);
        universe.getViewingPlatform().setNominalViewingTransform();
        universe.addBranchGraph(scene);
    }
}
 
frame = new Hello("Hello, World");
frame.setVisible(true);

実行方法

C:¥> SET CLASSPATH=bsh-2.0b4.jar;%CLASSPATH%
C:¥> java bsh.Interpreter hello.bsh

実行結果

+------------------------------------------+
|Hello, World!                    [_][~][X]|
+------------------------------------------+
|Hello, Java 3D World!                     |
|                                          |
|                                          |
|                                          |
|                                          |
|                                          |
|                                          |
|                                          |
|                                          |
|                                          |
+------------------------------------------+

Tags:

Categories: BeanShell, Java, Java 3D

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

WP-SpamFree by Pole Position Marketing