345
|
1 package alice.test.topology.aquarium;
|
|
2
|
|
3 import java.awt.GraphicsConfiguration;
|
|
4 import java.awt.image.BufferedImage;
|
|
5 import java.io.File;
|
|
6 import java.io.IOException;
|
|
7 import java.net.URL;
|
|
8
|
|
9 import javax.imageio.ImageIO;
|
|
10 import javax.media.j3d.Background;
|
|
11 import javax.media.j3d.BoundingSphere;
|
|
12 import javax.media.j3d.BranchGroup;
|
|
13 import javax.media.j3d.DirectionalLight;
|
|
14 import javax.media.j3d.ImageComponent2D;
|
|
15
|
|
16 import javax.swing.JFrame;
|
|
17 import javax.swing.JPanel;
|
|
18 import javax.vecmath.Color3f;
|
|
19 import javax.vecmath.Point3d;
|
|
20 import javax.vecmath.Vector3f;
|
|
21
|
|
22 import com.sun.j3d.utils.universe.SimpleUniverse;
|
|
23
|
|
24 public class MakeFrame {
|
|
25
|
|
26 private static final int F_SIZE_X = 800;
|
|
27 private static final int F_SIZE_Y = 800;
|
|
28 private ViewChange canvas;
|
|
29 private JFrame frame;
|
|
30 private ObjectList list = new ObjectList();
|
|
31
|
|
32 public MakeFrame(String str,float x) {
|
|
33 System.loadLibrary("jawt");
|
|
34 frame = new JFrame(str);
|
|
35 frame.setSize(F_SIZE_X, F_SIZE_Y);
|
|
36 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
37
|
|
38 JPanel cp = new JPanel();
|
|
39 cp.setLayout(null);
|
|
40 frame.add(cp);
|
|
41
|
|
42 GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
|
|
43 canvas = new ViewChange(x,0.01f,config);
|
|
44 canvas.setBounds(0,0, F_SIZE_X, F_SIZE_Y);
|
|
45 cp.add(canvas);
|
|
46
|
|
47 canvas.universe.addBranchGraph(createLight());
|
|
48 canvas.universe.addBranchGraph(setBackground());
|
|
49
|
|
50 canvas.addKeyListener(new KeyInputCodeSegment(this));
|
|
51 frame.setVisible(true);
|
|
52
|
|
53 }
|
|
54
|
|
55 private BranchGroup setBackground() {
|
|
56 BranchGroup scene = new BranchGroup();
|
|
57 BufferedImage img = null;
|
|
58 try {
|
373
|
59 URL url = getClass().getClassLoader().getResource("images/image1.jpg");
|
345
|
60 if (url!=null) {
|
|
61 img = ImageIO.read(url);
|
|
62 } else {
|
373
|
63 img = ImageIO.read(new File("images/image1.jpg"));
|
345
|
64 }
|
|
65 } catch (IOException e) {
|
|
66 e.printStackTrace();
|
|
67 }
|
|
68 ImageComponent2D image =
|
|
69 new ImageComponent2D(ImageComponent2D.FORMAT_RGBA8,img);
|
|
70 Background background = new Background(image);
|
|
71 background.setImageScaleMode(Background.SCALE_FIT_ALL);
|
|
72 BoundingSphere bounds = new BoundingSphere(new Point3d(), 10.0);
|
|
73 background.setApplicationBounds(bounds);
|
|
74 scene.addChild(background);
|
|
75 return scene;
|
|
76
|
|
77 }
|
|
78
|
|
79 private BranchGroup createLight() {
|
|
80 BranchGroup scene = new BranchGroup();
|
|
81 Color3f light_color = new Color3f(1.7f,1.7f,1.7f);
|
|
82 Vector3f light_direction = new Vector3f(0.0f,0.0f,-1.0f);
|
|
83 DirectionalLight light = new DirectionalLight(light_color,light_direction);
|
|
84 BoundingSphere bounds = new BoundingSphere(new Point3d(), 10.0);
|
|
85 light.setInfluencingBounds(bounds);
|
|
86 scene.addChild(light);
|
|
87 return scene;
|
|
88 }
|
|
89
|
|
90 public void register(MakeObject obj){
|
|
91 list.table.add(obj);
|
|
92 BranchGroup group = obj.createBranch();
|
|
93 this.canvas.universe.addBranchGraph(group);
|
|
94 }
|
|
95
|
|
96 public ViewChange getCanvas(){
|
|
97 return this.canvas;
|
|
98 }
|
|
99
|
|
100 public JFrame getJFrame(){
|
|
101 return this.frame;
|
|
102 }
|
|
103
|
|
104 public ObjectList getList(){
|
|
105 return this.list;
|
|
106 }
|
|
107
|
|
108 static public void main(String [] args) {
|
|
109 MakeFrame test = new MakeFrame("Test", 0);
|
|
110 test.setBackground();
|
|
111 test.createLight();
|
|
112 }
|
|
113
|
|
114 }
|