1 26 27 package org.nightlabs.editor2d.j2d; 28 29 import java.awt.geom.PathIterator ; 30 import java.awt.geom.Point2D ; 31 32 33 public class PathSegment 34 { 35 public static final int NO_POINT = 0; 36 public static final int POINT = 1; 37 public static final int CONTROL_POINT_1 = 2; 38 public static final int CONTROL_POINT_2 = 3; 39 40 48 public PathSegment(int type, int index, float[] coords, GeneralShape parent) 49 { 50 if (coords.length != 6) 51 throw new IllegalArgumentException ("Param coords must have length 6!"); 52 53 setType(type); 54 setIndex(index); 55 setCoords(coords); 56 this.parent = parent; 57 } 58 59 protected int type; 60 public int getType() { 61 return type; 62 } 63 protected void setType(int type) { 64 this.type = type; 65 } 66 67 protected int index; 68 public int getIndex() { 69 return index; 70 } 71 protected void setIndex(int index) { 72 this.index = index; 73 } 74 75 public float[] coords = new float[6]; 76 public float[] getCoords() { 77 return coords; 78 } 79 protected void setCoords(float[] coords) { 80 System.arraycopy(coords, 0, this.coords, 0, coords.length); 82 } 83 84 public GeneralShape parent; 85 public GeneralShape getParent() { 86 return parent; 87 } 88 protected void setParent(GeneralShape parent) { 89 this.parent = parent; 90 } 91 92 public boolean isLineSegement() { 93 return type == PathIterator.SEG_LINETO ? true : false; 94 } 95 96 public boolean isQuadSegment() { 97 return type == PathIterator.SEG_QUADTO ? true : false; 98 } 99 100 public boolean isCubicSegment() { 101 return type == PathIterator.SEG_CUBICTO ? true : false; 102 } 103 104 public boolean isStartSegment() { 105 return type == PathIterator.SEG_MOVETO ? true : false; 106 } 107 108 public Point2D getPoint() { 109 return new Point2D.Float (coords[0], coords[1]); 110 } 111 112 public Point2D getFirstControlPoint() 113 { 114 if (isQuadSegment() || isCubicSegment()) 115 return new Point2D.Float (coords[2], coords[3]); 116 else 117 return new Point2D.Float (); 118 } 119 120 public Point2D getSecondControlPoint() 121 { 122 if (isCubicSegment()) 123 return new Point2D.Float (coords[4], coords[5]); 124 else 125 return new Point2D.Float (); 126 } 127 128 public void setPoint(float x, float y) 129 { 130 float[] pointCoords = parent.getPointCoords(); 131 pointCoords[index] = x; 132 pointCoords[index+1] = y; 133 } 136 137 public void setFirstControlPoint(float x, float y) 138 { 139 float[] pointCoords = parent.getPointCoords(); 140 pointCoords[index+2] = x; 141 pointCoords[index+3] = y; 142 } 145 146 public void setSecondControlPoint(float x, float y) 147 { 148 float[] pointCoords = parent.getPointCoords(); 149 pointCoords[index+4] = x; 150 pointCoords[index+5] = y; 151 } 154 155 public int contains(int x, int y) 156 { 157 if (isLineSegement()) 158 { 159 if (coords[0] == x && coords[1] == y) 160 return POINT; 161 162 return NO_POINT; 163 } 164 else if (isQuadSegment()) 165 { 166 if (coords[0] == x && coords[1] == y) 167 return POINT; 168 else if (coords[2] == x && coords[3] == y) 169 return CONTROL_POINT_1; 170 171 return NO_POINT; 172 } 173 else if (isCubicSegment()) 174 { 175 if (coords[0] == x && coords[1] == y) 176 return POINT; 177 else if (coords[2] == x && coords[3] == y) 178 return CONTROL_POINT_1; 179 else if (coords[4] == x && coords[5] == y) 180 return CONTROL_POINT_2; 181 182 return NO_POINT; 183 } 184 return NO_POINT; 185 } 186 } 187 | Popular Tags |