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 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 24 public JPointer(int t) { 25 s=0.0; 26 type=t; 27 } 28 29 30 33 public double getValue() { 34 return s; 35 } 36 37 40 public void setValue(double v) { 41 if (s==v) return; 42 s=v; 43 if (!adjusting) fireStateChanged(); 44 } 45 46 49 public void setEnabled(boolean b) { 50 enabled=b; 51 } 52 53 59 public void setAdjusting(boolean b) { 60 if (adjusting && !b) fireStateChanged(); 61 adjusting=b; 62 } 63 64 65 68 public void setColor(Color col) { 69 color=col; 70 } 71 72 76 public boolean contains(Point p) { 77 return shape.contains(p) && enabled; 78 } 79 80 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 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 147 152 private transient ChangeEvent changeEvent = null; 153 154 158 protected EventListenerList listenerList = new EventListenerList(); 159 160 161 168 public void addChangeListener(ChangeListener l) { 169 listenerList.add(ChangeListener.class, l); 170 } 171 172 173 179 public void removeChangeListener(ChangeListener l) { 180 listenerList.remove(ChangeListener.class, l); 181 } 182 183 184 191 public ChangeListener[] getChangeListeners() { 192 return (ChangeListener[])listenerList.getListeners( 193 ChangeListener.class); 194 } 195 196 197 203 protected void fireStateChanged() 204 { 205 Object [] 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 229 public EventListener[] getListeners(Class listenerType) { 230 return listenerList.getListeners(listenerType); 231 } 232 233 234 235 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 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 |