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
028
029import javafx.beans.property.DoubleProperty;
030import javafx.beans.property.DoublePropertyBase;
031
032import com.sun.javafx.geom.Path2D;
033import com.sun.javafx.sg.PGPath;
034
035
036/**
037 * Creates a curved path element, defined by two new points,
038 * by drawing a Quadratic Bézier curve that intersects both the current coordinates
039 * and the specified coordinates {@code (x, y)},
040 * using the specified point {@code (controlX, controlY)}
041 * as a Bézier control point.
042 * All coordinates are specified in double precision.
043 *
044 * <p>For more information on path elements see the {@link Path} and
045 * {@link PathElement} classes.
046 *
047 * <p>Example:
048 *
049<PRE>
050import javafx.scene.shape.*;
051
052Path path = new Path();
053
054MoveTo moveTo = new MoveTo();
055moveTo.setX(0.0f);
056moveTo.setY(50.0f);
057
058QuadCurveTo quadTo = new QuadCurveTo();
059quadTo.setControlX(25.0f);
060quadTo.setControlY(0.0f);
061quadTo.setX(50.0f);
062quadTo.setY(50.0f);
063
064path.getElements().add(moveTo);
065path.getElements().add(cubicTo);
066</PRE>
067 */
068public  class QuadCurveTo extends PathElement {
069
070    /**
071     * Creates an empty instance of QuadCurveTo.
072     */
073    public QuadCurveTo() {
074    }
075
076    /**
077     * Creates a new instance of QuadCurveTo.
078     * @param controlX the X coordinate of the quadratic control point
079     * @param controlY the Y coordinate of the quadratic control point
080     * @param x the X coordinate of the final end point
081     * @param y the Y coordinate of the final end point
082     */
083    public QuadCurveTo(double controlX, double controlY, double x, double y) {
084        setControlX(controlX);
085        setControlY(controlY);
086        setX(x);
087        setY(y);
088    }
089
090    /**
091     * Defines the X coordinate of the quadratic control point.
092     *
093     * @defaultValue 0.0
094     */
095    private DoubleProperty controlX = new DoublePropertyBase() {
096
097        @Override
098        public void invalidated() {
099            u();
100        }
101
102        @Override
103        public Object getBean() {
104            return QuadCurveTo.this;
105        }
106
107        @Override
108        public String getName() {
109            return "controlX";
110        }
111    };
112
113
114    public final void setControlX(double value) {
115        controlX.set(value);
116    }
117
118    public final double getControlX() {
119        return controlX.get();
120    }
121
122    public final DoubleProperty controlXProperty() {
123        return controlX;
124    }
125
126    /**
127     * Defines the Y coordinate of the quadratic control point.
128     *
129     * @defaultValue 0.0
130     */
131    private DoubleProperty controlY = new DoublePropertyBase() {
132
133        @Override
134        public void invalidated() {
135            u();
136        }
137
138        @Override
139        public Object getBean() {
140            return QuadCurveTo.this;
141        }
142
143        @Override
144        public String getName() {
145            return "controlY";
146        }
147    };
148
149
150    public final void setControlY(double value) {
151        controlY.set(value);
152    }
153
154    public final double getControlY() {
155        return controlY.get();
156    }
157
158    public final DoubleProperty controlYProperty() {
159        return controlY;
160    }
161
162    /**
163     * Defines the X coordinate of the final end point.
164     *
165     * @defaultValue 0.0
166     */
167    private DoubleProperty x;
168
169    public final void setX(double value) {
170        if (x != null || value != 0.0) {
171            xProperty().set(value);
172        }
173    }
174
175    public final double getX() {
176        return x == null ? 0.0 : x.get();
177    }
178
179    public final DoubleProperty xProperty() {
180        if (x == null) {
181            x = new DoublePropertyBase() {
182
183                @Override
184                public void invalidated() {
185                    u();
186                }
187
188                @Override
189                public Object getBean() {
190                    return QuadCurveTo.this;
191                }
192
193                @Override
194                public String getName() {
195                    return "x";
196                }
197            };
198        }
199        return x;
200    }
201    
202    /**
203     * Defines the Y coordinate of the final end point.
204     *
205     * @defaultValue 0.0
206     */
207    private DoubleProperty y;
208
209    public final void setY(double value) {
210        if (y != null || value != 0.0) {
211            yProperty().set(value);
212        }
213    }
214
215    public final double getY() {
216        return y == null ? 0.0 : y.get();
217    }
218
219    public final DoubleProperty yProperty() {
220        if (y == null) {
221            y = new DoublePropertyBase() {
222
223                @Override
224                public void invalidated() {
225                    u();
226                }
227
228                @Override
229                public Object getBean() {
230                    return QuadCurveTo.this;
231                }
232
233                @Override
234                public String getName() {
235                    return "y";
236                }
237            };
238        }
239        return y;
240    }
241
242    @Override
243    void addTo(PGPath pgPath) {
244        if (isAbsolute()) {
245            pgPath.addQuadTo(
246                    (float)getControlX(),
247                    (float)getControlY(),
248                    (float)getX(),
249                    (float)getY());
250        } else {
251            final double dx = pgPath.getCurrentX();
252            final double dy = pgPath.getCurrentY();
253            pgPath.addQuadTo(
254                    (float)(getControlX()+dx),
255                    (float)(getControlY()+dy),
256                    (float)(getX()+dx),
257                    (float)(getY()+dy));
258        }
259    }
260
261    /**
262     * @treatAsPrivate implementation detail
263     * @deprecated This is an internal API that is not intended for use and will be removed in the next version
264     */
265    @Deprecated
266    @Override
267    public void impl_addTo(Path2D path) {
268        if (isAbsolute()) {
269            path.quadTo(
270                    (float)getControlX(),
271                    (float)getControlY(),
272                    (float)getX(),
273                    (float)getY());
274        } else {
275            final double dx = path.getCurrentX();
276            final double dy = path.getCurrentY();
277            path.quadTo(
278                    (float)(getControlX()+dx),
279                    (float)(getControlY()+dy),
280                    (float)(getX()+dx),
281                    (float)(getY()+dy));
282        }
283    }
284
285    /**
286     * Returns a string representation of this {@code CubicCurveTo} object.
287     * @return a string representation of this {@code CubicCurveTo} object.
288     */
289    @Override
290    public String toString() {
291        final StringBuilder sb = new StringBuilder("CubicCurveTo[");
292
293        sb.append("x=").append(getX());
294        sb.append(", y=").append(getY());
295        sb.append(", controlX=").append(getControlX());
296        sb.append(", controlY=").append(getControlY());
297
298        return sb.append("]").toString();
299    }
300}
301