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

This page was automatically generated by Maven