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 import java.awt.*;
23 import javax.swing.*;
24 import java.text.*;
25 import java.awt.event.*;
26 import java.util.*;
27 import camera3d.*;
28 import java.io.Serializable;
29
30
31
32 /***
33 * This component is inspired by some value-editing fields present at 3DStudio Max
34 * it allow the registration of objects that implement the interface ValueChangeListener
35 * interested in notification when the value of this component changes
36 * @author Fábio Roberto de Miranda
37 * @version 1.0
38 */
39 public class IntDigitalControl extends JPanel implements Serializable, IntControl {
40 JTextField controlTxtField = new JTextField();
41 JButton upBtn = new JButton();
42 JButton downBtn = new JButton();
43 DecimalFormat format;
44 DecimalFormatSymbols decSymbols = new DecimalFormatSymbols();
45
46 Vector valueChangedListeners = new Vector(20, 10);
47
48 private int value;
49 private int incrementStep = 1;
50 private boolean debugEnable = false;
51 private boolean dragging = false;
52
53 private Point dragBeginPoint;
54 private int dragBeginValue;
55
56 private int lastDelta;
57
58 public IntDigitalControl() {
59 decSymbols.setDecimalSeparator('.');
60 format = new DecimalFormat("###.##", decSymbols);
61 try {
62 jbInit();
63 }
64 catch(Exception e) {
65 e.printStackTrace();
66 }
67 debug("Int digital control init in debug mode");
68 }
69
70 private void jbInit() throws Exception {
71 //format.setDecimalSeparatorAlwaysShown(true);
72 //format.setMinimumFractionDigits(2);
73 controlTxtField.setToolTipText("dummy value");
74 controlTxtField.setText("0");
75 controlTxtField.setHorizontalAlignment(SwingConstants.RIGHT);
76 controlTxtField.setBounds(new Rectangle(4, 5, 38, 24));
77 controlTxtField.addActionListener(new java.awt.event.ActionListener() {
78 public void actionPerformed(ActionEvent e) {
79 controlTxtField_actionPerformed(e);
80 }
81 });
82 this.setLayout(null);
83 upBtn.setToolTipText("Press this button to increase the value seen at left");
84 upBtn.setMargin(new Insets(1, 1, 1, 1));
85 upBtn.setBounds(new Rectangle(42, 5, 20, 12));
86 upBtn.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
87 public void mouseDragged(MouseEvent e) {
88 upBtn_mouseDragged(e);
89 }
90 });
91 upBtn.addMouseListener(new java.awt.event.MouseAdapter() {
92 public void mouseClicked(MouseEvent e) {
93 upBtn_mouseClicked(e);
94 }
95 public void mousePressed(MouseEvent e) {
96 upBtn_mousePressed(e);
97 }
98 public void mouseReleased(MouseEvent e) {
99 upBtn_mouseReleased(e);
100 }
101 });
102 downBtn.setBounds(new Rectangle(42, 17, 20, 12));
103 downBtn.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
104 public void mouseDragged(MouseEvent e) {
105 downBtn_mouseDragged(e);
106 }
107 });
108 downBtn.addMouseListener(new java.awt.event.MouseAdapter() {
109 public void mouseClicked(MouseEvent e) {
110 downBtn_mouseClicked(e);
111 }
112 public void mousePressed(MouseEvent e) {
113 downBtn_mousePressed(e);
114 }
115 public void mouseReleased(MouseEvent e) {
116 downBtn_mouseReleased(e);
117 }
118 });
119 downBtn.setMargin(new Insets(1, 1, 1, 1));
120 downBtn.setToolTipText("Press this button to decrease the value seen at left");
121 this.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
122 public void mouseDragged(MouseEvent e) {
123 this_mouseDragged(e);
124 }
125 });
126 this.add(controlTxtField, null);
127 this.add(upBtn, null);
128 this.add(downBtn, null);
129 upBtn.setIcon(new ImageIcon(this.getClass().getResource("icons/up_arrow.gif")));
130 downBtn.setIcon(new ImageIcon(this.getClass().getResource("icons/down_arrow.gif")));
131 // upBtn.setIconImage((new ImageIcon("up_arrow.gif")).getImage());
132 // downBtn.setDisabledIcon(setIconImage((new ImageIcon("down_arrow.gif")).getImage());
133 }
134
135 public void increase(int step){
136 value += incrementStep;
137 lastDelta = incrementStep;
138 update();
139 }
140
141 public void decrease(int step){
142 value -= incrementStep;
143 lastDelta = -incrementStep;
144 update();
145 }
146
147 public void update(){
148 controlTxtField.setText(format.format(this.value));
149 notifyValueChangedListeners();
150 }
151
152 public void setIncrementStep(int d){
153 this.incrementStep = d;
154 }
155
156 public int getIncrementStep(){
157 return this.incrementStep;
158 }
159
160 public void debug(String s){
161 if (debugEnable){
162 System.out.println(s);
163 }
164 }
165
166 public void setDebugEnable(boolean b){
167 this.debugEnable = b;
168 }
169
170 public int getValue(){
171 return this.value;
172 }
173
174 /***
175 *
176 */
177 public static void main(String[] args) {
178 JFrame frame = new JFrame("Testing digital control");
179 }
180
181 void upBtn_mouseClicked(MouseEvent e) {
182 debug("Mouse clicked");
183 increase(incrementStep);
184 }
185
186 void downBtn_mouseClicked(MouseEvent e) {
187 debug("Mouse clicked");
188 decrease(incrementStep);
189 }
190
191 void this_mouseDragged(MouseEvent e) {
192 debug("Mouse dragged");
193 }
194
195 void upBtn_mouseDragged(MouseEvent e) {
196 Point currentPoint = e.getPoint();
197 int deltaX = currentPoint.x - dragBeginPoint.x;
198 int deltaY = currentPoint.y - dragBeginPoint.y;
199 if (deltaY < 0.0){
200 setValue(dragBeginValue - (deltaY)*incrementStep);
201 }
202 }
203
204 void upBtn_mousePressed(MouseEvent e) {
205 dragging = true;
206 dragBeginPoint = e.getPoint();
207 dragBeginValue = value;
208 }
209
210 void upBtn_mouseReleased(MouseEvent e) {
211 dragging = false;
212 }
213
214 public void setValue(int d){
215 this.value = d;
216 update();
217 }
218
219 public void resetDelta(){
220 this.lastDelta = 0;
221 }
222
223 void controlTxtField_actionPerformed(ActionEvent e) {
224 debug("Action Performed on text field");
225 int result;
226 try {
227 result = Integer.parseInt(controlTxtField.getText());
228 lastDelta = result - value;
229 this.value = result;
230 }catch (NumberFormatException nf){
231 }
232 update();
233 }
234
235 void downBtn_mouseDragged(MouseEvent e) {
236 Point currentPoint = e.getPoint();
237 int deltaX = currentPoint.x - dragBeginPoint.x;
238 int deltaY = currentPoint.y - dragBeginPoint.y;
239 if (deltaY > 0.0){
240 setValue(dragBeginValue - (deltaY)*incrementStep);
241 }
242 }
243
244 void downBtn_mousePressed(MouseEvent e) {
245 dragging = true;
246 dragBeginPoint = e.getPoint();
247 dragBeginValue = value;
248 }
249
250 void downBtn_mouseReleased(MouseEvent e) {
251 dragging = false;
252 }
253
254 void notifyValueChangedListeners(){
255 Iterator iter = valueChangedListeners.iterator();
256 while (iter.hasNext()){
257 ((IntValueChangeListener)iter.next()).valueChanged(new IntChangeEvent(this));
258 }
259 }
260
261 /*
262 * Registers a listener interested in notification of changes in this objects
263 */
264 public void addValueChangeListener(IntValueChangeListener v){
265 valueChangedListeners.addElement(v);
266 }
267
268 /*** Returns the amount this value was changed in the last time*/
269 public int getDelta(){
270 return this.lastDelta;
271 }
272 }
This page was automatically generated by Maven