1 7 8 package java.awt.geom; 9 10 import java.util.*; 11 12 19 class RoundRectIterator implements PathIterator { 20 double x, y, w, h, aw, ah; 21 AffineTransform affine; 22 int index; 23 24 RoundRectIterator(RoundRectangle2D rr, AffineTransform at) { 25 this.x = rr.getX(); 26 this.y = rr.getY(); 27 this.w = rr.getWidth(); 28 this.h = rr.getHeight(); 29 this.aw = Math.min(w, Math.abs(rr.getArcWidth())); 30 this.ah = Math.min(h, Math.abs(rr.getArcHeight())); 31 this.affine = at; 32 if (aw < 0 || ah < 0) { 33 index = ctrlpts.length; 35 } 36 } 37 38 44 public int getWindingRule() { 45 return WIND_NON_ZERO; 46 } 47 48 52 public boolean isDone() { 53 return index >= ctrlpts.length; 54 } 55 56 61 public void next() { 62 index++; 63 } 64 65 private static final double angle = Math.PI / 4.0; 66 private static final double a = 1.0 - Math.cos(angle); 67 private static final double b = Math.tan(angle); 68 private static final double c = Math.sqrt(1.0 + b * b) - 1 + a; 69 private static final double cv = 4.0 / 3.0 * a * b / c; 70 private static final double acv = (1.0 - cv) / 2.0; 71 72 private static double ctrlpts[][] = { 77 { 0.0, 0.0, 0.0, 0.5 }, 78 { 0.0, 0.0, 1.0, -0.5 }, 79 { 0.0, 0.0, 1.0, -acv, 80 0.0, acv, 1.0, 0.0, 81 0.0, 0.5, 1.0, 0.0 }, 82 { 1.0, -0.5, 1.0, 0.0 }, 83 { 1.0, -acv, 1.0, 0.0, 84 1.0, 0.0, 1.0, -acv, 85 1.0, 0.0, 1.0, -0.5 }, 86 { 1.0, 0.0, 0.0, 0.5 }, 87 { 1.0, 0.0, 0.0, acv, 88 1.0, -acv, 0.0, 0.0, 89 1.0, -0.5, 0.0, 0.0 }, 90 { 0.0, 0.5, 0.0, 0.0 }, 91 { 0.0, acv, 0.0, 0.0, 92 0.0, 0.0, 0.0, acv, 93 0.0, 0.0, 0.0, 0.5 }, 94 {}, 95 }; 96 private static int types[] = { 97 SEG_MOVETO, 98 SEG_LINETO, SEG_CUBICTO, 99 SEG_LINETO, SEG_CUBICTO, 100 SEG_LINETO, SEG_CUBICTO, 101 SEG_LINETO, SEG_CUBICTO, 102 SEG_CLOSE, 103 }; 104 105 123 public int currentSegment(float[] coords) { 124 if (isDone()) { 125 throw new NoSuchElementException("roundrect iterator out of bounds"); 126 } 127 double ctrls[] = ctrlpts[index]; 128 int nc = 0; 129 for (int i = 0; i < ctrls.length; i += 4) { 130 coords[nc++] = (float) (x + ctrls[i + 0] * w + ctrls[i + 1] * aw); 131 coords[nc++] = (float) (y + ctrls[i + 2] * h + ctrls[i + 3] * ah); 132 } 133 if (affine != null) { 134 affine.transform(coords, 0, coords, 0, nc / 2); 135 } 136 return types[index]; 137 } 138 139 157 public int currentSegment(double[] coords) { 158 if (isDone()) { 159 throw new NoSuchElementException("roundrect iterator out of bounds"); 160 } 161 double ctrls[] = ctrlpts[index]; 162 int nc = 0; 163 for (int i = 0; i < ctrls.length; i += 4) { 164 coords[nc++] = (x + ctrls[i + 0] * w + ctrls[i + 1] * aw); 165 coords[nc++] = (y + ctrls[i + 2] * h + ctrls[i + 3] * ah); 166 } 167 if (affine != null) { 168 affine.transform(coords, 0, coords, 0, nc / 2); 169 } 170 return types[index]; 171 } 172 } 173 | Popular Tags |