comparison src/alice/test/topology/movement/FishMovement.java @ 81:02dfb6c72632 working

This is movement test
author sugi
date Thu, 24 May 2012 00:20:16 +0900
parents
children 5cf20458b9e0
comparison
equal deleted inserted replaced
80:54f5b5496bfe 81:02dfb6c72632
1 package alice.test.topology.movement;
2
3 import java.awt.GraphicsConfiguration;
4
5 import javax.media.j3d.Appearance;
6 import javax.media.j3d.BoundingSphere;
7 import javax.media.j3d.BranchGroup;
8 import javax.media.j3d.Canvas3D;
9 import javax.media.j3d.DirectionalLight;
10 import javax.media.j3d.Transform3D;
11 import javax.media.j3d.TransformGroup;
12 import javax.swing.JFrame;
13 import javax.swing.JPanel;
14 import javax.vecmath.Color3f;
15 import javax.vecmath.Vector3f;
16
17 import com.sun.j3d.utils.geometry.Box;
18 import com.sun.j3d.utils.universe.SimpleUniverse;
19 import com.sun.j3d.utils.universe.ViewingPlatform;
20
21 public final class FishMovement {
22
23 private Vector3f vector = null;
24 private Transform3D transform1 = null;
25 private TransformGroup transform_group = null;
26
27 private static class instanceHolder{
28 private static final FishMovement instance = new FishMovement();
29 }
30 public static FishMovement getInstance() {
31 return instanceHolder.instance;
32
33 }
34
35 private FishMovement(){
36 JFrame frame = new JFrame();
37 frame.setSize(1000,1000);
38 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
39 JPanel cp = new JPanel();
40 frame.add(cp);
41
42
43 GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
44
45 Canvas3D canvas = new Canvas3D(config);
46 canvas.setBounds(0,0,1000,1000);//set canvas size
47 cp.add(canvas);//register canvas with ContentPane
48
49 SimpleUniverse universe = new SimpleUniverse(canvas);
50 frame.setVisible(true);
51 BranchGroup group1 = createBranch();
52 universe.addBranchGraph(group1);
53
54 BranchGroup group2 = createLight();
55 universe.addBranchGraph(group2);
56
57 ViewingPlatform camera = universe.getViewingPlatform();
58 camera.setNominalViewingTransform();
59 }
60
61 private BranchGroup createBranch(){
62 BranchGroup scene = new BranchGroup();
63 transform_group = new TransformGroup();
64 scene.addChild(transform_group);
65 transform_group.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
66 Box box = new Box(0.4f,0.4f,0.8f,Box.GENERATE_TEXTURE_COORDS,createAppearance());
67 transform_group.addChild(box);
68 setLocation(0.0f,0.0f);
69 return scene;
70 }
71
72 private Appearance createAppearance() {
73 Appearance app = new Appearance();
74 /*
75 Image image = null;
76 Toolkit toolkit = Toolkit.getDefaultToolkit();
77 image = toolkit.getImage("fish1.jpg");
78
79 MediaTracker mt = new MediaTracker();
80 mt.addImage(image, 0);
81 mt.checkAll(true);
82 try {
83 mt.waitForID(0);
84
85 }catch (InterruptedException e){
86 e.printStackTrace();
87
88 }
89 Texture texture = new TextureLoader(image,this).getTexture();
90 app.setTexture(texture);
91 */
92 return app;
93 }
94
95 private BranchGroup createLight(){
96 BranchGroup scene = new BranchGroup();
97 Color3f light_color = new Color3f(1.7f,1.7f,1.7f);
98 Vector3f light_direction = new Vector3f(0.2f,-0.2f,-0.6f);
99 DirectionalLight light = new DirectionalLight(light_color,light_direction);
100 BoundingSphere bounds = new BoundingSphere();
101 light.setInfluencingBounds(bounds);
102 scene.addChild(light);
103 return scene;
104 }
105
106 public void setLocation(float x,float y){
107 transform1 = new Transform3D();
108 vector = new Vector3f(x,y,0.0f);
109 transform1.setTranslation(vector);
110 transform_group.setTransform(transform1);
111 }
112
113 public void setLocation(float x,float y,float z){
114 transform1 = new Transform3D();
115 vector = new Vector3f(x,y,z);
116 transform1.setTranslation(vector);
117 transform_group.setTransform(transform1);
118 }
119
120
121
122
123 }