1   /*****************************************************************************
2    *                   Virtual Mockup for Machine Vision
3    *  Copyright (C) 2001-2003 Fabio R. de Miranda, João E. Kogler Jr., 
4    *                         Carlos S. Santos.
5    *   Virtual Mockup for Machine Vision Project funded by SENAC-SP
6    *
7    * Permission is granted to redistribute and/or modify this
8    * software under the terms of the GNU Lesser General Public
9    * License as published by the Free Software Foundation; either
10   * version 2.1 of the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15   * Lesser General Public License (http://www.gnu.org/copyleft/lesser.html)
16   * for more details.
17   *
18   *****************************************************************************/
19  
20  package camera3d.gui;
21  
22  
23  
24  import java.awt.*;
25  import javax.swing.*;
26  import java.awt.event.*;
27  
28  /***
29   * Simple dialog window for asking the user which is the desired size of snapshot
30   * image to be saved.
31   *
32   * @author Fábio Roberto de Miranda e Carlos da Silva dos Santos
33   * @version 1.0
34   */
35  class SavedImageSizeDialog extends JDialog {
36  
37      public static final int OK_SELECTED = 0;
38      public static final int CANCEL_SELECTED = 1;
39  
40      private int lastUserAction = -1;
41  
42      private int imageWidth;
43      private int imageHeight;
44  
45      private boolean validatedFields = false;
46  
47      private JButton okBtn = new JButton();
48      private JButton cancelBtn = new JButton();
49      private JTextField widthTxt = new JTextField();
50      private JTextField heightTxt = new JTextField();
51      private JLabel widthLabel = new JLabel();
52      private JLabel heightLabel = new JLabel();
53      private GridBagLayout gridBagLayout1 = new GridBagLayout();
54  
55      public SavedImageSizeDialog() {
56          this.setModal(true);
57          try {
58              jbInit();
59          }
60          catch(Exception e) {
61              e.printStackTrace();
62          }
63      }
64  
65      private void jbInit() throws Exception {
66          okBtn.setText("OK");
67          okBtn.addActionListener(new java.awt.event.ActionListener() {
68              public void actionPerformed(ActionEvent e) {
69                  okBtn_actionPerformed(e);
70              }
71          });
72          this.getContentPane().setLayout(gridBagLayout1);
73          cancelBtn.addActionListener(new java.awt.event.ActionListener() {
74              public void actionPerformed(ActionEvent e) {
75                  cancelBtn_actionPerformed(e);
76              }
77          });
78          cancelBtn.setText("Cancel");
79          this.setTitle("Please select image size");
80          widthTxt.setText(imageWidth+"");
81          widthTxt.setHorizontalAlignment(SwingConstants.RIGHT);
82          heightTxt.setText(imageHeight+"");
83          heightTxt.setHorizontalAlignment(SwingConstants.RIGHT);
84          widthLabel.setText("width");
85          heightLabel.setText("height");
86          this.getContentPane().add(heightTxt, new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0
87              ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(6, 0, 0, 98), 61, 6));
88          this.getContentPane().add(cancelBtn, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0
89              ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(17, 0, 35, 91), 2, 0));
90          this.getContentPane().add(okBtn, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0
91              ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(17, 80, 35, 0), 24, 0));
92          this.getContentPane().add(widthTxt, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0
93              ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(54, 0, 0, 97), 61, 6));
94          this.getContentPane().add(heightLabel, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
95              ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(10, 88, 6, 26), 6, 0));
96          this.getContentPane().add(widthLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
97              ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(54, 87, 7, 27), 10, 0));
98          this.setSize(400, 250);
99      }
100 
101     void cancelBtn_actionPerformed(ActionEvent e) {
102         this.lastUserAction = CANCEL_SELECTED;
103         this.hide();
104     }
105 
106     /***
107      * Returns flag indicating last action selected by user. It can be either OK_SELECTED
108      * or CANCEL_SELECTED.
109      */
110     public int getUserAction(){
111         return lastUserAction;
112     }
113 
114 
115     /***
116      * Returns desired width of image.
117      */
118     public int getWidth(){
119         return this.imageWidth;
120     }
121 
122     /***
123      * Returns desired height of image.
124      */
125     public int getHeight(){
126         return this.imageHeight;
127     }
128 
129     void okBtn_actionPerformed(ActionEvent e) {
130         validateUserInput();
131         if (validatedFields){
132             this.lastUserAction = OK_SELECTED;
133             this.hide();
134         }
135     }
136 
137     /***
138      * Sets value of image size.
139      * @param w image width.
140      * @param h image height.
141      */
142     public void setImageSize(int w, int h){
143         this.imageHeight = h;
144         this.imageWidth = w;
145         this.widthTxt.setText(w + "");
146         this.heightTxt.setText(h + "");
147     }
148 
149     /***
150      * Checks whether the values enteres by the user are valid. The validatedFields
151      * flag is set according to the test result.
152      */
153     void validateUserInput(){
154         int w;
155         int h;
156         try {
157             w = Integer.parseInt(widthTxt.getText());
158             h = Integer.parseInt(heightTxt.getText());
159         } catch (NumberFormatException ex){
160             this.validatedFields = false;
161             return;
162         }
163         this.imageWidth = w;
164         this.imageHeight = h;
165         this.validatedFields = true;
166     }
167 
168 }
This page was automatically generated by Maven