001: /*
002: * $RCSfile: PrintFromButton.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.GraphicsConfiguration;
052: import javax.swing.JPopupMenu;
053: import java.awt.*;
054: import java.awt.image.BufferedImage;
055:
056: /**
057: * PrintFromButton programs with simple UI.
058: */
059: public class PrintFromButton extends javax.swing.JFrame {
060:
061: private SimpleUniverse univ = null;
062: private BranchGroup scene = null;
063: private Raster drawRaster = null;
064: private OffScreenCanvas3D offScreenCanvas = null;
065:
066: public BranchGroup createSceneGraph() {
067: // Create the root of the branch graph
068: BranchGroup objRoot = new BranchGroup();
069:
070: // trans object has composited transformation matrix
071: Transform3D trans = new Transform3D();
072: Transform3D rot = new Transform3D();
073:
074: trans.rotX(Math.PI / 4.0d);
075: rot.rotY(Math.PI / 5.0d);
076: trans.mul(rot);
077: trans.setScale(0.7);
078: trans.setTranslation(new Vector3d(-0.4, 0.3, 0.0));
079:
080: TransformGroup objTrans = new TransformGroup(trans);
081: objRoot.addChild(objTrans);
082:
083: // Create a simple shape leaf node, add it to the scene graph.
084: // ColorCube is a Convenience Utility class
085: objTrans.addChild(new ColorCube(0.4));
086:
087: //Create a raster
088: BufferedImage bImage = new BufferedImage(200, 200,
089: BufferedImage.TYPE_INT_ARGB);
090: ImageComponent2D buffer = new ImageComponent2D(
091: ImageComponent.FORMAT_RGBA, bImage, true, true);
092: buffer.setCapability(ImageComponent2D.ALLOW_IMAGE_READ);
093:
094: drawRaster = new Raster(new Point3f(0.0f, 0.0f, 0.0f),
095: Raster.RASTER_COLOR, 0, 0, 200, 200, buffer, null);
096:
097: drawRaster.setCapability(Raster.ALLOW_IMAGE_WRITE);
098: Shape3D shape = new Shape3D(drawRaster);
099: objRoot.addChild(shape);
100:
101: // Let Java 3D perform optimizations on this scene graph.
102: objRoot.compile();
103:
104: return objRoot;
105: }
106:
107: private OnScreenCanvas3D createOnScreenCanvasAndUniverse() {
108: // Get the preferred graphics configuration for the default screen
109: GraphicsConfiguration config = SimpleUniverse
110: .getPreferredConfiguration();
111:
112: // Create a Canvas3D using the preferred configuration
113: OnScreenCanvas3D onScrCanvas = new OnScreenCanvas3D(config,
114: false);
115:
116: // Create simple universe with view branch
117: univ = new SimpleUniverse(onScrCanvas);
118:
119: // This will move the ViewPlatform back a bit so the
120: // objects in the scene can be viewed.
121: univ.getViewingPlatform().setNominalViewingTransform();
122:
123: // Ensure at least 5 msec per frame (i.e., < 200Hz)
124: univ.getViewer().getView().setMinimumFrameCycleTime(5);
125:
126: return onScrCanvas;
127: }
128:
129: private OffScreenCanvas3D createOffScreenCanvas() {
130: // request an offscreen Canvas3D with a single buffer configuration
131: GraphicsConfigTemplate3D template = new GraphicsConfigTemplate3D();
132: template.setDoubleBuffer(GraphicsConfigTemplate3D.UNNECESSARY);
133: GraphicsConfiguration gc = GraphicsEnvironment
134: .getLocalGraphicsEnvironment().getDefaultScreenDevice()
135: .getBestConfiguration(template);
136:
137: // Create a offscreen Canvas3D using the single buffer configuration.
138: OffScreenCanvas3D offScrCanvas = new OffScreenCanvas3D(gc,
139: true, drawRaster);
140:
141: return offScrCanvas;
142: }
143:
144: /**
145: * Creates new form PrintFromButton
146: */
147: public PrintFromButton() {
148: // Initialize the GUI components
149: JPopupMenu.setDefaultLightWeightPopupEnabled(false);
150: initComponents();
151:
152: // Create the content branch and add it to the universe
153: scene = createSceneGraph();
154:
155: // Create an OnScreenCanvas3D and SimpleUniverse; add canvas to drawing panel
156: OnScreenCanvas3D onScreenCanvas = createOnScreenCanvasAndUniverse();
157: drawingPanel.add(onScreenCanvas, java.awt.BorderLayout.CENTER);
158:
159: // Creante an OffScreenCanvas3D
160: offScreenCanvas = createOffScreenCanvas();
161:
162: // set the offscreen to match the onscreen
163: Screen3D sOn = onScreenCanvas.getScreen3D();
164: Screen3D sOff = offScreenCanvas.getScreen3D();
165: sOff.setSize(sOn.getSize());
166: sOff.setPhysicalScreenWidth(sOn.getPhysicalScreenWidth());
167: sOff.setPhysicalScreenHeight(sOn.getPhysicalScreenHeight());
168:
169: // attach the same view to the offscreen canvas
170: View view = univ.getViewer().getView();
171: view.addCanvas3D(offScreenCanvas);
172:
173: univ.addBranchGraph(scene);
174: }
175:
176: // ----------------------------------------------------------------
177:
178: /** This method is called from within the constructor to
179: * initialize the form.
180: * WARNING: Do NOT modify this code. The content of this method is
181: * always regenerated by the Form Editor.
182: */
183: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
184: private void initComponents() {
185: java.awt.GridBagConstraints gridBagConstraints;
186:
187: guiPanel = new javax.swing.JPanel();
188: myButton = new javax.swing.JButton();
189: drawingPanel = new javax.swing.JPanel();
190: jMenuBar1 = new javax.swing.JMenuBar();
191: fileMenu = new javax.swing.JMenu();
192: exitMenuItem = new javax.swing.JMenuItem();
193:
194: setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
195: setTitle("Window Title");
196: guiPanel.setLayout(new java.awt.GridBagLayout());
197:
198: myButton.setText("Print");
199: myButton.addActionListener(new java.awt.event.ActionListener() {
200: public void actionPerformed(java.awt.event.ActionEvent evt) {
201: myButtonActionPerformed(evt);
202: }
203: });
204:
205: gridBagConstraints = new java.awt.GridBagConstraints();
206: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
207: gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
208: guiPanel.add(myButton, gridBagConstraints);
209:
210: getContentPane().add(guiPanel, java.awt.BorderLayout.NORTH);
211:
212: drawingPanel.setLayout(new java.awt.BorderLayout());
213:
214: drawingPanel.setPreferredSize(new java.awt.Dimension(500, 500));
215: getContentPane()
216: .add(drawingPanel, java.awt.BorderLayout.CENTER);
217:
218: fileMenu.setText("File");
219: exitMenuItem.setText("Exit");
220: exitMenuItem
221: .addActionListener(new java.awt.event.ActionListener() {
222: public void actionPerformed(
223: java.awt.event.ActionEvent evt) {
224: exitMenuItemActionPerformed(evt);
225: }
226: });
227:
228: fileMenu.add(exitMenuItem);
229:
230: jMenuBar1.add(fileMenu);
231:
232: setJMenuBar(jMenuBar1);
233:
234: pack();
235: }// </editor-fold>//GEN-END:initComponents
236:
237: private void myButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_myButtonActionPerformed
238: offScreenCanvas.print(false);
239: }//GEN-LAST:event_myButtonActionPerformed
240:
241: private void exitMenuItemActionPerformed(
242: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuItemActionPerformed
243: System.exit(0);
244: }//GEN-LAST:event_exitMenuItemActionPerformed
245:
246: /**
247: * @param args the command line arguments
248: */
249: public static void main(String args[]) {
250: java.awt.EventQueue.invokeLater(new Runnable() {
251: public void run() {
252: new PrintFromButton().setVisible(true);
253: }
254: });
255: }
256:
257: // Variables declaration - do not modify//GEN-BEGIN:variables
258: private javax.swing.JPanel drawingPanel;
259: private javax.swing.JMenuItem exitMenuItem;
260: private javax.swing.JMenu fileMenu;
261: private javax.swing.JPanel guiPanel;
262: private javax.swing.JMenuBar jMenuBar1;
263: private javax.swing.JButton myButton;
264: // End of variables declaration//GEN-END:variables
265:
266: }
|