001: /*
002: * $RCSfile: PrintCanvas3D.java,v $
003: *
004: * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * - Redistribution of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * - Redistribution in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * Neither the name of Sun Microsystems, Inc. or the names of
019: * contributors may be used to endorse or promote products derived
020: * from this software without specific prior written permission.
021: *
022: * This software is provided "AS IS," without a warranty of any
023: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
024: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
025: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
026: * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
027: * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
028: * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
029: * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
030: * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
031: * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
032: * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
033: * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
034: * POSSIBILITY OF SUCH DAMAGES.
035: *
036: * You acknowledge that this software is not designed, licensed or
037: * intended for use in the design, construction, operation or
038: * maintenance of any nuclear facility.
039: *
040: * $Revision: 1.3 $
041: * $Date: 2007/02/09 17:21:50 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.examples.print_canvas3d;
046:
047: import com.sun.j3d.utils.universe.*;
048: import java.net.MalformedURLException;
049: import javax.media.j3d.*;
050: import javax.vecmath.*;
051: import java.awt.*;
052: import java.awt.GraphicsConfiguration;
053: import javax.swing.JPopupMenu;
054:
055: import com.sun.j3d.loaders.objectfile.ObjectFile;
056: import com.sun.j3d.loaders.ParsingErrorException;
057: import com.sun.j3d.loaders.IncorrectFormatException;
058: import com.sun.j3d.loaders.Scene;
059: import java.awt.image.BufferedImage;
060: import java.io.*;
061: import com.sun.j3d.utils.behaviors.mouse.*;
062: import java.net.URL;
063: import org.jdesktop.j3d.examples.Resources;
064:
065: public class PrintCanvas3D extends javax.swing.JFrame {
066:
067: private static final boolean noTriangulate = false;
068: private static final boolean noStripify = false;
069: private static final double creaseAngle = 60.0;
070: private Canvas3D onScreenCanvas3D;
071: private OffScreenCanvas3D offScreenCanvas3D;
072: private URL filename = null;
073: private static final int OFF_SCREEN_SCALE = 3;
074:
075: private SimpleUniverse univ = null;
076:
077: public BranchGroup createSceneGraph(String args[]) {
078: // Create the root of the branch graph
079: BranchGroup objRoot = new BranchGroup();
080:
081: // Create a Transformgroup to scale all objects so they
082: // appear in the scene.
083: TransformGroup objScale = new TransformGroup();
084: Transform3D t3d = new Transform3D();
085: t3d.setScale(0.7);
086: objScale.setTransform(t3d);
087: objRoot.addChild(objScale);
088:
089: // Create the transform group node and initialize it to the
090: // identity. Enable the TRANSFORM_WRITE capability so that
091: // our behavior code can modify it at runtime. Add it to the
092: // root of the subgraph.
093: TransformGroup objTrans = new TransformGroup();
094: objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
095: objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
096: objScale.addChild(objTrans);
097:
098: int flags = ObjectFile.RESIZE;
099: if (!noTriangulate)
100: flags |= ObjectFile.TRIANGULATE;
101: if (!noStripify)
102: flags |= ObjectFile.STRIPIFY;
103: ObjectFile f = new ObjectFile(flags, (float) (creaseAngle
104: * Math.PI / 180.0));
105: Scene scene = null;
106: try {
107: scene = f.load(filename);
108: } catch (FileNotFoundException e) {
109: System.err.println(e);
110: System.exit(1);
111: } catch (ParsingErrorException e) {
112: System.err.println(e);
113: System.exit(1);
114: } catch (IncorrectFormatException e) {
115: System.err.println(e);
116: System.exit(1);
117: }
118:
119: objTrans.addChild(scene.getSceneGroup());
120:
121: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
122: 0.0, 0.0), 100.0);
123:
124: // Create the rotate behavior node
125: MouseRotate behavior = new MouseRotate();
126: behavior.setTransformGroup(objTrans);
127: objTrans.addChild(behavior);
128: behavior.setSchedulingBounds(bounds);
129:
130: // Create the zoom behavior node
131: MouseZoom behavior2 = new MouseZoom();
132: behavior2.setTransformGroup(objTrans);
133: objTrans.addChild(behavior2);
134: behavior2.setSchedulingBounds(bounds);
135:
136: // Create the translate behavior node
137: MouseTranslate behavior3 = new MouseTranslate();
138: behavior3.setTransformGroup(objTrans);
139: objTrans.addChild(behavior3);
140: behavior3.setSchedulingBounds(bounds);
141:
142: // Set up the background
143: Color3f bgColor = new Color3f(0.05f, 0.05f, 0.5f);
144: Background bgNode = new Background(bgColor);
145: bgNode.setApplicationBounds(bounds);
146: objRoot.addChild(bgNode);
147:
148: // Set up the ambient light
149: Color3f ambientColor = new Color3f(0.1f, 0.1f, 0.1f);
150: AmbientLight ambientLightNode = new AmbientLight(ambientColor);
151: ambientLightNode.setInfluencingBounds(bounds);
152: objRoot.addChild(ambientLightNode);
153:
154: // Set up the directional lights
155: Color3f light1Color = new Color3f(1.0f, 1.0f, 0.9f);
156: Vector3f light1Direction = new Vector3f(4.0f, -7.0f, -12.0f);
157: Color3f light2Color = new Color3f(0.3f, 0.3f, 0.4f);
158: Vector3f light2Direction = new Vector3f(-6.0f, -2.0f, -1.0f);
159:
160: DirectionalLight light1 = new DirectionalLight(light1Color,
161: light1Direction);
162: light1.setInfluencingBounds(bounds);
163: objRoot.addChild(light1);
164:
165: DirectionalLight light2 = new DirectionalLight(light2Color,
166: light2Direction);
167: light2.setInfluencingBounds(bounds);
168: objRoot.addChild(light2);
169:
170: return objRoot;
171: }
172:
173: private void usage() {
174: System.out.println("Usage: java PrintCanvas3D <.obj file>");
175: System.exit(0);
176: } // End of usage
177:
178: private OffScreenCanvas3D createOffScreenCanvas(
179: Canvas3D onScreenCanvas3D) {
180: // Create the off-screen Canvas3D object
181: // request an offscreen Canvas3D with a single buffer configuration
182: GraphicsConfigTemplate3D template = new GraphicsConfigTemplate3D();
183: template.setDoubleBuffer(GraphicsConfigTemplate3D.UNNECESSARY);
184: GraphicsConfiguration gc = GraphicsEnvironment
185: .getLocalGraphicsEnvironment().getDefaultScreenDevice()
186: .getBestConfiguration(template);
187:
188: offScreenCanvas3D = new OffScreenCanvas3D(gc, true);
189: // Set the off-screen size based on a scale factor times the
190: // on-screen size
191: Screen3D sOn = onScreenCanvas3D.getScreen3D();
192: Screen3D sOff = offScreenCanvas3D.getScreen3D();
193: Dimension dim = sOn.getSize();
194: dim.width *= OFF_SCREEN_SCALE;
195: dim.height *= OFF_SCREEN_SCALE;
196: sOff.setSize(dim);
197: sOff.setPhysicalScreenWidth(sOn.getPhysicalScreenWidth()
198: * OFF_SCREEN_SCALE);
199: sOff.setPhysicalScreenHeight(sOn.getPhysicalScreenHeight()
200: * OFF_SCREEN_SCALE);
201:
202: // attach the offscreen canvas to the view
203: univ.getViewer().getView().addCanvas3D(offScreenCanvas3D);
204:
205: return offScreenCanvas3D;
206:
207: }
208:
209: private Canvas3D createUniverse() {
210: GraphicsConfiguration config = SimpleUniverse
211: .getPreferredConfiguration();
212:
213: Canvas3D c = new Canvas3D(config);
214:
215: univ = new SimpleUniverse(c);
216:
217: // This will move the ViewPlatform back a bit so the
218: // objects in the scene can be viewed.
219: univ.getViewingPlatform().setNominalViewingTransform();
220:
221: // Ensure at least 5 msec per frame (i.e., < 200Hz)
222: univ.getViewer().getView().setMinimumFrameCycleTime(5);
223:
224: return c;
225: }
226:
227: /**
228: * Creates new form PrintCanvas3D
229: */
230: public PrintCanvas3D(String args[]) {
231:
232: if (args.length == 0) {
233: filename = Resources
234: .getResource("resources/geometry/beethoven.obj");
235: if (filename == null) {
236: System.err
237: .println("resources/geometry/beethoven.obj not found");
238: System.exit(1);
239: }
240: } else {
241: for (int i = 0; i < args.length; i++) {
242: if (args[i].startsWith("-")) {
243: System.err.println("Argument '" + args[i]
244: + "' ignored.");
245: } else {
246: try {
247: filename = new URL(args[i]);
248: } catch (MalformedURLException e) {
249: System.err.println(e.getMessage());
250: System.exit(1);
251: }
252: }
253: }
254: }
255:
256: if (filename == null) {
257: usage();
258: }
259:
260: // Initialize the GUI components
261: JPopupMenu.setDefaultLightWeightPopupEnabled(false);
262: initComponents();
263:
264: // Create Canvas3D and SimpleUniverse; add canvas to drawing panel
265: onScreenCanvas3D = createUniverse();
266: drawingPanel
267: .add(onScreenCanvas3D, java.awt.BorderLayout.CENTER);
268:
269: // Create the content branch and add it to the universe
270: BranchGroup scene = createSceneGraph(args);
271:
272: // Create the off-screen Canvas3D object
273: createOffScreenCanvas(onScreenCanvas3D);
274:
275: univ.addBranchGraph(scene);
276: }
277:
278: // ----------------------------------------------------------------
279:
280: /** This method is called from within the constructor to
281: * initialize the form.
282: * WARNING: Do NOT modify this code. The content of this method is
283: * always regenerated by the Form Editor.
284: */
285: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
286: private void initComponents() {
287: drawingPanel = new javax.swing.JPanel();
288: jMenuBar1 = new javax.swing.JMenuBar();
289: fileMenu = new javax.swing.JMenu();
290: snapShotMenuItem = new javax.swing.JMenuItem();
291: printMenuItem = new javax.swing.JMenuItem();
292: exitMenuItem = new javax.swing.JMenuItem();
293:
294: setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
295: setTitle("Window Title");
296: drawingPanel.setLayout(new java.awt.BorderLayout());
297:
298: drawingPanel.setPreferredSize(new java.awt.Dimension(500, 500));
299: getContentPane()
300: .add(drawingPanel, java.awt.BorderLayout.CENTER);
301:
302: fileMenu.setText("File");
303: snapShotMenuItem.setText("Snapshot");
304: snapShotMenuItem
305: .addActionListener(new java.awt.event.ActionListener() {
306: public void actionPerformed(
307: java.awt.event.ActionEvent evt) {
308: snapShotMenuItemActionPerformed(evt);
309: }
310: });
311:
312: fileMenu.add(snapShotMenuItem);
313:
314: printMenuItem.setText("Print");
315: printMenuItem
316: .addActionListener(new java.awt.event.ActionListener() {
317: public void actionPerformed(
318: java.awt.event.ActionEvent evt) {
319: printMenuItemActionPerformed(evt);
320: }
321: });
322:
323: fileMenu.add(printMenuItem);
324:
325: exitMenuItem.setText("Exit");
326: exitMenuItem
327: .addActionListener(new java.awt.event.ActionListener() {
328: public void actionPerformed(
329: java.awt.event.ActionEvent evt) {
330: exitMenuItemActionPerformed(evt);
331: }
332: });
333:
334: fileMenu.add(exitMenuItem);
335:
336: jMenuBar1.add(fileMenu);
337:
338: setJMenuBar(jMenuBar1);
339:
340: pack();
341: }// </editor-fold>//GEN-END:initComponents
342:
343: private void printMenuItemActionPerformed(
344: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_printMenuItemActionPerformed
345: Point loc = onScreenCanvas3D.getLocationOnScreen();
346: offScreenCanvas3D.setOffScreenLocation(loc);
347: Dimension dim = onScreenCanvas3D.getSize();
348: dim.width *= OFF_SCREEN_SCALE;
349: dim.height *= OFF_SCREEN_SCALE;
350: BufferedImage bImage = offScreenCanvas3D.doRender(dim.width,
351: dim.height);
352:
353: new ImagePrinter(bImage).print();
354:
355: }//GEN-LAST:event_printMenuItemActionPerformed
356:
357: private void snapShotMenuItemActionPerformed(
358: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_snapShotMenuItemActionPerformed
359: Point loc = onScreenCanvas3D.getLocationOnScreen();
360: offScreenCanvas3D.setOffScreenLocation(loc);
361: Dimension dim = onScreenCanvas3D.getSize();
362: dim.width *= OFF_SCREEN_SCALE;
363: dim.height *= OFF_SCREEN_SCALE;
364: BufferedImage bImage = offScreenCanvas3D.doRender(dim.width,
365: dim.height);
366:
367: new ImageDisplayer(bImage);
368:
369: }//GEN-LAST:event_snapShotMenuItemActionPerformed
370:
371: private void exitMenuItemActionPerformed(
372: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuItemActionPerformed
373: System.exit(0);
374: }//GEN-LAST:event_exitMenuItemActionPerformed
375:
376: /**
377: * @param args the command line arguments
378: */
379: public static void main(final String args[]) {
380: java.awt.EventQueue.invokeLater(new Runnable() {
381: public void run() {
382: new PrintCanvas3D(args).setVisible(true);
383: ;
384: }
385: });
386: }
387:
388: // Variables declaration - do not modify//GEN-BEGIN:variables
389: private javax.swing.JPanel drawingPanel;
390: private javax.swing.JMenuItem exitMenuItem;
391: private javax.swing.JMenu fileMenu;
392: private javax.swing.JMenuBar jMenuBar1;
393: private javax.swing.JMenuItem printMenuItem;
394: private javax.swing.JMenuItem snapShotMenuItem;
395: // End of variables declaration//GEN-END:variables
396:
397: }
|