KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > imageviewer > ImageViewer


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package examples.imageviewer;
21
22 /** This class is an entry point of the simple image viewer.
23  * It creates and shows the main application frame.
24  */

25 public class ImageViewer extends javax.swing.JFrame JavaDoc {
26
27     /** Image Viewer constructor.
28      * It initializes all GUI components [menu bar, menu items, desktop pane, etc.].
29      */

30     public ImageViewer() {
31         initComponents();
32         pack();
33         setBounds( 100, 100, 400, 400 );
34     }
35
36     /** This method is called from within the constructor to
37      * initialize the form.
38      * WARNING: Do NOT modify this code. The content of this method is
39      * always regenerated by the FormEditor.
40      */

41     private void initComponents() {//GEN-BEGIN:initComponents
42
desktop = new javax.swing.JDesktopPane JavaDoc();
43         mainMenuBar = new javax.swing.JMenuBar JavaDoc();
44         fileMenu = new javax.swing.JMenu JavaDoc();
45         openMenuItem = new javax.swing.JMenuItem JavaDoc();
46         jSeparator1 = new javax.swing.JSeparator JavaDoc();
47         exitMenuItem = new javax.swing.JMenuItem JavaDoc();
48
49         setTitle("Image Viewer");
50         addWindowListener(new java.awt.event.WindowAdapter JavaDoc() {
51             public void windowClosing(java.awt.event.WindowEvent JavaDoc evt) {
52                 exitForm(evt);
53             }
54         });
55
56         getAccessibleContext().setAccessibleName("Image Viewer Frame");
57         getContentPane().add(desktop, java.awt.BorderLayout.CENTER);
58         desktop.getAccessibleContext().setAccessibleName("Image Desktop");
59         desktop.getAccessibleContext().setAccessibleDescription("Image desktop");
60
61         fileMenu.setMnemonic('f');
62         fileMenu.setText("File");
63         openMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
64         openMenuItem.setMnemonic('o');
65         openMenuItem.setText("Open");
66         openMenuItem.addActionListener(new java.awt.event.ActionListener JavaDoc() {
67             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
68                 openMenuItemActionPerformed(evt);
69             }
70         });
71
72         fileMenu.add(openMenuItem);
73         openMenuItem.getAccessibleContext().setAccessibleName("Open Menu Item");
74         openMenuItem.getAccessibleContext().setAccessibleDescription("Open menu item.");
75
76         fileMenu.add(jSeparator1);
77
78         exitMenuItem.setMnemonic('x');
79         exitMenuItem.setText("Exit");
80         exitMenuItem.addActionListener(new java.awt.event.ActionListener JavaDoc() {
81             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
82                 exitMenuItemActionPerformed(evt);
83             }
84         });
85
86         fileMenu.add(exitMenuItem);
87         exitMenuItem.getAccessibleContext().setAccessibleName("Exit Menu Item");
88         exitMenuItem.getAccessibleContext().setAccessibleDescription("Exit menu item.");
89
90         mainMenuBar.add(fileMenu);
91         fileMenu.getAccessibleContext().setAccessibleName("File Menu");
92         fileMenu.getAccessibleContext().setAccessibleDescription("File menu.");
93
94         setJMenuBar(mainMenuBar);
95         mainMenuBar.getAccessibleContext().setAccessibleName("Main Menu Bar");
96         mainMenuBar.getAccessibleContext().setAccessibleDescription("Main menu bar.");
97
98     }//GEN-END:initComponents
99

100     /** This method is called when File -> Exit menu item is invoked.
101      * It closes the application.
102      * @param evt ActionEvent instance passed from actionPerformed event.
103      */

104     private void exitMenuItemActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_exitMenuItemActionPerformed
105
System.exit( 0 );
106     }//GEN-LAST:event_exitMenuItemActionPerformed
107

108     /** This method is called when File -> Open menu item is invoked.
109      * It displays a dialog to choose the image file to be opened and displayed.
110      * @param evt ActionEvent instance passed from actionPerformed event.
111      */

112     private void openMenuItemActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_openMenuItemActionPerformed
113
javax.swing.JFileChooser JavaDoc chooser = new javax.swing.JFileChooser JavaDoc();
114         chooser.addChoosableFileFilter(new ImageFileFilter());
115         int option = chooser.showOpenDialog(this);
116         if (option == javax.swing.JFileChooser.APPROVE_OPTION) {
117             java.io.File JavaDoc file = chooser.getSelectedFile();
118             if (file == null) return;
119             ImageFrame ifr = new ImageFrame(file.getAbsolutePath());
120             desktop.add(ifr, javax.swing.JLayeredPane.DEFAULT_LAYER);
121             
122             ifr.setVisible( true );
123             ifr.setSize(200, 200);
124             ifr.setLocation(0, 0);
125         }
126     }//GEN-LAST:event_openMenuItemActionPerformed
127

128     /** This method is called when the application frame is closed.
129      * @param evt WindowEvent instance passed from windowClosing event.
130      */

131     private void exitForm(java.awt.event.WindowEvent JavaDoc evt) {//GEN-FIRST:event_exitForm
132
System.exit(0);
133     }//GEN-LAST:event_exitForm
134

135     /** Define custom file filter for acceptable image files.
136      */

137     private static class ImageFileFilter extends javax.swing.filechooser.FileFilter JavaDoc {
138         
139         public boolean accept(java.io.File JavaDoc file) {
140             if (file == null)
141                 return false;
142             return file.isDirectory() || file.getName().toLowerCase().endsWith(".gif") || file.getName().toLowerCase().endsWith(".jpg");
143         }
144         
145         public String JavaDoc getDescription() {
146             return "Image files (*.gif, *.jpg)";
147         }
148         
149     }
150
151
152     // Variables declaration - do not modify//GEN-BEGIN:variables
153
private javax.swing.JDesktopPane JavaDoc desktop;
154     private javax.swing.JMenuItem JavaDoc exitMenuItem;
155     private javax.swing.JMenu JavaDoc fileMenu;
156     private javax.swing.JSeparator JavaDoc jSeparator1;
157     private javax.swing.JMenuBar JavaDoc mainMenuBar;
158     private javax.swing.JMenuItem JavaDoc openMenuItem;
159     // End of variables declaration//GEN-END:variables
160

161
162     /** Starts the application.
163      * @param args Application arguments.
164      */

165     public static void main(String JavaDoc args[]) {
166         new ImageViewer().show();
167     }
168
169 }
170
Popular Tags