KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* *****************************************************************************
2  * NightLabs Editor2D - Graphical editor framework *
3  * Copyright (C) 2004-2005 NightLabs - http://NightLabs.org *
4  * *
5  * This library is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or (at your option) any later version. *
9  * *
10  * This library is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
13  * Lesser General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU Lesser General Public *
16  * License along with this library; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 51 Franklin St, Fifth Floor, *
19  * Boston, MA 02110-1301 USA *
20  * *
21  * Or get it online : *
22  * http://www.gnu.org/copyleft/lesser.html *
23  * *
24  * *
25  ******************************************************************************/

26
27 package org.nightlabs.editor2d.j2d;
28
29 import java.awt.geom.PathIterator JavaDoc;
30 import java.awt.geom.Point2D JavaDoc;
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   /**
41    * Represents a PathSegment of a GeneralShape
42    *
43    * param type the type of PathSegment (@see java.awt.geom.PathIterator)
44    * param index the index of this PathSegment
45    * param coords the Coordinates of the PathSegment
46    *
47    */

48   public PathSegment(int type, int index, float[] coords, GeneralShape parent)
49   {
50     if (coords.length != 6)
51       throw new IllegalArgumentException JavaDoc("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 // this.coords = coords;
81
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 JavaDoc getPoint() {
109     return new Point2D.Float JavaDoc(coords[0], coords[1]);
110   }
111   
112   public Point2D JavaDoc getFirstControlPoint()
113   {
114     if (isQuadSegment() || isCubicSegment())
115       return new Point2D.Float JavaDoc(coords[2], coords[3]);
116     else
117       return new Point2D.Float JavaDoc();
118   }
119
120   public Point2D JavaDoc getSecondControlPoint()
121   {
122     if (isCubicSegment())
123       return new Point2D.Float JavaDoc(coords[4], coords[5]);
124     else
125       return new Point2D.Float JavaDoc();
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 // coords[0] = x;
134
// coords[1] = y;
135
}
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 // coords[2] = x;
143
// coords[3] = y;
144
}
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 // coords[4] = x;
152
// coords[5] = y;
153
}
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