3 class CbSlider extends Canvas
5 int dir, min, max, pos;
6 CbSliderCallback callback;
8 Color lc1 = Util.light_edge, lc2 = Util.body, lc3 = Util.dark_edge;
9 Color hc1 = Util.light_edge_hi, hc2 = Util.body_hi, hc3 = Util.dark_edge_hi;
11 boolean inside = false, dragging = false;
14 /**Create a new slider
15 * @param d 0=horizontal, 1=vertical
16 * @param mi Minimum value
17 * @param ma Maximum value
18 * @param p Current value
20 public CbSlider(int d, int mi, int ma, int p)
22 this(d, mi, ma, p, null);
25 /**Create a new slider
26 * @param d 0=horizontal, 1=vertical
27 * @param mi Minimum value
28 * @param ma Maximum value
29 * @param p Current value
30 * @param cb Object to call back to
32 public CbSlider(int d, int mi, int ma, int p, CbSliderCallback cb)
34 dir = d; min = mi; max = ma;
39 /**Toggle drawing of tick-marks on the slider track
40 * @param t The number of units/tick, or 0 to disable
42 public void setTicks(int t)
48 /**Returns the current slider position
50 public int getPosition() { return pos; }
52 /**Sets the current slider position
54 public void setPosition(int p)
62 /**Returns the current minimum slider value
64 public int getMinimum() { return min; }
66 /**Sets the minimum slider value
67 * @param mi The new minimum
69 public void setMinimum(int mi)
76 /**Returns the current maximum slider value
78 public int getMaximum() { return max; }
80 /**Sets the maximum slider value
81 * @param mx The new maximum
83 public void setMaximum(int mx)
90 public void paint(Graphics g)
92 Color c1 = inside ? hc1 : lc1,
93 c2 = inside ? hc2 : lc2,
94 c3 = inside ? hc3 : lc3;
97 int w = size().width, h = size().height;
99 g.fillRect(0, 0, w, h);
101 g.drawLine(8, h/2, w-8, h/2);
103 g.drawLine(8, h/2+1, w-8, h/2+1);
107 g.drawLine(0, 0, w-1, 0);
108 g.drawLine(0, 0, 0, h-1);
110 g.drawLine(w-1, h-1, w-1, 0);
111 g.drawLine(w-1, h-1, 0, h-1);
113 g.drawLine(w-2, h-2, w-2, 0);
114 g.drawLine(w-2, h-2, 0, h-2);
120 for(int i=0; i<=mm; i+=ticks) {
121 int tx = ((w-16)*i / mm) + 8;
123 g.drawLine(tx, h/2, tx, h/2-6);
128 px = ((w-16)*pos / (max - min)) + 8;
131 int xpt[] = { px-3, px-3, px, px+3, px+3 };
132 int ypt[] = { py+5, py-4, py-6, py-4, py+5 };
133 g.fillPolygon(xpt, ypt, 5);
134 g.setColor(dragging ? c3 : c1);
135 g.drawLine(px-3, py+5, px-3, py-4);
136 g.drawLine(px-3, py-4, px, py-6);
137 g.setColor(dragging ? c1 : c3);
138 g.drawLine(px-3, py+5, px+3, py+5);
139 g.drawLine(px+3, py+5, px+3, py-4);
142 public void update(Graphics g) { paint(g); }
144 public boolean mouseEnter(Event e, int x, int y)
151 public boolean mouseDown(Event e, int x, int y)
153 int step = ticks==0 ? (max-min)/10 : ticks;
155 // move one tick to the left
159 // move one tick to the right
168 if (callback != null)
169 callback.moved(this, pos);
174 public boolean mouseDrag(Event e, int x, int y)
178 pos = (px-8)*(max - min) / (size().width-16);
180 if (callback != null)
181 callback.moving(this, pos);
187 public boolean mouseUp(Event e, int x, int y)
191 if (callback != null)
192 callback.moved(this, pos);
199 public boolean mouseExit(Event e, int x, int y)
206 protected void checkPos()
208 if (pos < min) pos = min;
209 else if (pos > max) pos = max;
212 public Dimension preferredSize()
214 return new Dimension(100, 20);
217 public Dimension minimumSize() { return preferredSize(); }
220 interface CbSliderCallback
222 /**Callled back when the slider stops at a new position
223 * @param s The slider being moved
224 * @param p New position
226 public void moved(CbSlider s, int p);
228 /**Callled back whenever the slider is being dragged
229 * @param s The slider being moved
230 * @param p New position
232 public void moving(CbSlider s, int p);