001: /*
002: * $RCSfile: ImageDisplayer.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.2 $
041: * $Date: 2007/02/09 17:21:50 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.examples.print_canvas3d;
046:
047: import javax.swing.*;
048: import java.awt.BorderLayout;
049: import java.awt.Container;
050: import java.awt.Color;
051: import java.awt.Dimension;
052: import java.awt.Graphics;
053: import java.awt.image.BufferedImage;
054: import java.awt.event.*;
055:
056: class ImageDisplayer extends JFrame implements ActionListener {
057: BufferedImage bImage;
058:
059: private class ImagePanel extends JPanel {
060: public void paint(Graphics g) {
061: g.setColor(Color.black);
062: g.fillRect(0, 0, getSize().width, getSize().height);
063: g.drawImage(bImage, 0, 0, this );
064: }
065:
066: private ImagePanel() {
067: setPreferredSize(new Dimension(bImage.getWidth(), bImage
068: .getHeight()));
069: }
070: }
071:
072: private JMenuItem printItem;
073: private JMenuItem closeItem;
074:
075: private void freeResources() {
076: this .removeAll();
077: this .setVisible(false);
078: bImage = null;
079: }
080:
081: public void actionPerformed(ActionEvent event) {
082: Object target = event.getSource();
083:
084: if (target == printItem) {
085: new ImagePrinter(bImage).print();
086: } else if (target == closeItem) {
087: freeResources();
088: }
089: }
090:
091: private JMenuBar createMenuBar() {
092: JMenuBar menuBar = new JMenuBar();
093: JMenu fileMenu = new JMenu("File");
094: printItem = new JMenuItem("Print...");
095: printItem.addActionListener(this );
096: closeItem = new JMenuItem("Close");
097: closeItem.addActionListener(this );
098: fileMenu.add(printItem);
099: fileMenu.add(new JSeparator());
100: fileMenu.add(closeItem);
101: menuBar.add(fileMenu);
102: return menuBar;
103: }
104:
105: ImageDisplayer(BufferedImage bImage) {
106: this .bImage = bImage;
107: this .setTitle("Off-screen Canvas3D Snapshot");
108:
109: // Create and initialize menu bar
110: this .setJMenuBar(createMenuBar());
111:
112: // Create scroll pane, and embedded image panel
113: ImagePanel imagePanel = new ImagePanel();
114: JScrollPane scrollPane = new JScrollPane(imagePanel);
115: scrollPane.getViewport().setPreferredSize(
116: new Dimension(700, 700));
117:
118: // Handle the close event
119: this .addWindowListener(new WindowAdapter() {
120: public void windowClosing(WindowEvent winEvent) {
121: freeResources();
122: }
123: });
124:
125: // Add scroll pane to the frame and make it visible
126: this .getContentPane().add(scrollPane);
127: this .pack();
128: this .setVisible(true);
129: }
130: }
|