001: /*
002: *
003: * Copyright (c) 2000 Silvere Martin-Michiellot All Rights Reserved.
004: *
005: * Silvere Martin-Michiellot grants you ("Licensee") a non-exclusive,
006: * royalty free, license to use, modify and redistribute this
007: * software in source and binary code form,
008: * provided that i) this copyright notice and license appear on all copies of
009: * the software; and ii) Licensee does not utilize the software in a manner
010: * which is disparaging to Silvere Martin-Michiellot.
011: *
012: * This software is provided "AS IS," without a warranty of any kind. ALL
013: * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
014: * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
015: * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. Silvere Martin-Michiellot
016: * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
017: * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
018: * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
019: * Silvere Martin-Michiellot OR ITS LICENSORS BE LIABLE
020: * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
021: * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
022: * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
023: * OR INABILITY TO USE SOFTWARE, EVEN IF Silvere Martin-Michiellot HAS BEEN
024: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
025: *
026: * This software is not designed or intended for use in on-line control of
027: * aircraft, air traffic, aircraft navigation or aircraft communications; or in
028: * the design, construction, operation or maintenance of any nuclear
029: * facility. Licensee represents and warrants that it will not use or
030: * redistribute the Software for such purposes.
031: *
032: *
033: */
034:
035: package com.db.media.print;
036:
037: // This code is repackaged after the code from the Java3D SDK
038: // Site http://java.sun.com/
039: // Email java3d-interest@java.sun.com (mailing list)
040:
041: import javax.swing.*;
042: import java.awt.BorderLayout;
043: import java.awt.Container;
044: import java.awt.Dimension;
045: import java.awt.GraphicsConfiguration;
046: import java.awt.Point;
047: import java.awt.image.BufferedImage;
048: import java.awt.event.*;
049: import com.sun.j3d.utils.universe.*;
050: import javax.media.j3d.*;
051: import javax.vecmath.*;
052: import java.io.*;
053:
054: public class PrintCanvas3D extends JFrame implements ActionListener {
055:
056: private JMenuItem printItem;
057: private JMenuItem saveItem;
058: private JMenuItem quitItem;
059:
060: private Canvas3D canvas3D;
061: private OffScreenCanvas3D offScreenCanvas3D;
062:
063: private SimpleUniverse simpleUniverse;
064:
065: private static final int OFF_SCREEN_SCALE = 3;
066:
067: private class AppPanel extends JPanel {
068:
069: public BranchGroup createSceneGraph(String args[]) {
070: // Create the root of the branch graph
071:
072: BranchGroup objRoot;
073:
074: objRoot = new BranchGroup();
075: //this is the place you have to put your code to
076: //DISPLAY SOMETHING in the Canvas3D
077:
078: return objRoot;
079:
080: }
081:
082: // Create the Canvas3D (both on-screen and off-screen)
083: private void createCanvas3D(String[] args) {
084:
085: // Create Canvas3D
086: GraphicsConfiguration config = SimpleUniverse
087: .getPreferredConfiguration();
088:
089: canvas3D = new Canvas3D(config);
090: canvas3D.setSize(600, 450);
091:
092: // Create a simple scene and attach it to the virtual universe
093: BranchGroup scene = createSceneGraph(args);
094: simpleUniverse = new SimpleUniverse(canvas3D);
095: // This will move the ViewPlatform back a bit so the
096: // objects in the scene can be viewed.
097: simpleUniverse.getViewingPlatform()
098: .setNominalViewingTransform();
099: simpleUniverse.addBranchGraph(scene);
100:
101: // Create the off-screen Canvas3D object
102: offScreenCanvas3D = new OffScreenCanvas3D(config, true);
103: // Set the off-screen size based on a scale factor times the
104: // on-screen size
105: Screen3D sOn = canvas3D.getScreen3D();
106: Screen3D sOff = offScreenCanvas3D.getScreen3D();
107: Dimension dim = sOn.getSize();
108: dim.width *= OFF_SCREEN_SCALE;
109: dim.height *= OFF_SCREEN_SCALE;
110: sOff.setSize(dim);
111: sOff.setPhysicalScreenWidth(sOn.getPhysicalScreenWidth()
112: * OFF_SCREEN_SCALE);
113: sOff.setPhysicalScreenHeight(sOn.getPhysicalScreenHeight()
114: * OFF_SCREEN_SCALE);
115:
116: // attach the offscreen canvas to the view
117: simpleUniverse.getViewer().getView().addCanvas3D(
118: offScreenCanvas3D);
119:
120: }
121:
122: private AppPanel(String args[]) {
123: setLayout(new BorderLayout());
124:
125: // Create Canvas3D and scene graph
126: createCanvas3D(args);
127: add("Center", canvas3D);
128: }
129:
130: }
131:
132: public void actionPerformed(ActionEvent event) {
133:
134: Object target = event.getSource();
135:
136: if ((target == printItem) || (target == saveItem)) {
137: Point loc = canvas3D.getLocationOnScreen();
138: offScreenCanvas3D.setOffScreenLocation(loc);
139: Dimension dim = canvas3D.getSize();
140: dim.width *= OFF_SCREEN_SCALE;
141: dim.height *= OFF_SCREEN_SCALE;
142: BufferedImage bImage = offScreenCanvas3D.doRender(
143: dim.width, dim.height);
144:
145: if (target == saveItem) {
146: new ImageSaver(bImage).save("capture.jpg", 90.0f);
147: } else {
148: new ImagePrinter(bImage).print();
149: }
150: }
151:
152: else if (target == quitItem) {
153: simpleUniverse.removeAllLocales();
154: System.exit(0);
155: }
156:
157: }
158:
159: private JMenuBar createMenuBar() {
160:
161: JMenuBar menuBar = new JMenuBar();
162: JMenu fileMenu = new JMenu("File");
163: printItem = new JMenuItem("Print...");
164: printItem.addActionListener(this );
165: saveItem = new JMenuItem("Save");
166: //should propose a file dialog
167: saveItem.addActionListener(this );
168: quitItem = new JMenuItem("Quit");
169: quitItem.addActionListener(this );
170: fileMenu.add(printItem);
171: fileMenu.add(saveItem);
172: fileMenu.add(new JSeparator());
173: fileMenu.add(quitItem);
174: menuBar.add(fileMenu);
175:
176: return menuBar;
177:
178: }
179:
180: private PrintCanvas3D(String args[]) {
181:
182: this .setTitle("Canvas3D Print Test");
183:
184: // Create and initialize menu bar
185: JPopupMenu.setDefaultLightWeightPopupEnabled(false);
186: this .setJMenuBar(createMenuBar());
187:
188: // Handle the close event
189: this .addWindowListener(new WindowAdapter() {
190: public void windowClosing(WindowEvent winEvent) {
191: System.exit(0);
192: }
193: });
194:
195: // Add main panel to top-level frame and make it visible
196: this .getContentPane().add(new AppPanel(args));
197: this .pack();
198: this .setVisible(true);
199:
200: }
201:
202: public static void main(String[] args) {
203:
204: new PrintCanvas3D(args);
205:
206: }
207:
208: }
|