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 line path element by drawing a straight line
037 * from the current coordinate to the new coordinates.
038 *
039 * <p>For more information on path elements see the {@link Path} and
040 * {@link PathElement} classes.
041 *
042 * <p>Example:
043 *
044<PRE>
045import javafx.scene.shape.*;
046
047Path path = new Path();
048path.getElements().add(new MoveTo(0.0f, 50.0f));
049path.getElements().add(new LineTo(100.0f, 100.0f));
050</PRE>
051 */
052public class LineTo extends PathElement {
053
054    /**
055     * Creates an empty instance of LineTo.
056     */
057    public LineTo() {
058    }
059
060    /**
061     * Creates a new isntance of LineTo.
062     * @param x the horizontal coordinate of the line end point
063     * @param y the vertical coordinate of the line end point
064     */
065    public LineTo(double x, double y) {
066        setX(x);
067        setY(y);
068    }
069
070    /**
071     * Defines the X coordinate.
072     *
073     * @defaultValue 0.0
074     */
075    private DoubleProperty x;
076
077
078
079    public final void setX(double value) {
080        if (x != null || value != 0.0) {
081            xProperty().set(value);
082        }
083    }
084
085    public final double getX() {
086        return x == null ? 0.0 : x.get();
087    }
088
089    public final DoubleProperty xProperty() {
090        if (x == null) {
091            x = new DoublePropertyBase() {
092
093                @Override
094                public void invalidated() {
095                    u();
096                }
097
098                @Override
099                public Object getBean() {
100                    return LineTo.this;
101                }
102
103                @Override
104                public String getName() {
105                    return "x";
106                }
107            };
108        }
109        return x;
110    }
111
112    /**
113     * Defines the Y coordinate.
114     *
115     * @defaultValue 0.0
116     */
117    private DoubleProperty y;
118
119    public final void setY(double value) {
120        if (y != null || value != 0.0) {
121            yProperty().set(value);
122        }
123    }
124
125    public final double getY() {
126        return y == null ? 0.0 : y.get();
127    }
128
129    public final DoubleProperty yProperty() {
130        if (y == null) {
131            y = new DoublePropertyBase() {
132
133                @Override
134                public void invalidated() {
135                    u();
136                }
137
138                @Override
139                public Object getBean() {
140                    return LineTo.this;
141                }
142
143                @Override
144                public String getName() {
145                    return "y";
146                }
147            };
148        }
149        return y;
150    }
151
152    @Override
153    void addTo(PGPath pgPath) {
154        if (isAbsolute()) {
155            pgPath.addLineTo((float)getX(), (float)getY());
156        } else {
157            pgPath.addLineTo((float)(pgPath.getCurrentX() + getX()),
158                             (float)(pgPath.getCurrentY() + getY()));
159        }
160    }
161
162    /**
163     * @treatAsPrivate implementation detail
164     * @deprecated This is an internal API that is not intended for use and will be removed in the next version
165     */
166    @Deprecated
167    @Override
168    public void impl_addTo(Path2D path) {
169        if (isAbsolute()) {
170            path.lineTo((float)getX(), (float)getY());
171        } else {
172            path.lineTo((float)(path.getCurrentX() + getX()),
173                        (float)(path.getCurrentY() + getY()));
174        }
175    }
176
177    /**
178     * Returns a string representation of this {@code LineTo} object.
179     * @return a string representation of this {@code LineTo} object.
180     */
181    @Override
182    public String toString() {
183        final StringBuilder sb = new StringBuilder("LineTo[");
184        sb.append("x=").append(getX());
185        sb.append(", y=").append(getY());
186        return sb.append("]").toString();
187    }
188}
189