345
|
1 package alice.test.topology.aquarium;
|
|
2
|
|
3 import java.awt.Image;
|
|
4 import java.awt.MediaTracker;
|
|
5 import java.awt.Toolkit;
|
|
6 import java.io.FileNotFoundException;
|
|
7 import java.net.URL;
|
|
8
|
|
9 import javax.media.j3d.Appearance;
|
|
10 import javax.media.j3d.BranchGroup;
|
|
11 import javax.media.j3d.Texture;
|
|
12 import javax.media.j3d.Transform3D;
|
|
13 import javax.media.j3d.TransformGroup;
|
|
14 import javax.vecmath.Matrix4d;
|
|
15
|
|
16 import com.sun.j3d.loaders.IncorrectFormatException;
|
|
17 import com.sun.j3d.loaders.ParsingErrorException;
|
|
18 import com.sun.j3d.loaders.Scene;
|
|
19 import com.sun.j3d.loaders.objectfile.ObjectFile;
|
|
20
|
|
21 import com.sun.j3d.utils.image.TextureLoader;
|
|
22
|
|
23 public class MakeObject {
|
|
24
|
|
25 private Transform3D transform;
|
|
26 private TransformGroup transform_group;
|
|
27 private ViewChange canvas;
|
|
28 private Matrix4d matrix;
|
|
29 private double s;
|
|
30
|
|
31
|
|
32 public MakeObject(MakeFrame frame){
|
|
33 this.canvas = frame.getCanvas();
|
|
34 this.s = 0.3;
|
|
35 frame.register(this);
|
|
36 }
|
|
37
|
|
38 public BranchGroup createBranch(){
|
|
39 BranchGroup scene = new BranchGroup();
|
|
40 /*Box box = new Box(0.1f,0.1f,0.0f,
|
|
41 Box.GENERATE_NORMALS|Box.GENERATE_TEXTURE_COORDS,createAppearance());*/
|
|
42 ObjectFile obj = new ObjectFile(ObjectFile.RESIZE);
|
|
43 Scene img = null;
|
|
44 try{
|
|
45 URL url=getClass().getClassLoader().getResource("TUNA");
|
|
46 img = obj.load(url);
|
|
47 } catch(FileNotFoundException e){
|
|
48 System.err.println(e);
|
|
49 System.exit(1);
|
|
50 } catch(ParsingErrorException e){
|
|
51 System.err.println(e);
|
|
52 System.exit(1);
|
|
53 } catch(IncorrectFormatException e){
|
|
54 System.err.println(e);
|
|
55 System.exit(1);
|
|
56 }
|
|
57
|
|
58 transform_group = new TransformGroup();
|
|
59 setLocation(-2.0f,-2.0f); //set out of window
|
|
60 transform_group.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
|
|
61 transform_group.addChild(img.getSceneGroup());
|
|
62 scene.addChild(transform_group);
|
|
63 return scene;
|
|
64 }
|
|
65
|
|
66 // no use method
|
|
67 public Appearance createAppearance(){
|
|
68 Appearance app = new Appearance();
|
|
69 Image image = null;
|
|
70 Toolkit toolkit = Toolkit.getDefaultToolkit();
|
|
71 URL url = getClass().getClassLoader().getResource("fish.jpg");
|
|
72 image = toolkit.getImage(url);
|
|
73 MediaTracker mt = new MediaTracker(canvas);
|
|
74 mt.addImage(image, 0);
|
|
75 mt.checkAll(true);
|
|
76 try {
|
|
77 mt.waitForID(0);
|
|
78
|
|
79 }catch (InterruptedException e){
|
|
80 e.printStackTrace();
|
|
81
|
|
82 }
|
|
83 Texture texture = new TextureLoader(image,canvas).getTexture();
|
|
84 app.setTexture(texture);
|
|
85 return app;
|
|
86
|
|
87 }
|
|
88
|
|
89 public void setScale(float size){
|
|
90 s = size;
|
|
91 }
|
|
92
|
|
93 public void setLocation(float x,float y){
|
|
94 transform = new Transform3D();
|
|
95 matrix = new Matrix4d(s,0,0,x,
|
|
96 0,s,0,y,
|
|
97 0,0,s,0,
|
|
98 0,0,0,1);
|
|
99 transform.set(matrix);
|
|
100 transform_group.setTransform(transform);
|
|
101 }
|
|
102
|
|
103 public void setLocation(float x,float y,float z){
|
|
104 transform = new Transform3D();
|
|
105 matrix = new Matrix4d(s,0,0,x,
|
|
106 0,s,0,y,
|
|
107 0,0,s,z,
|
|
108 0,0,0,1);
|
|
109 transform.set(matrix);
|
|
110 transform_group.setTransform(transform);
|
|
111 }
|
|
112
|
|
113
|
|
114 }
|
|
115
|