[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Java3Djp:00081] Re: 書籍「 Java3D プログラミング・バイブル」 4/8 出版



えんどうです。

> [書籍プレゼントのお知らせ]
> 
> Java3Djp ML参加者の中から抽選で3名の方に
> 「Java 3Dプログラミング・バイブル」をプレゼントします。

当選発表です。

応募総数 15 通

当選者

 Hiroki Kinoshita さん <kino@xxxxxxxxxxxxxxxxxxxxxx>
 Seigo Abo        さん <s_abo@xxxxxxxxxxxxxxxxxxxxx>
 YAJI Takayuki    さん <ujb00006@xxxxxxxxx>

当選された方は j3dpb@xxxxxxxxxxxx あてに、賞品の送り先を
ご連絡ください。


抽選にはJava 3D抽選アプレットを使用しました。

起動方法: java Strike <文字列1> <文字列2> ...
使い方: 回転する青い文字にColorCubeを当てる。
        ColorCubeの方向はマウスで回転できる。
        当たった文字列の色が赤く変化する。

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.GraphicsConfiguration;
import java.awt.Font;

import javax.vecmath.Vector3d;
import javax.vecmath.Point3d;
import javax.vecmath.Color3f;

import javax.media.j3d.Canvas3D;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.Font3D;
import javax.media.j3d.FontExtrusion;
import javax.media.j3d.Text3D;
import javax.media.j3d.Appearance;
import javax.media.j3d.Shape3D;
import javax.media.j3d.Alpha;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.RotationInterpolator;
import javax.media.j3d.PositionInterpolator;
import javax.media.j3d.Node;
import javax.media.j3d.ColoringAttributes;

import com.sun.j3d.utils.universe.SimpleUniverse;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.ColorCube;
import com.sun.j3d.utils.behaviors.mouse.MouseRotate;

public class Strike extends Applet {
    public Strike(String[] labels) {
	GraphicsConfiguration config =
	    SimpleUniverse.getPreferredConfiguration();
	Canvas3D canvas = new Canvas3D(config);
	this.setLayout(new BorderLayout());
	this.add(canvas, BorderLayout.CENTER);
	SimpleUniverse universe = new SimpleUniverse(canvas);
	universe.getViewingPlatform().setNominalViewingTransform();
	universe.addBranchGraph(createSceneGraph(labels));
    }

    public BranchGroup createSceneGraph(String[] labels) {
	BranchGroup root = new BranchGroup();

	Transform3D transform3D = new Transform3D();
	transform3D.setTranslation(new Vector3d(0.0, 0.0, -9.0));
	TransformGroup trans = new TransformGroup(transform3D);
	root.addChild(trans);

	Transform3D rotation3D = new Transform3D();
	rotation3D.rotX(Math.PI/2.0);
        TransformGroup upTrans = new TransformGroup(rotation3D);
	trans.addChild(upTrans);
       
	TransformGroup rotation = new TransformGroup();
	rotation.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
	rotation.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
	upTrans.addChild(rotation);

	BoundingSphere bounds = new BoundingSphere(new Point3d(), 100.0);
	Alpha alpha = new Alpha(-1, 8000);
	RotationInterpolator interpolator =
	    new RotationInterpolator(alpha, rotation);
	interpolator.setSchedulingBounds(bounds);
	root.addChild(interpolator);

	rotation.addChild(new Target(labels));

	TransformGroup buletTrans = new TransformGroup();
	buletTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
	buletTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
	root.addChild(buletTrans);

	MouseRotate mouseRotate = new MouseRotate(buletTrans);
	mouseRotate.setSchedulingBounds(bounds);
	root.addChild(mouseRotate);

	TransformGroup buletPosition = new TransformGroup();
	buletPosition.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
	buletPosition.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
	buletTrans.addChild(buletPosition);

	Transform3D zAxis = new Transform3D();
	zAxis.rotY(Math.PI/2.0);
	PositionInterpolator positionInterpolator =
	    new PositionInterpolator(new Alpha(-1, 5000), buletPosition,
				     zAxis, 0.0f, 10.0f);
	positionInterpolator.setSchedulingBounds(bounds);
	root.addChild(positionInterpolator);

	ColorCube bulet = new ColorCube(0.5);
	CollisionCallback callback =
	    new CollisionCallback(){
		public void crash(Node node) {
		    System.out.println(node.getUserData());
		    ColoringAttributes cattr =
			new ColoringAttributes(new Color3f(1.0f, 0.0f, 0.0f),
					       ColoringAttributes.FASTEST);
		    ((Shape3D)node).getAppearance().setColoringAttributes(cattr);
		}
	    };
	SimpleCollisionBehavior scb =
	    new SimpleCollisionBehavior(bulet, callback);
	scb.setSchedulingBounds(bounds);
	root.addChild(scb);

	buletPosition.addChild(bulet);

	return root;
    }

    public static void main(String[] args) {
	new MainFrame(new Strike(args), 500, 500);
    }

    private class Target extends TransformGroup {
	public Target(String[] labels) {
	    Transform3D translate3D = new Transform3D();
	    translate3D.setTranslation(new Vector3d(0.0, 0.0, -4.0));
	    TransformGroup translation = new TransformGroup(translate3D);

	    Transform3D textTrans3D = new Transform3D();
	    textTrans3D.rotX(-(Math.PI/2.0));
	    TransformGroup textTrans = new TransformGroup(textTrans3D);
	    
	    for (int i=0; i<labels.length; i++) {
		Transform3D rotation3D = new Transform3D();
		rotation3D.rotY((2.0 *Math.PI) / (double)labels.length * (double)i);
		TransformGroup rotation = new TransformGroup(rotation3D);
		this.addChild(rotation);
		TransformGroup trans = (TransformGroup)translation.cloneTree();
		rotation.addChild(trans);
		TransformGroup textTransform = (TransformGroup)textTrans.cloneTree(); 
		trans.addChild(textTransform);
		Font3D font3D =
		    new Font3D( new Font("TestFont", Font.PLAIN, 1), new FontExtrusion());
		Text3D text3D = new Text3D(font3D, labels[i]);
		text3D.setAlignment(Text3D.ALIGN_CENTER);
		Appearance app = new Appearance();
		app.setCapability(Appearance.ALLOW_COLORING_ATTRIBUTES_READ);
		app.setCapability(Appearance.ALLOW_COLORING_ATTRIBUTES_WRITE);
		ColoringAttributes cattr =
		    new ColoringAttributes(new Color3f(0.0f, 0.0f, 1.0f),
					   ColoringAttributes.FASTEST);
		app.setColoringAttributes(cattr);
		Shape3D shape = new Shape3D(text3D, app);
		shape.setCapability(Shape3D.ALLOW_APPEARANCE_READ);
		shape.setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
		shape.setUserData(labels[i]);
		textTransform.addChild(shape);
	    }
	}
    }
}


-- 
ENDO Yasuyuki <yasuyuki@xxxxxxxxxxxx>
http://www.javaopen.org/yasuyuki/ (Personal/Japanese Only)
http://www.javaopen.org/jfriends/ (Japanese Only)