KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > swing > JPointer


1 package JSci.swing;
2
3 import java.awt.*;
4 import java.awt.event.*;
5 import java.awt.geom.*;
6 import java.util.*;
7 import javax.swing.*;
8 import javax.swing.event.*;
9
10 /** A pointer for the JRoundDial or JArcDial */
11 public class JPointer {
12
13     private double s;
14     private int type;
15     private Shape shape = null;
16     private Color color = Color.BLACK;
17     private boolean enabled = true;
18     private boolean adjusting = false;
19
20
21     /**
22      * @param t the type of the pointer
23      */

24     public JPointer(int t) {
25     s=0.0;
26     type=t;
27     }
28
29     
30     /** get the value of the pointer
31      * @return the value
32      */

33     public double getValue() {
34     return s;
35     }
36
37     /** set the value of the pointer
38      * @param v the value
39      */

40     public void setValue(double v) {
41     if (s==v) return;
42     s=v;
43     if (!adjusting) fireStateChanged();
44     }
45
46     /** enable or disable the motion by the mouse
47      * @param b should the mouse move the pointer?
48      */

49     public void setEnabled(boolean b) {
50     enabled=b;
51     }
52
53     /** If you are adjusting the value, the changes should not be fired
54      * (dispatched) to the listeners. Typically, when the movement is done by
55      * the mouse, only releasing the button actually fires the change.
56      * If appropriate, this fires the state changed.
57      * @param b are the following movements adjustments?
58      */

59     public void setAdjusting(boolean b) {
60     if (adjusting && !b) fireStateChanged();
61     adjusting=b;
62     }
63
64
65     /** set the color of the pointer
66      * @param col the desired color
67      */

68     public void setColor(Color col) {
69     color=col;
70     }
71
72     /** check if a point is inside the pointer image
73      * @param p the point
74      * @return is p inside the pointer image
75      */

76     public boolean contains(Point p) {
77     return shape.contains(p) && enabled;
78     }
79     
80     /** draw the pointer on the dial. Called by the dials.
81      * @param g the graphics on which to draw
82      * @param radius the radius of the dial
83      * @param one the value of one turn
84      * @param zero the angle (clockwise, starting from the top) at which the zero should
85      * be placed
86      * @param x0 horizontal coordinate of the center of the dial
87      * @param y0 vertical coordinate of the center of the dial
88      */

89     protected void paintOnDial(Graphics2D g,double radius,double one,double zero,double x0,double y0) {
90     AffineTransform at = AffineTransform.getTranslateInstance(x0,y0);
91     at.scale(radius/1000.0,radius/1000.0);
92     at.rotate(2.0*Math.PI*getValue()/one+zero);
93     switch (type) {
94     case POINTER_SIMPLE_TRIANGLE:
95         g.setColor(color);
96         shape=at.createTransformedShape(POINTER_SHAPE_SIMPLE_TRIANGLE);
97         g.fill(shape);
98         break;
99     case POINTER_SIMPLE_QUADRANGLE:
100         g.setColor(color);
101         shape=at.createTransformedShape(POINTER_SHAPE_SIMPLE_QUADRANGLE);
102         g.fill(shape);
103         break;
104     case POINTER_SIMPLE_STOP:
105         g.setColor(color);
106         shape=at.createTransformedShape(POINTER_SHAPE_SIMPLE_STOP);
107         g.fill(shape);
108         break;
109     }
110     }
111
112     /** draw the slider on the dial. Called by the dials.
113      * @param g the graphics on which to draw
114      * @param start the value on the bottom of the JSliderPlus
115      * @param end the value on the top of the JSliderPlus
116      * @param width the width of the component
117      * @param heigth the heigth of the component
118      */

119     protected void paintOnSlider(Graphics2D g,double start,double end,double width,double heigth) {
120     AffineTransform at = AffineTransform.getTranslateInstance(
121                                   width/2.0,
122                                   heigth*(0.9-0.8*(s-start)/(end-start))
123                                   );
124     at.scale(width/1000.0,heigth/2000.0);
125     switch (type) {
126     case SLIDER_SIMPLE_TRIANGLE:
127         g.setColor(color);
128         shape=at.createTransformedShape(SLIDER_SHAPE_SIMPLE_TRIANGLE);
129         g.fill(shape);
130         break;
131     case SLIDER_SIMPLE_QUADRANGLE:
132         g.setColor(color);
133         shape=at.createTransformedShape(SLIDER_SHAPE_SIMPLE_QUADRANGLE);
134         g.fill(shape);
135         break;
136     case SLIDER_SIMPLE_STOP:
137         g.setColor(color);
138         shape=at.createTransformedShape(SLIDER_SHAPE_SIMPLE_STOP);
139         g.fill(shape);
140         break;
141     }
142     }
143     
144
145     // EVENTS HANDLING
146

147     /**
148      * Only one ChangeEvent is needed per model instance since the
149      * event's only (read-only) state is the source property. The source
150      * of events generated here is always "this".
151      */

152     private transient ChangeEvent changeEvent = null;
153
154     /**
155      * The list of ChangeListeners for this model. Subclasses may
156      * store their own listeners here.
157      */

158     protected EventListenerList listenerList = new EventListenerList();
159
160
161     /**
162      * Adds a ChangeListener to the model's listener list. The
163      * ChangeListeners must be notified when the models value changes.
164      *
165      * @param l the ChangeListener to add
166      * @see #removeChangeListener
167      */

168     public void addChangeListener(ChangeListener l) {
169         listenerList.add(ChangeListener.class, l);
170     }
171     
172
173     /**
174      * Removes a ChangeListener from the model's listener list.
175      *
176      * @param l the ChangeListener to remove
177      * @see #addChangeListener
178      */

179     public void removeChangeListener(ChangeListener l) {
180         listenerList.remove(ChangeListener.class, l);
181     }
182
183
184     /**
185      * Returns an array of all the <code>ChangeListener</code>s added
186      * to this JPointer with addChangeListener().
187      *
188      * @return all of the <code>ChangeListener</code>s added or an empty
189      * array if no listeners have been added
190      */

191     public ChangeListener[] getChangeListeners() {
192         return (ChangeListener[])listenerList.getListeners(
193                                ChangeListener.class);
194     }
195
196
197     /**
198      * Run each ChangeListeners stateChanged() method.
199      *
200      * @see #setValue
201      * @see EventListenerList
202      */

203     protected void fireStateChanged()
204     {
205         Object JavaDoc[] listeners = listenerList.getListenerList();
206         for (int i = listeners.length - 2; i >= 0; i -=2 ) {
207             if (listeners[i] == ChangeListener.class) {
208                 if (changeEvent == null) {
209                     changeEvent = new ChangeEvent(this);
210                 }
211                 ((ChangeListener)listeners[i+1]).stateChanged(changeEvent);
212             }
213         }
214     }
215
216
217     /**
218      * Return an array of all the listeners of the given type that
219      * were added to this model. For example to find all of the
220      * ChangeListeners added to this model:
221      * <pre>
222      * myJPointer.getListeners(ChangeListener.class);
223      * </pre>
224      *
225      * @param listenerType the type of listeners to return, e.g. ChangeListener.class
226      * @return all of the objects receiving <em>listenerType</em> notifications
227      * from this model
228      */

229     public EventListener[] getListeners(Class JavaDoc listenerType) {
230     return listenerList.getListeners(listenerType);
231     }
232
233
234
235     // POINTER TYPES AND SHAPES
236

237
238     public static final int POINTER_SIMPLE_TRIANGLE = 0;
239     public static final int POINTER_SIMPLE_QUADRANGLE = 1;
240     public static final int POINTER_SIMPLE_STOP = 2;
241
242
243     private static final Shape POINTER_SHAPE_SIMPLE_TRIANGLE;
244     private static final Shape POINTER_SHAPE_SIMPLE_QUADRANGLE;
245     private static final Shape POINTER_SHAPE_SIMPLE_STOP;
246
247     
248     static {
249     POINTER_SHAPE_SIMPLE_TRIANGLE = new Polygon(
250                             new int[] {-100,100,00},
251                             new int[] {100,100,-600},
252                             3);
253     POINTER_SHAPE_SIMPLE_QUADRANGLE = new Polygon(
254                               new int[] {-100,0,100,00},
255                               new int[] {0,100,0,-600},
256                               4);
257     int []x = new int[16];
258     int []y = new int[16];
259     for (int j=0;j<16;j++) {
260         x[j]=(int)(30*Math.cos(2.0*Math.PI*j/16));
261         y[j]=(int)(-595+30*Math.sin(2.0*Math.PI*j/16));
262     }
263     POINTER_SHAPE_SIMPLE_STOP = new Polygon(x,y,16);
264     }
265
266
267     // SLIDER TYPES AND SHAPES
268

269     
270     public static final int SLIDER_SIMPLE_TRIANGLE = 3;
271     public static final int SLIDER_SIMPLE_QUADRANGLE = 4;
272     public static final int SLIDER_SIMPLE_STOP = 5;
273
274
275     private static final Shape SLIDER_SHAPE_SIMPLE_TRIANGLE;
276     private static final Shape SLIDER_SHAPE_SIMPLE_QUADRANGLE;
277     private static final Shape SLIDER_SHAPE_SIMPLE_STOP;
278
279     
280     static {
281     SLIDER_SHAPE_SIMPLE_TRIANGLE = new Polygon(
282                         new int[] {100,0,100},
283                         new int[] {-100,0,100},
284                         3);
285     SLIDER_SHAPE_SIMPLE_QUADRANGLE = new Polygon(
286                           new int[] {100,0,100,50},
287                           new int[] {-100,0,100,0},
288                           4);
289     int []x = new int[16];
290     int []y = new int[16];
291     for (int j=0;j<16;j++) {
292         x[j]=(int)(200+30*Math.cos(2.0*Math.PI*j/16));
293         y[j]=(int)(30*Math.sin(2.0*Math.PI*j/16));
294     }
295     SLIDER_SHAPE_SIMPLE_STOP = new Polygon(x,y,16);
296     }
297
298
299
300 }
301
Popular Tags