View Javadoc

1   /*
2    * Copyright (c) 2003-2008 by Cosylab d. d.
3    *
4    * This file is part of CosyBeans-Common.
5    *
6    * CosyBeans-Common is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * CosyBeans-Common is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with CosyBeans-Common.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package com.cosylab.gui.components.wheelswitch;
21  
22  import java.awt.Graphics;
23  import java.awt.Graphics2D;
24  import java.awt.image.BufferedImage;
25  
26  /**
27   * Descedant of <code>Digit</code> displaying a integer value digit.
28   *
29   * @author <a href="mailto:jernej.kamenik@cosylab.com">Jernej Kamenik</a>
30   * @version $id$
31   */
32  public class ValueDigit extends Digit {
33  
34  	private static final long serialVersionUID = 1L;
35  	public static int INCREASE_VALUE = -1;
36  	public static int DECREASE_VALUE = -2;
37  
38  	private int value;
39  	private int oldValue;
40  
41  	/**
42  	 * Constructor for ValueDigit.
43  	 *
44  	 * @param newValue accepts 0 to 9.
45  	 */
46  	public ValueDigit(int newValue) throws IllegalArgumentException {
47  		super();
48  		if (newValue>=0 && newValue<=9) {
49  			value=newValue;
50  			setText(String.valueOf(value));
51  		} else throw new IllegalArgumentException();
52  	}
53  
54  	/**
55  	 * Sets the value displayed.
56  	 *
57  	 * @param newValue INCREASE_VALUE increases the value,
58  	 * DECREASE_VALUE decreases the value, else the number newValue is set.
59  	 */
60  	public void setValue(int newValue) throws IllegalArgumentException {
61  		if (newValue>=0 && newValue<=9) {
62  			oldValue=value;
63  			value=newValue;
64  		} else if (newValue==INCREASE_VALUE) {
65  			if (value==9) value=0;
66  			else value++;
67  		} else if (newValue==DECREASE_VALUE) {
68  			if (value==0) value=9;
69  			else value--;
70  		} else throw new IllegalArgumentException();
71  		setText(String.valueOf(value));
72  	}
73  
74  	/**
75  	 * Returns the value displayed.
76  	 *
77  	 * @return int
78  	 */
79  	public int getValue() {
80  		return value;
81  	}
82  
83  
84  	/**
85  	 * This method was overriden to implement animated number digit
86  	 * scrolling.
87  	 */
88  	protected void paintDigitTransition(BufferedImage oldImage,BufferedImage newImage, Graphics g, float parameter) {
89  		Graphics2D g2D = (Graphics2D)g;
90  		if ((value>oldValue && (value!=9 || oldValue!=0)) || (value==0 && oldValue==9)) {
91  			g2D.translate(0,(int)(getHeight()*(parameter-1.)));
92  			if (newImage!=null) g2D.drawImage(newImage,null,0,0);
93  			g2D.translate(0,getHeight());
94  			if (oldImage!=null) g2D.drawImage(oldImage,null,0,0);
95        		g2D.translate(0,-(int)(getHeight()*(parameter-1.))-getHeight());
96  		} else {
97  			g2D.translate(0,(int)(getHeight()*(1.-parameter)));
98  			if (newImage!=null) g2D.drawImage(newImage,null,0,0);
99  			g2D.translate(0,-getHeight());
100 			if (oldImage!=null) g2D.drawImage(oldImage,null,0,0);
101       		g2D.translate(0,getHeight()-(int)(getHeight()*(1.-parameter)));
102 		}
103 	}
104 
105 
106 }