KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > editor2d > j2d > PathSegment


1 /**
2  * <p> Project: com.nightlabs.gui </p>
3  * <p> Copyright: Copyright (c) 2004 </p>
4  * <p> Company: NightLabs GmbH (Germany) </p>
5  * <p> Creation Date: 19.01.2005 </p>
6  * <p> Author: Daniel Mazurek </p>
7 **/

8 package com.nightlabs.editor2d.j2d;
9
10 import java.awt.geom.PathIterator JavaDoc;
11 import java.awt.geom.Point2D JavaDoc;
12
13
14 public class PathSegment
15 {
16   public static final int NO_POINT = 0;
17   public static final int POINT = 1;
18   public static final int CONTROL_POINT_1 = 2;
19   public static final int CONTROL_POINT_2 = 3;
20   
21   /**
22    * Represents a PathSegment of a GeneralShape
23    *
24    * param type the type of PathSegment (@see java.awt.geom.PathIterator)
25    * param index the index of this PathSegment
26    * param coords the Coordinates of the PathSegment
27    *
28    */

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