Spec-Zone .ru
спецификации, руководства, описания, API
001/*
002 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.  Oracle designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Oracle in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
022 * or visit www.oracle.com if you need additional information or have any
023 * questions.
024 */
025
026package javafx.scene.shape;
027
028import javafx.beans.property.DoubleProperty;
029import javafx.beans.property.DoublePropertyBase;
030
031import com.sun.javafx.geom.Path2D;
032import com.sun.javafx.sg.PGPath;
033
034
035/**
036 * Creates a curved path element, defined by three new points,
037 * by drawing a Cubic Bézier curve that intersects both the current coordinates
038 * and the specified coordinates {@code (x,y)}, using the
039 * specified points {@code (controlX1,controlY1)} and {@code (controlX2,controlY2)}
040 * as Bézier control points. All coordinates are specified in double precision.
041 *
042 * <p>For more information on path elements see the {@link Path} and
043 * {@link PathElement} classes.
044 *
045 * <p>Example:
046 *
047<PRE>
048import javafx.scene.shape.*;
049
050Path path = new Path();
051
052MoveTo moveTo = new MoveTo();
053moveTo.setX(0.0f);
054moveTo.setY(0.0f);
055
056CubicCurveTo cubicTo = new CubicCurveTo();
057cubicTo.setControlX1(0.0f);
058cubicTo.setControlY1(0.0f);
059cubicTo.setControlX2(100.0f);
060cubicTo.setControlY2(100.0f);
061cubicTo.setX(100.0f);
062cubicTo.setY(50.0f);
063
064path.getElements().add(moveTo);
065path.getElements().add(cubicTo);
066</PRE>
067 */
068public class CubicCurveTo extends PathElement {
069
070    /**
071     * Creates an empty instance of CubicCurveTo.
072     */
073    public CubicCurveTo() {
074    }
075
076    /**
077     * Creates a new instance of CubicCurveTo.
078     * @param controlX1 the X coordinate of the first B&eacute;zier control point
079     * @param controlY1 the Y coordinate of the first B&eacute;zier control point
080     * @param controlX2 the X coordinate of the second B&eacute;zier control point
081     * @param controlY2 the Y coordinate of the second B&eacute;zier control point
082     * @param x the X coordinate of the final end point
083     * @param y the Y coordinate of the final end point
084     */
085    public CubicCurveTo(double controlX1, double controlY1, double controlX2,
086        double controlY2, double x, double y)
087    {
088        setControlX1(controlX1);
089        setControlY1(controlY1);
090        setControlX2(controlX2);
091        setControlY2(controlY2);
092        setX(x);
093        setY(y);
094    }
095
096    /**
097     * Defines the X coordinate of the first B&eacute;zier control point.
098     *
099     * @defaultValue 0.0
100     */
101    private DoubleProperty controlX1;
102
103
104    public final void setControlX1(double value) {
105        if (controlX1 != null || value != 0.0) {
106            controlX1Property().set(value);
107        }
108    }
109
110    public final double getControlX1() {
111        return controlX1 == null ? 0.0 : controlX1.get();
112    }
113
114    public final DoubleProperty controlX1Property() {
115        if (controlX1 == null) {
116            controlX1 = new DoublePropertyBase() {
117
118                @Override
119                public void invalidated() {
120                    u();
121                }
122
123                @Override
124                public Object getBean() {
125                    return CubicCurveTo.this;
126                }
127
128                @Override
129                public String getName() {
130                    return "controlX1";
131                }
132            };
133        }
134        return controlX1;
135    }
136
137    /**
138     * Defines the Y coordinate of the first B&eacute;zier control point.
139     *
140     * @defaultValue 0.0
141     */
142    private DoubleProperty controlY1;
143
144
145    public final void setControlY1(double value) {
146        if (controlY1 != null || value != 0.0) {
147            controlY1Property().set(value);
148        }
149    }
150
151    public final double getControlY1() {
152        return controlY1 == null ? 0.0 : controlY1.get();
153    }
154
155    public final DoubleProperty controlY1Property() {
156        if (controlY1 == null) {
157            controlY1 = new DoublePropertyBase() {
158
159                @Override
160                public void invalidated() {
161                    u();
162                }
163
164                @Override
165                public Object getBean() {
166                    return CubicCurveTo.this;
167                }
168
169                @Override
170                public String getName() {
171                    return "controlY1";
172                }
173            };
174        }
175        return controlY1;
176    }
177
178    /**
179     * Defines the X coordinate of the second B&eacute;zier control point.
180     *
181     * @defaultValue 0.0
182     */
183    private DoubleProperty controlX2;
184
185
186    public final void setControlX2(double value) {
187        if (controlX2 != null || value != 0.0) {
188            controlX2Property().set(value);
189        }
190    }
191
192    public final double getControlX2() {
193        return controlX2 == null ? 0.0 : controlX2.get();
194    }
195
196    public final DoubleProperty controlX2Property() {
197        if (controlX2 == null) {
198            controlX2 = new DoublePropertyBase() {
199
200                @Override
201                public void invalidated() {
202                    u();
203                }
204
205                @Override
206                public Object getBean() {
207                    return CubicCurveTo.this;
208                }
209
210                @Override
211                public String getName() {
212                    return "controlX2";
213                }
214            };
215        }
216        return controlX2;
217    }
218
219    /**
220     * Defines the Y coordinate of the second B&eacute;zier control point.
221     *
222     * @defaultValue 0.0
223     */
224    private DoubleProperty controlY2;
225
226
227    public final void setControlY2(double value) {
228        if (controlY2 != null || value != 0.0) {
229            controlY2Property().set(value);
230        }
231    }
232
233    public final double getControlY2() {
234        return controlY2 == null ? 0.0 : controlY2.get();
235    }
236
237    public final DoubleProperty controlY2Property() {
238        if (controlY2 == null) {
239            controlY2 = new DoublePropertyBase() {
240
241                @Override
242                public void invalidated() {
243                    u();
244                }
245
246                @Override
247                public Object getBean() {
248                    return CubicCurveTo.this;
249                }
250
251                @Override
252                public String getName() {
253                    return "controlY2";
254                }
255            };
256        }
257        return controlY2;
258    }
259
260    /**
261     * Defines the X coordinate of the final end point.
262     *
263     * @defaultValue 0.0
264     */
265    private DoubleProperty x;
266
267    public final void setX(double value) {
268        if (x != null || value != 0.0) {
269            xProperty().set(value);
270        }
271    }
272
273    public final double getX() {
274        return x == null ? 0.0 : x.get();
275    }
276
277    public final DoubleProperty xProperty() {
278        if (x == null) {
279            x = new DoublePropertyBase() {
280
281                @Override
282                public void invalidated() {
283                    u();
284                }
285
286                @Override
287                public Object getBean() {
288                    return CubicCurveTo.this;
289                }
290
291                @Override
292                public String getName() {
293                    return "x";
294                }
295            };
296        }
297        return x;
298    }
299
300    /**
301     * Defines the Y coordinate of the final end point.
302     *
303     * @defaultValue 0.0
304     */
305    private DoubleProperty y;
306
307
308    public final void setY(double value) {
309        if (y != null || value != 0.0) {
310            yProperty().set(value);
311        }
312    }
313
314    public final double getY() {
315        return y == null ? 0.0 : y.get();
316    }
317
318    public final DoubleProperty yProperty() {
319        if (y == null) {
320            y = new DoublePropertyBase() {
321
322                @Override
323                public void invalidated() {
324                    u();
325                }
326
327                @Override
328                public Object getBean() {
329                    return CubicCurveTo.this;
330                }
331
332                @Override
333                public String getName() {
334                    return "y";
335                }
336            };
337        }
338        return y;
339    }
340
341    /**
342     * Adds the curved path element to the specified path.
343     */
344    @Override
345    void addTo(PGPath pgPath) {
346        if (isAbsolute()) {
347            pgPath.addCubicTo((float)getControlX1(), (float)getControlY1(),
348                              (float)getControlX2(), (float)getControlY2(),
349                              (float)getX(), (float)getY());
350        } else {
351            final double dx = pgPath.getCurrentX();
352            final double dy = pgPath.getCurrentY();
353            pgPath.addCubicTo((float)(getControlX1()+dx), (float)(getControlY1()+dy),
354                              (float)(getControlX2()+dx), (float)(getControlY2()+dy),
355                              (float)(getX()+dx), (float)(getY()+dy));
356        }
357    }
358
359    /**
360     * @treatAsPrivate implementation detail
361     * @deprecated This is an internal API that is not intended for use and will be removed in the next version
362     */
363    @Deprecated
364    @Override public void impl_addTo(Path2D path) {
365        if (isAbsolute()) {
366            path.curveTo((float)getControlX1(), (float)getControlY1(),
367                         (float)getControlX2(), (float)getControlY2(),
368                         (float)getX(), (float)getY());
369        } else {
370            final double dx = path.getCurrentX();
371            final double dy = path.getCurrentY();
372            path.curveTo((float)(getControlX1()+dx), (float)(getControlY1()+dy),
373                         (float)(getControlX2()+dx), (float)(getControlY2()+dy),
374                         (float)(getX()+dx), (float)(getY()+dy));
375        }
376    }
377
378    /**
379     * Returns a string representation of this {@code CubicCurveTo} object.
380     * @return a string representation of this {@code CubicCurveTo} object.
381     */
382    @Override
383    public String toString() {
384        final StringBuilder sb = new StringBuilder("CubicCurveTo[");
385
386        sb.append("x=").append(getX());
387        sb.append(", y=").append(getY());
388        sb.append(", controlX1=").append(getControlX1());
389        sb.append(", controlY1=").append(getControlY1());
390        sb.append(", controlX2=").append(getControlX2());
391        sb.append(", controlY2=").append(getControlY2());
392
393        return sb.append("]").toString();
394    }
395}
396