view src/alice/test/topology/aquarium/MakeFrame.java @ 146:36dc63d1bdcf working

read image from jar
author sugi
date Tue, 25 Sep 2012 14:11:37 +0900
parents 287aae21e7d8
children 6a69891b7232
line wrap: on
line source

package alice.test.topology.aquarium;

import java.awt.GraphicsConfiguration;
import java.awt.image.BufferedImage;
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 int fSizeX = 800;
	private int fSizeY = 800;
	private ViewChange canvas;
	private JFrame frame;
	private ObjectList list = new ObjectList();
	
	public MakeFrame(String str){
		frame = new JFrame(str);
		frame.setSize(fSizeX,fSizeY);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JPanel cp = new JPanel();
		cp.setLayout(null);
		frame.add(cp);
		
		GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
		canvas = new ViewChange(3.0f,0.01f,config);
		canvas.setBounds(0,0,fSizeX,fSizeY);
		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");
			img = ImageIO.read(url);
		} 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(0.0, 0.0, 0.0), Double.POSITIVE_INFINITY);
		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.2f,-0.2f,-0.6f);
		DirectionalLight light = new DirectionalLight(light_color,light_direction);
		BoundingSphere bounds = new BoundingSphere();
		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;
	}
	
}