76.
77. public void setWindowMode()
78. {
79. frame = new JFrame();
80. frame.setResizable(false);//禁止窗体改变大小
81. frame.setPreferredSize(new Dimension(scrWidth,scrHeight));
82. frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);//响应窗体的关闭事件,但不关闭窗体
83. frame.setVisible(true);
84.// 侦听窗体事件并捕获窗体关闭中的事件,在用户确认后退出程序
85. frame.addWindowListener(new WindowAdapter(){
86. public void windowClosing(WindowEvent e)
87. {
88. int res = JOptionPane.showConfirmDialog(null, "是否退出!","退出",JOptionPane.YES_NO_OPTION);
89. if(res == JOptionPane.YES_OPTION)
90. closeFrame();
91. }
92. });
93. this.setFrametoCenter();
94. }
95.
96. public void setFullWindowMode()
97. {
98. if(frame != null)
99. {
100. device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
101. DisplayMode displayMode = device.getDisplayMode();
102. frame.setPreferredSize(new Dimension(displayMode.getWidth(),displayMode.getHeight()));
103. }
104. }
105.
106. public int getWidth()
107. {
108. return scrWidth;
109. }
110.
111. public int getHeight()
112. {
113. return scrHeight;
114. }
115.
116. public JFrame getFrame()
117. {
118. return frame;
119. }
120.
121.// 将窗体在显示屏幕内居中显示
122. public void setFrametoCenter()
123. {
124. if(device!=null)
125. return;
126. Insets inset = frame.getInsets();
127. int scrx=0;
128. int scry=0;
129. Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize();
130. if(scrSize.width > scrWidth)
131. scrx = (scrSize.width-scrWidth)/2;
132. if(scrSize.height > scrHeight)
133. scry = (scrSize.height-scrHeight)/2;
134. frame.setBounds(scrx-inset.left, scry-inset.top, scrWidth+inset.right+inset.left, scrHeight+inset.bottom+inset.top);
135. }
136.
137.// 关闭窗体事件
138. public void closeFrame()
139. {
140. frame.dispose();
141. System.exit(0);
142. }
143.}
LoadModelDemo.java
view plaincopy to clipboardprint?
01.import java.awt.BorderLayout;
02.import java.awt.Dimension;
03.import java.awt.Font;
04.import java.awt.GraphicsConfiguration;
05.import java.io.FileNotFoundException;
06.import java.util.Enumeration;
07.
08.import javax.media.j3d.Background;
09.import javax.media.j3d.BoundingBox;
10.import javax.media.j3d.BoundingSphere;
11.import javax.media.j3d.BranchGroup;
12.import javax.media.j3d.Canvas3D;
13.import javax.media.j3d.Transform3D;
14.import javax.media.j3d.TransformGroup;
15.import javax.swing.JPanel;
16.import javax.vecmath.Color3f;
17.import javax.vecmath.Point3d;
18.import javax.vecmath.Vector3d;
19.import javax.vecmath.Vector3f;
20.
21.import com.sun.j3d.loaders.IncorrectFormatException;
22.import com.sun.j3d.loaders.ParsingErrorException;
23.import com.sun.j3d.loaders.Scene;
24.import com.sun.j3d.loaders.objectfile.ObjectFile;
25.import com.sun.j3d.utils.behaviors.vp.OrbitBehavior;
26.import com.sun.j3d.utils.geometry.Text2D;
27.import com.sun.j3d.utils.universe.SimpleUniverse;
28.import com.sun.j3d.utils.universe.ViewingPlatform;
29.
30.
31.public class LoadModelDemo extends JPanel{
32.
33. private BranchGroup sceneBG;
34. private SimpleUniverse universe;
35. private BoundingSphere bounds;
36. private double boundRadius = 100;
37.
38. public LoadModelDemo(int width,int height)
39. {
40. this.setLayout(new BorderLayout());
41. GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
42. Canvas3D canvas = new Canvas3D(config);
43. canvas.setSize(width, height);
44. this.add(canvas,BorderLayout.CENTER);
45.
46. universe = new SimpleUniverse(canvas);
47. createSceneGroup();
48. initUserPosition();
49. orbitControls(canvas);
50. universe.addBranchGraph(sceneBG);
51. }
52.
53. public void createSceneGroup()
54. {
55. sceneBG = new BranchGroup();
56. bounds = new BoundingSphere(new Point3d(0,0,0),boundRadius);
57. addBackground();
58. TransformGroup[] objModel = new TransformGroup[4];
59. sceneBG.addChild(objModel[0] = loadModel("leet/Liit.obj",ObjectFile.RESIZE,new Vector3d(-5.4,0,0)));
60. sceneBG.addChild(objModel[1] = loadModel("leet/Liit.obj",ObjectFile.REVERSE,new Vector3d(-1.8,0,0)));
61. sceneBG.addChild(objModel[2] = loadModel("leet/Liit.obj",ObjectFile.TRIANGULATE,new Vector3d(1.8,0,0)));
62. sceneBG.addChild(objModel[3] = loadModel("leet/Liit.obj",ObjectFile.STRIPIFY,new Vector3d(5.4,0,0)));
63. sceneBG.addChild(new CheckerFloor().getBG());
64. //对sceneBG有关的对象进行编译和缓存,如果在编译之后再次添加其它分支的话将抛异常
65. sceneBG.compile();
66. }
67.
68. public void addBackground()
69. {
70. Background back = new Background();
71. back.setApplicationBounds(bounds);
72. back.setColor(0.17f, 0.62f, 0.92f);
73. sceneBG.addChild(back);
74. }
75.
76. private void initUserPosition()
77. {
78. //返回当前虚拟世界的观察平台
79. ViewingPlatform vp = universe.getViewingPlatform();
80. //得到观察平台的坐标枝花点
81. TransformGroup steerTG = vp.getViewPlatformTransform();
82. Transform3D t3d = new Transform3D();
83. steerTG.getTransform(t3d);
84. //设置观察点坐标在(0,5,20),看向坐标(0,0,0)处,指定Y轴向正数沿伸方向为正方向
85. t3d.lookAt(new Point3d(0,5,20), new Point3d(0,0,0), new Vector3d(0,1,0));
86. t3d.invert();
87. steerTG.setTransform(t3d);
88. }
89.
90. private void orbitControls(Canvas3D canvas)
91. {
92. OrbitBehavior orbit = new OrbitBehavior(canvas, OrbitBehavior.REVERSE_ALL);
93. orbit.setSchedulingBounds(bounds);
94. ViewingPlatform vp = universe.getViewingPlatform( );
95. vp.setViewPlatformBehavior(orbit);
96. }
97.
98. private TransformGroup loadModel(String filename,int flag,Vector3d translation)
99. {
100. Scene loadScene = this.loadFromFile(filename,flag);
101. Transform3D t3d = new Transform3D();
102. t3d = this.rotateModel();
103. t3d.setScale(this.calcScaleFactor(loadScene.getSceneGroup()));
104. t3d.setTranslation(translation);
105. TransformGroup tg = new TransformGroup(t3d);
106. tg.addChild(loadScene.getSceneGroup());
107. return tg;
108. }
109.
110. private Scene loadFromFile(String filename,int flag)
111. {
112. Scene loadScene = null;
113. ObjectFile obj = new ObjectFile(flag);
114. try {
115. loadScene = obj.load(this.getClass().getClassLoader().getResource(filename));
116. } catch (FileNotFoundException e) {
117. // TODO Auto-generated catch block
118. System.out.println("文件未找到或文件路径不正确");
119. e.printStackTrace();
120. } catch (IncorrectFormatException e) {
121. // TODO Auto-generated catch block
122. System.out.println("文件格式不正确");
123. e.printStackTrace();
124. } catch (ParsingErrorException e) {
125. // TODO Auto-generated catch block