001: /*
002: * $RCSfile: OffScreenTest.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:45 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.examples.offscreen_canvas3d;
046:
047: import com.sun.j3d.utils.universe.*;
048: import com.sun.j3d.utils.geometry.ColorCube;
049: import javax.media.j3d.*;
050: import javax.vecmath.*;
051: import java.awt.*;
052: import java.awt.image.BufferedImage;
053:
054: /**
055: * OffScreenTest programs with no UI.
056: */
057: public class OffScreenTest extends javax.swing.JFrame {
058:
059: private SimpleUniverse univ = null;
060: private BranchGroup scene = null;
061: private Raster drawRaster = null;
062:
063: private BranchGroup createSceneGraph() {
064: // Create the root of the branch graph
065: BranchGroup objRoot = new BranchGroup();
066:
067: // trans object has composited transformation matrix
068: Transform3D trans = new Transform3D();
069: Transform3D rot = new Transform3D();
070:
071: trans.rotX(Math.PI / 4.0d);
072: rot.rotY(Math.PI / 5.0d);
073: trans.mul(rot);
074: trans.setScale(0.7);
075: trans.setTranslation(new Vector3d(-0.4, 0.3, 0.0));
076:
077: TransformGroup objTrans = new TransformGroup(trans);
078: objRoot.addChild(objTrans);
079:
080: // Create a simple shape leaf node, add it to the scene graph.
081: // ColorCube is a Convenience Utility class
082: objTrans.addChild(new ColorCube(0.4));
083:
084: //Create a raster
085: BufferedImage bImage = new BufferedImage(200, 200,
086: BufferedImage.TYPE_INT_ARGB);
087: ImageComponent2D buffer = new ImageComponent2D(
088: ImageComponent.FORMAT_RGBA, bImage, true, true);
089: buffer.setCapability(ImageComponent2D.ALLOW_IMAGE_READ);
090:
091: drawRaster = new Raster(new Point3f(0.0f, 0.0f, 0.0f),
092: Raster.RASTER_COLOR, 0, 0, 200, 200, buffer, null);
093:
094: drawRaster.setCapability(Raster.ALLOW_IMAGE_WRITE);
095: Shape3D shape = new Shape3D(drawRaster);
096: objRoot.addChild(shape);
097:
098: // Let Java 3D perform optimizations on this scene graph.
099: objRoot.compile();
100:
101: return objRoot;
102: }
103:
104: private OnScreenCanvas3D createOnScreenCanvasAndUniverse() {
105: // Get the preferred graphics configuration for the default screen
106: GraphicsConfiguration config = SimpleUniverse
107: .getPreferredConfiguration();
108:
109: // Create a Canvas3D using the preferred configuration
110: OnScreenCanvas3D onScrCanvas = new OnScreenCanvas3D(config,
111: false);
112:
113: // Create simple universe with view branch
114: univ = new SimpleUniverse(onScrCanvas);
115:
116: // This will move the ViewPlatform back a bit so the
117: // objects in the scene can be viewed.
118: univ.getViewingPlatform().setNominalViewingTransform();
119:
120: // Ensure at least 5 msec per frame (i.e., < 200Hz)
121: univ.getViewer().getView().setMinimumFrameCycleTime(5);
122:
123: return onScrCanvas;
124: }
125:
126: private OffScreenCanvas3D createOffScreenCanvas() {
127: // request an offscreen Canvas3D with a single buffer configuration
128: GraphicsConfigTemplate3D template = new GraphicsConfigTemplate3D();
129: template.setDoubleBuffer(GraphicsConfigTemplate3D.UNNECESSARY);
130: GraphicsConfiguration gc = GraphicsEnvironment
131: .getLocalGraphicsEnvironment().getDefaultScreenDevice()
132: .getBestConfiguration(template);
133:
134: // Create a offscreen Canvas3D using the single buffer configuration.
135: OffScreenCanvas3D offScrCanvas = new OffScreenCanvas3D(gc,
136: true, drawRaster);
137:
138: return offScrCanvas;
139: }
140:
141: /**
142: * Creates new form OffScreenTest
143: */
144: public OffScreenTest() {
145: // Initialize the GUI components
146: initComponents();
147:
148: // Create the content branch and add it to the universe
149: scene = createSceneGraph();
150:
151: // Create an OnScreenCanvas3D and SimpleUniverse; add canvas to drawing panel
152: OnScreenCanvas3D onScreenCanvas = createOnScreenCanvasAndUniverse();
153: drawingPanel.add(onScreenCanvas, java.awt.BorderLayout.CENTER);
154:
155: // Creante an OffScreenCanvas3D
156: OffScreenCanvas3D offScreenCanvas = createOffScreenCanvas();
157:
158: // set the offscreen to match the onscreen
159: Screen3D sOn = onScreenCanvas.getScreen3D();
160: Screen3D sOff = offScreenCanvas.getScreen3D();
161: sOff.setSize(sOn.getSize());
162: sOff.setPhysicalScreenWidth(sOn.getPhysicalScreenWidth());
163: sOff.setPhysicalScreenHeight(sOn.getPhysicalScreenHeight());
164:
165: // attach the same view to the offscreen canvas
166: View view = univ.getViewer().getView();
167: view.addCanvas3D(offScreenCanvas);
168:
169: // tell onscreen about the offscreen so it knows to
170: // render to the offscreen at postswap
171: onScreenCanvas.setOffScreenCanvas(offScreenCanvas);
172:
173: univ.addBranchGraph(scene);
174:
175: view.stopView();
176: // Make sure that image are render completely
177: // before grab it in postSwap().
178: onScreenCanvas.setImageReady();
179: view.startView();
180: }
181:
182: // ----------------------------------------------------------------
183:
184: /** This method is called from within the constructor to
185: * initialize the form.
186: * WARNING: Do NOT modify this code. The content of this method is
187: * always regenerated by the Form Editor.
188: */
189: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
190: private void initComponents() {
191: drawingPanel = new javax.swing.JPanel();
192:
193: setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
194: setTitle("Window Title");
195: drawingPanel.setLayout(new java.awt.BorderLayout());
196:
197: drawingPanel.setPreferredSize(new java.awt.Dimension(500, 500));
198: getContentPane()
199: .add(drawingPanel, java.awt.BorderLayout.CENTER);
200:
201: pack();
202: }// </editor-fold>//GEN-END:initComponents
203:
204: /**
205: * @param args the command line arguments
206: */
207: public static void main(String args[]) {
208: java.awt.EventQueue.invokeLater(new Runnable() {
209: public void run() {
210: new OffScreenTest().setVisible(true);
211: }
212: });
213: }
214:
215: // Variables declaration - do not modify//GEN-BEGIN:variables
216: private javax.swing.JPanel drawingPanel;
217: // End of variables declaration//GEN-END:variables
218:
219: }
|