KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > swing > JSliderPlus


1 package JSci.swing;
2
3 import java.awt.*;
4 import java.awt.geom.*;
5 import java.awt.event.*;
6 import javax.swing.*;
7 import javax.swing.event.*;
8 import java.text.*;
9 import java.util.*;
10
11 /** A place where <code>JPointer</code>s can be placed.
12     <code>JPointer</code>s must be added. */

13 public class JSliderPlus extends JPanel {
14
15     private double startValue;
16     private double endValue;
17     private double minorTic = 10;
18     private double majorTic = 20;
19     private boolean setPaintMinorTicks = true;
20     private boolean setPaintMajorTicks = true;
21     private boolean setPaintLabels = true;
22     private ArrayList sliders = new ArrayList();
23     private ChangeListener changeListener = new ChangeListener() {
24         public void stateChanged(ChangeEvent event) {
25         repaint();
26         }
27     };
28
29     /** For building a vertical SliderHolder */
30     public static final int VERTICAL = 0;
31     /** For building a horizontal SliderHolder */
32     public static final int HORIZONTAL = 1;
33
34     /** constructs a SliderHolder with no sliders; the sliders will move
35      * inside a given range.
36      * @param o orientation (currently only VERTICAL)
37      * @param start starting value of the SliderHolder
38      * @param end end value of the SliderHolder
39      */

40     public JSliderPlus(int o,double start,double end) {
41     startValue=start;
42     endValue=end;
43     setSize(getPreferredSize());
44     updateMinorTics();
45     updateMajorTics();
46     addMouseListener(new TheMouseListener());
47     addMouseMotionListener(new TheMouseMotionListener());
48     }
49
50     /** set the limits of the Slider
51      * @param start starting value of the SliderHolder
52      * @param end end value of the SliderHolder
53      */

54     public void setLimits(double start,double end) {
55     startValue=start;
56     endValue=end;
57     setSize(getPreferredSize());
58     updateMinorTics();
59     updateMajorTics();
60     }
61
62     /** as described in java.awt.Component */
63     public Dimension getPreferredSize() {
64     return new Dimension(100,200);
65     }
66
67     /** set the spacing between minor tics
68      * @param s the spacing between minor tics
69      */

70     public void setMinorTickSpacing(double s) {
71     minorTic = s;
72     updateMinorTics();
73     repaint();
74     }
75
76     /** set the spacing between major tics
77      * @param s the spacing between major tics
78      */

79     public void setMajorTickSpacing(double s) {
80     majorTic = s;
81     updateMajorTics();
82     repaint();
83     }
84
85     /** show the minor tics?
86      * @param b show the minor tics?
87      */

88     public void setPaintMinorTicks(boolean b) {
89     setPaintMinorTicks = b;
90     repaint();
91     }
92
93     /** show the major tics?
94      * @param b show the major tics?
95      */

96     public void setPaintMajorTicks(boolean b) {
97     setPaintMajorTicks = b;
98     repaint();
99     }
100
101     /** show the tic labels?
102      * @param b show the tic labels?
103      */

104     public void setPaintLabels(boolean b) {
105     setPaintLabels = b;
106     repaint();
107     }
108
109     private Line2D [] minorTics = null;
110     private Line2D [] majorTics = null;
111     private Point2D [] ticLabelPos = null;
112     private String JavaDoc [] ticLabelText = null;
113     private static NumberFormat formatter;
114     static {
115     formatter = NumberFormat.getNumberInstance();
116     formatter.setMaximumFractionDigits(2);
117     formatter.setMaximumIntegerDigits(3);
118     }
119
120     private void updateMinorTics() {
121     double width = getSize().getWidth();
122     double heigth = getSize().getHeight();
123     int N =(int)Math.abs((endValue-startValue)/minorTic);
124     minorTics = new Line2D[N+1];
125     for (int j=0;j<=N;j++)
126         minorTics[j] = new Line2D.Double(
127                          width*0.3,heigth*(0.9-j*0.8/N),
128                          width*0.4,heigth*(0.9-j*0.8/N)
129                          );
130     }
131
132     private void updateMajorTics() {
133     double width = getSize().getWidth();
134     double heigth = getSize().getHeight();
135     int N =(int)Math.abs((endValue-startValue)/majorTic);
136     majorTics = new Line2D[N+1];
137     ticLabelPos = new Point2D[N+1];
138     ticLabelText = new String JavaDoc[N+1];
139     for (int j=0;j<=N;j++) {
140         majorTics[j] =
141         new Line2D.Double(
142                   width*0.25,heigth*(0.9-j*0.8/N),
143                   width*0.4,heigth*(0.9-j*0.8/N)
144                   );
145         ticLabelText[j] = formatter.format(startValue+j*(endValue-startValue)/N);
146         ticLabelPos[j] =
147         new Point2D.Double(0.0,heigth*(0.9-j*0.8/N)+6);
148     }
149     }
150
151
152     /** draw the component - don't call this directly, but use repaint() */
153     public void paintComponent(Graphics g) {
154     super.paintComponent(g);
155     Graphics2D g2 = (Graphics2D)g;
156     g2.draw(new Rectangle2D.Double(
157                        getSize().getWidth()*0.45,getSize().getHeight()*0.05,
158                        getSize().getWidth()*0.1,getSize().getHeight()*0.9)
159         );
160     if (setPaintMinorTicks)
161         for (int j=0;j<minorTics.length;j++)
162         g2.draw(minorTics[j]);
163     if (setPaintMajorTicks)
164         for (int j=0;j<majorTics.length;j++)
165         g2.draw(majorTics[j]);
166     if (setPaintLabels)
167         for (int j=0;j<majorTics.length;j++)
168         g2.drawString(ticLabelText[j],(int)ticLabelPos[j].getX(),(int)ticLabelPos[j].getY());
169     Color saveColor = g2.getColor();
170     for (int j=0;j<sliders.size();j++)
171         ((JPointer)sliders.get(j)).paintOnSlider(g2,startValue,endValue,getSize().getWidth(),getSize().getHeight());
172     g2.setColor(saveColor);
173     }
174
175     /** add a slider; it will be the actual component that contains informations
176      * @param p the slider that must be added
177      */

178     public void addJPointer(JPointer p) {
179     for (int j=0;j<sliders.size();j++) if (sliders.get(j).equals(p)) return;
180     sliders.add(p);
181     p.addChangeListener(changeListener);
182     repaint();
183     }
184
185     /** remove a slider; it should have been added with addJPointer()
186      * @param p the slider that must be removed
187      */

188     public void removeJPointer(JPointer p) {
189     int j=0;
190     while (j<sliders.size()) {
191         if (sliders.get(j).equals(p)) sliders.remove(j);
192         else j++;
193     }
194     p.removeChangeListener(changeListener);
195     repaint();
196     }
197
198     // Mouse listeners
199

200     private JPointer dragged = null;
201     private double dragDelta;
202     private double mousePosition(MouseEvent e) {
203     return startValue+(0.9-e.getY()/getSize().getHeight())/0.8*(endValue-startValue);
204     }
205     private class TheMouseListener extends MouseAdapter {
206     public void mousePressed(MouseEvent e) {
207         for (int j=sliders.size()-1;j>=0;j--)
208         if (((JPointer)sliders.get(j)).contains(e.getPoint())) {
209             dragged=(JPointer)sliders.get(j);
210             dragDelta=dragged.getValue()-mousePosition(e);
211             dragged.setAdjusting(true);
212             break;
213         }
214     }
215     public void mouseReleased(MouseEvent e) {
216         if (dragged!=null) dragged.setAdjusting(false);
217         dragged = null;
218         boolean inside = false;
219         for (int j=0;j<sliders.size();j++)
220         if (((JPointer)sliders.get(j)).contains(e.getPoint())) {
221             inside=true;
222             break;
223         }
224         if (inside) setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
225         else setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
226     }
227     }
228
229     private class TheMouseMotionListener extends MouseMotionAdapter {
230     public void mouseMoved(MouseEvent e) {
231         boolean inside = false;
232         for (int j=0;j<sliders.size();j++)
233         if (((JPointer)sliders.get(j)).contains(e.getPoint())) {
234             inside=true;
235             break;
236         }
237         if (inside) setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
238         else setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
239     }
240     public void mouseDragged(MouseEvent e) {
241         if (dragged!=null) {
242         double to = (mousePosition(e)+dragDelta);
243         if (to>Math.max(startValue,endValue)) to=Math.max(startValue,endValue);
244         if (to<Math.min(startValue,endValue)) to=Math.min(startValue,endValue);
245         dragged.setValue(to);
246         repaint();
247         }
248     }
249     }
250
251
252
253     // Main
254

255     public static void main(String JavaDoc [] args) {
256     JFrame frm = new JFrame();
257     frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
258     JSliderPlus d = new JSliderPlus(JSliderPlus.VERTICAL,0.0,100.0);
259     frm.getContentPane().add(d);
260     frm.pack();
261     frm.show();
262         
263     try { Thread.sleep(3000); } catch (InterruptedException JavaDoc e) {}
264     JPointer p = new JPointer(JPointer.SLIDER_SIMPLE_QUADRANGLE);
265     p.setColor(Color.MAGENTA);
266     p.setValue(25.0);
267     d.addJPointer(p);
268     try { Thread.sleep(3000); } catch (InterruptedException JavaDoc e) {}
269     JPointer p1 = new JPointer(JPointer.SLIDER_SIMPLE_TRIANGLE);
270     p1.setColor(Color.CYAN);
271     p1.setValue(75.0);
272     d.addJPointer(p1);
273         
274     p1.addChangeListener(new ChangeListener() {
275         public void stateChanged(ChangeEvent e) {
276             double v=((JPointer)e.getSource()).getValue();
277             System.out.println("State changed: "+v);
278         }
279         });
280
281     d.setLimits(-10.0,140.0);
282     d.setMinorTickSpacing(10.0);
283     d.setMajorTickSpacing(30.0);
284
285     //try { Thread.sleep(3000); } catch (InterruptedException e) {}
286
//p.setValue(p.getValue()+10.0);
287
//try { Thread.sleep(3000); } catch (InterruptedException e) {}
288
//p.setValue(p.getValue()+10.0);
289
//try { Thread.sleep(3000); } catch (InterruptedException e) {}
290
//p.setValue(p.getValue()+10.0);
291
//try { Thread.sleep(3000); } catch (InterruptedException e) {}
292
//p.setValue(p.getValue()+10.0);
293

294     //try { Thread.sleep(3000); } catch (InterruptedException e) {}
295
//d.removeJPointer(p);
296
//try { Thread.sleep(3000); } catch (InterruptedException e) {}
297
//d.removeJPointer(p1);
298
//try { Thread.sleep(3000); } catch (InterruptedException e) {}
299
}
300
301
302     
303 }
304
Popular Tags