view src/main/java/alice/test/topology/aquarium/MakeFrame.java @ 345:8f71c3e6f11d

Change directory structure Maven standard
author sugi
date Wed, 16 Apr 2014 18:26:07 +0900
parents
children 4f534c07d41e
line wrap: on
line source

package alice.test.topology.aquarium;

import java.awt.GraphicsConfiguration;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.media.j3d.Background;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.ImageComponent2D;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3f;

import com.sun.j3d.utils.universe.SimpleUniverse;

public class MakeFrame {
	
	private static final int F_SIZE_X = 800;
	private static final int F_SIZE_Y = 800;
	private ViewChange canvas;
	private JFrame frame;
	private ObjectList list = new ObjectList();
	
	public MakeFrame(String str,float x) {
	    System.loadLibrary("jawt"); 
		frame = new JFrame(str);
		frame.setSize(F_SIZE_X, F_SIZE_Y);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JPanel cp = new JPanel();
		cp.setLayout(null);
		frame.add(cp);
		
		GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
		canvas = new ViewChange(x,0.01f,config);
		canvas.setBounds(0,0, F_SIZE_X, F_SIZE_Y);
		cp.add(canvas);
		
		canvas.universe.addBranchGraph(createLight());
		canvas.universe.addBranchGraph(setBackground());
		
		canvas.addKeyListener(new KeyInputCodeSegment(this));
		frame.setVisible(true);
		
	}
	
	private BranchGroup setBackground() {
		BranchGroup scene = new BranchGroup();
		BufferedImage img = null;
		try {
			URL url = getClass().getClassLoader().getResource("image1.jpg");
			if (url!=null) {
			    img = ImageIO.read(url);
			} else {
			    img = ImageIO.read(new File("image/image1.jpg"));
			}
		} catch (IOException e) {
		  	e.printStackTrace();
		}
		ImageComponent2D image = 
				new ImageComponent2D(ImageComponent2D.FORMAT_RGBA8,img);
		Background background = new Background(image);
		background.setImageScaleMode(Background.SCALE_FIT_ALL);
		BoundingSphere bounds = new BoundingSphere(new Point3d(), 10.0);
		background.setApplicationBounds(bounds);
		scene.addChild(background);
		return scene;
		
	}
	
	private BranchGroup createLight() {
		BranchGroup scene = new BranchGroup();
		Color3f light_color  = new Color3f(1.7f,1.7f,1.7f);
		Vector3f light_direction = new Vector3f(0.0f,0.0f,-1.0f);
		DirectionalLight light = new DirectionalLight(light_color,light_direction);
		BoundingSphere bounds = new BoundingSphere(new Point3d(), 10.0);
		light.setInfluencingBounds(bounds);
		scene.addChild(light);
		return scene;
	}
	
	public void register(MakeObject obj){
		list.table.add(obj);
		BranchGroup group = obj.createBranch();
		this.canvas.universe.addBranchGraph(group);
	}
	
	public ViewChange getCanvas(){
		return this.canvas;
	}
	
	public JFrame getJFrame(){
		return this.frame;
	}
	
	public ObjectList getList(){
		return this.list;
	}
	
	static public void main(String [] args) {
	    MakeFrame test = new MakeFrame("Test", 0);
	    test.setBackground();
	    test.createLight();
	}
	
}