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;
|
81
|
31
|
110
|
32 public MakeFrame(String str){
|
|
33 JFrame frame = new JFrame(str);
|
88
|
34 frame.setSize(fSizeX,fSizeY);
|
|
35 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
81
|
36
|
|
37 JPanel cp = new JPanel();
|
88
|
38 cp.setLayout(null);
|
81
|
39 frame.add(cp);
|
|
40
|
|
41 GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
|
82
|
42 canvas = new Canvas3D(config);
|
88
|
43 canvas.setBounds(0,0,fSizeX,fSizeY);
|
|
44 cp.add(canvas);
|
82
|
45
|
88
|
46 universe = new SimpleUniverse(canvas);
|
|
47 universe.addBranchGraph(createLight());
|
109
|
48 universe.addBranchGraph(setBackground());
|
132
|
49
|
138
|
50 canvas.addKeyListener(new KeyInputCodeSegment());
|
88
|
51 frame.setVisible(true);
|
81
|
52
|
|
53 ViewingPlatform camera = universe.getViewingPlatform();
|
|
54 camera.setNominalViewingTransform();
|
|
55 }
|
|
56
|
109
|
57 private BranchGroup setBackground(){
|
|
58 BranchGroup scene = new BranchGroup();
|
|
59 BufferedImage img = null;
|
|
60 try {
|
|
61 img = ImageIO.read(new File("../image/image1.jpg"));
|
|
62 } catch (IOException e) {
|
|
63 e.printStackTrace();
|
|
64 }
|
|
65 ImageComponent2D image =
|
|
66 new ImageComponent2D(ImageComponent2D.FORMAT_RGBA8,img);
|
|
67 Background background = new Background(image);
|
|
68 background.setImageScaleMode(Background.SCALE_FIT_ALL);
|
|
69 BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), Double.POSITIVE_INFINITY);
|
|
70 background.setApplicationBounds(bounds);
|
|
71 scene.addChild(background);
|
|
72 return scene;
|
|
73
|
|
74 }
|
|
75
|
81
|
76 private BranchGroup createLight(){
|
|
77 BranchGroup scene = new BranchGroup();
|
|
78 Color3f light_color = new Color3f(1.7f,1.7f,1.7f);
|
|
79 Vector3f light_direction = new Vector3f(0.2f,-0.2f,-0.6f);
|
|
80 DirectionalLight light = new DirectionalLight(light_color,light_direction);
|
|
81 BoundingSphere bounds = new BoundingSphere();
|
|
82 light.setInfluencingBounds(bounds);
|
|
83 scene.addChild(light);
|
|
84 return scene;
|
|
85 }
|
|
86
|
89
|
87 public void register(MakeObject obj){
|
|
88 BranchGroup group = obj.createBranch();
|
88
|
89 this.universe.addBranchGraph(group);
|
|
90 }
|
|
91
|
111
|
92 public SimpleUniverse getUniverse(){
|
|
93 return this.universe;
|
92
|
94 }
|
|
95
|
88
|
96 public Canvas3D getCanvas(){
|
|
97 return this.canvas;
|
81
|
98 }
|
|
99 }
|