view src/alice/test/topology/movement/MakeObject.java @ 93:6601f8854126 working

can KeyInputCodeSegment
author sugi
date Tue, 05 Jun 2012 16:33:28 +0900
parents 19f3bd33277d
children 993a9f194615
line wrap: on
line source

package alice.test.topology.movement;

import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.io.File;

import javax.media.j3d.Appearance;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.Texture;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Vector3f;

import com.sun.j3d.utils.geometry.Box;
import com.sun.j3d.utils.image.TextureLoader;

public class MakeObject {
	
	public Vector3f vector;
	Transform3D transform;  
	TransformGroup transform_group;
	Canvas3D canvas;
	KeyInput key;
	KeyInputCodeSegment KIC; 
	
	public MakeObject(MakeFrame MF){
		this.canvas = MF.getCanvas();
		this.key = MF.getKey();
		this.KIC = MF.getKeySegment();
	}
	
		
	public BranchGroup createKeyBranch(){
		BranchGroup scene = new BranchGroup();
		Box box = new Box(0.1f,0.1f,0.0f,
				Box.GENERATE_NORMALS|Box.GENERATE_TEXTURE_COORDS,createAppearance());
		key.transformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
		key.transformGroup.addChild(box);
		scene.addChild(key.transformGroup);
		return scene;
		
	}
	
	public BranchGroup createBranch(){
		BranchGroup scene = new BranchGroup();
		Box box = new Box(0.1f,0.1f,0.0f,
				Box.GENERATE_NORMALS|Box.GENERATE_TEXTURE_COORDS,createAppearance());	
		transform_group = new TransformGroup();
		transform_group.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
		transform_group.addChild(box);
		scene.addChild(transform_group);
		setLocation(-10,0);//default position
		return scene;
		
	}
	
	private Appearance createAppearance(){
		System.out.println(new File(".").getAbsolutePath());
		Appearance app = new Appearance();
		Image image = null; 	
		Toolkit toolkit = Toolkit.getDefaultToolkit();
		//image = toolkit.getImage("image/fish.jpg");
		image = toolkit.getImage("../image/fish.jpg");//jar
		MediaTracker mt = new MediaTracker(canvas);
		mt.addImage(image, 0);
		mt.checkAll(true);
		try {
			mt.waitForID(0);
			
		}catch (InterruptedException e){
			e.printStackTrace();
			
		}
		Texture texture = new TextureLoader(image,canvas).getTexture();
		app.setTexture(texture);
		return app;
		
	}
	
	public void setLocation(float x,float y){
		transform = new Transform3D();
		vector = new Vector3f(x,y,0.0f);
		transform.setTranslation(vector);
		transform_group.setTransform(transform);
	}
}