134
|
1 package alice.test.topology.aquarium;
|
81
|
2
|
|
3 import java.awt.GraphicsConfiguration;
|
109
|
4 import java.awt.image.BufferedImage;
|
|
5 import java.io.File;
|
|
6 import java.io.IOException;
|
81
|
7
|
109
|
8 import javax.imageio.ImageIO;
|
|
9 import javax.media.j3d.Background;
|
81
|
10 import javax.media.j3d.BoundingSphere;
|
|
11 import javax.media.j3d.BranchGroup;
|
|
12 import javax.media.j3d.Canvas3D;
|
|
13 import javax.media.j3d.DirectionalLight;
|
109
|
14 import javax.media.j3d.ImageComponent2D;
|
88
|
15
|
81
|
16 import javax.swing.JFrame;
|
|
17 import javax.swing.JPanel;
|
|
18 import javax.vecmath.Color3f;
|
109
|
19 import javax.vecmath.Point3d;
|
81
|
20 import javax.vecmath.Vector3f;
|
|
21
|
|
22 import com.sun.j3d.utils.universe.SimpleUniverse;
|
|
23 import com.sun.j3d.utils.universe.ViewingPlatform;
|
|
24
|
88
|
25 public class MakeFrame {
|
81
|
26
|
88
|
27 int fSizeX = 800;
|
|
28 int fSizeY = 800;
|
85
|
29 private Canvas3D canvas;
|
88
|
30 private SimpleUniverse universe;
|
127
|
31 private KeyInputCodeSegment kics;
|
81
|
32
|
110
|
33 public MakeFrame(String str){
|
|
34 JFrame frame = new JFrame(str);
|
88
|
35 frame.setSize(fSizeX,fSizeY);
|
|
36 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
81
|
37
|
|
38 JPanel cp = new JPanel();
|
88
|
39 cp.setLayout(null);
|
81
|
40 frame.add(cp);
|
|
41
|
|
42 GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
|
82
|
43 canvas = new Canvas3D(config);
|
88
|
44 canvas.setBounds(0,0,fSizeX,fSizeY);
|
|
45 cp.add(canvas);
|
82
|
46
|
88
|
47 universe = new SimpleUniverse(canvas);
|
|
48 universe.addBranchGraph(createLight());
|
109
|
49 universe.addBranchGraph(setBackground());
|
132
|
50
|
127
|
51 kics = new KeyInputCodeSegment();
|
|
52 canvas.addKeyListener(kics);
|
88
|
53 frame.setVisible(true);
|
81
|
54
|
|
55 ViewingPlatform camera = universe.getViewingPlatform();
|
|
56 camera.setNominalViewingTransform();
|
|
57 }
|
|
58
|
109
|
59 private BranchGroup setBackground(){
|
|
60 BranchGroup scene = new BranchGroup();
|
|
61 BufferedImage img = null;
|
|
62 try {
|
|
63 img = ImageIO.read(new File("../image/image1.jpg"));
|
|
64 } catch (IOException e) {
|
|
65 e.printStackTrace();
|
|
66 }
|
|
67 ImageComponent2D image =
|
|
68 new ImageComponent2D(ImageComponent2D.FORMAT_RGBA8,img);
|
|
69 Background background = new Background(image);
|
|
70 background.setImageScaleMode(Background.SCALE_FIT_ALL);
|
|
71 BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), Double.POSITIVE_INFINITY);
|
|
72 background.setApplicationBounds(bounds);
|
|
73 scene.addChild(background);
|
|
74 return scene;
|
|
75
|
|
76 }
|
|
77
|
81
|
78 private BranchGroup createLight(){
|
|
79 BranchGroup scene = new BranchGroup();
|
|
80 Color3f light_color = new Color3f(1.7f,1.7f,1.7f);
|
|
81 Vector3f light_direction = new Vector3f(0.2f,-0.2f,-0.6f);
|
|
82 DirectionalLight light = new DirectionalLight(light_color,light_direction);
|
|
83 BoundingSphere bounds = new BoundingSphere();
|
|
84 light.setInfluencingBounds(bounds);
|
|
85 scene.addChild(light);
|
|
86 return scene;
|
|
87 }
|
|
88
|
89
|
89 public void register(MakeObject obj){
|
|
90 BranchGroup group = obj.createBranch();
|
88
|
91 this.universe.addBranchGraph(group);
|
|
92 }
|
|
93
|
111
|
94 public SimpleUniverse getUniverse(){
|
|
95 return this.universe;
|
92
|
96 }
|
|
97
|
88
|
98 public Canvas3D getCanvas(){
|
|
99 return this.canvas;
|
81
|
100 }
|
|
101
|
93
|
102 public KeyInputCodeSegment getKeySegment(){
|
127
|
103 return this.kics;
|
81
|
104 }
|
|
105
|
|
106 }
|