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.collections.ListChangeListener.Change;
029import javafx.collections.ObservableList;
030import javafx.scene.paint.Color;
031
032import com.sun.javafx.collections.TrackableObservableList;
033import javafx.css.CssMetaData;
034import com.sun.javafx.css.converters.PaintConverter;
035import com.sun.javafx.geom.BaseBounds;
036import com.sun.javafx.geom.Path2D;
037import com.sun.javafx.geom.transform.BaseTransform;
038import com.sun.javafx.scene.DirtyBits;
039import com.sun.javafx.sg.PGNode;
040import com.sun.javafx.sg.PGPolyline;
041import com.sun.javafx.sg.PGShape.Mode;
042import com.sun.javafx.tk.Toolkit;
043import java.util.ArrayList;
044import java.util.Collections;
045import java.util.List;
046import javafx.beans.value.WritableValue;
047import javafx.css.StyleableProperty;
048import javafx.scene.paint.Paint;
049
050/**
051 * Creates a polyline, defined by the array of the segment points. The Polyline
052 * class is similar to the Polygon class, except that it is not automatically
053 * closed.
054 *
055<PRE>
056import javafx.scene.shape.*;
057
058Polyline polyline = new Polyline();
059polyline.getPoints().addAll(new Double[]{
060    0.0, 0.0,
061    20.0, 10.0,
062    10.0, 20.0 });
063</PRE>
064 */
065public  class Polyline extends Shape {
066
067    private final Path2D shape = new Path2D();
068
069    {
070        // overriding default values for fill and stroke
071        // Set through CSS property so that it appears to be a UA style rather
072        // that a USER style so that fill and stroke can still be set from CSS.
073        ((StyleableProperty)fillProperty()).applyStyle(null, null);
074        ((StyleableProperty)strokeProperty()).applyStyle(null, Color.BLACK);
075    }
076
077    /**
078     * Creates an empty instance of Polyline.
079     */
080    public Polyline() {
081    }
082
083    /**
084     * Creates a new instance of Polyline.
085     * @param points the coordinates of the polyline segments
086     */
087    public Polyline(double... points) {
088        if (points != null) {
089            for (double p : points) {
090                this.getPoints().add(p);
091            }
092        }
093    }
094
095    /**
096     * Defines the coordinates of the polyline segments.
097     *
098     * @defaultValue empty
099     */
100    private final ObservableList<Double> points = new TrackableObservableList<Double>() {
101        @Override
102        protected void onChanged(Change<Double> c) {
103            impl_markDirty(DirtyBits.NODE_GEOMETRY);
104            impl_geomChanged();
105        }
106    };
107
108    /**
109     * Gets the coordinates of the {@code PolyLine} segments.
110     * @return An observable list of points constituting segments of this
111     * {@code PolyLine}
112     */
113    public final ObservableList<Double> getPoints() { return points; }
114
115    /**
116     * @treatAsPrivate implementation detail
117     * @deprecated This is an internal API that is not intended for use and will be removed in the next version
118     */
119    @Deprecated
120    protected PGNode impl_createPGNode() {
121        return Toolkit.getToolkit().createPGPolyline();
122    }
123
124    PGPolyline getPGPolyline() {
125        return (PGPolyline)impl_getPGNode();
126    }
127
128    /**
129     * @treatAsPrivate implementation detail
130     * @deprecated This is an internal API that is not intended for use and will be removed in the next version
131     */
132    @Deprecated
133    public BaseBounds impl_computeGeomBounds(BaseBounds bounds, BaseTransform tx) {
134        if (impl_mode == Mode.EMPTY || getPoints().size() <= 1) {
135            return bounds.makeEmpty();
136        }
137
138        if (getPoints().size() == 2) {
139            if (impl_mode == Mode.FILL || getStrokeType() == StrokeType.INSIDE) {
140                return bounds.makeEmpty();
141            }
142            double upad = getStrokeWidth();
143            if (getStrokeType() == StrokeType.CENTERED) {
144                upad /= 2.0f;
145            }
146            return computeBounds(bounds, tx, upad, 0.5f,
147                getPoints().get(0), getPoints().get(1), 0.0f, 0.0f);
148        } else {
149            return computeShapeBounds(bounds, tx, impl_configShape());
150        }
151    }
152
153    /**
154     * @treatAsPrivate implementation detail
155     * @deprecated This is an internal API that is not intended for use and will be removed in the next version
156     */
157    @Deprecated
158    @Override
159        public Path2D impl_configShape() {
160        double p1 = getPoints().get(0);
161        double p2 = getPoints().get(1);
162        shape.reset();
163        shape.moveTo((float)p1, (float)p2);
164        final int numValidPoints = getPoints().size() & ~1;
165        for (int i = 2; i < numValidPoints; i += 2) {
166            p1 = getPoints().get(i); p2 = getPoints().get(i+1);
167            shape.lineTo((float)p1, (float)p2);
168        }
169        return shape;
170    }
171
172    /**
173     * @treatAsPrivate implementation detail
174     * @deprecated This is an internal API that is not intended for use and will be removed in the next version
175     */
176    @Deprecated
177    public void impl_updatePG() {
178        super.impl_updatePG();
179
180        if (impl_isDirty(DirtyBits.NODE_GEOMETRY)) {
181            final int numValidPoints = getPoints().size() & ~1;
182            float points_array[] = new float[numValidPoints];
183            for (int i = 0; i < numValidPoints; i++) {
184                points_array[i] = (float)getPoints().get(i).doubleValue();
185            }
186            getPGPolyline().updatePolyline(points_array);
187        }
188    }
189
190    /***************************************************************************
191     *                                                                         *
192     *                         Stylesheet Handling                             *
193     *                                                                         *
194     **************************************************************************/
195
196    /** 
197     * Some sub-class of Shape, such as {@link Line}, override the
198     * default value for the {@link Shape#fill} property. This allows
199     * CSS to get the correct initial value.
200     * @treatAsPrivate Implementation detail
201     * @deprecated This is an internal API that is not intended for use and will be removed in the next version
202     */
203    @Deprecated
204    protected Paint impl_cssGetFillInitialValue() {
205        return null;
206    }    
207    
208    /** 
209     * Some sub-class of Shape, such as {@link Line}, override the
210     * default value for the {@link Shape#stroke} property. This allows
211     * CSS to get the correct initial value.
212     * @treatAsPrivate Implementation detail
213     * @deprecated This is an internal API that is not intended for use and will be removed in the next version
214     */
215    @Deprecated
216    protected Paint impl_cssGetStrokeInitialValue() {
217        return Color.BLACK;
218    }    
219    
220    /**
221     * Returns a string representation of this {@code Polyline} object.
222     * @return a string representation of this {@code Polyline} object.
223     */
224    @Override
225    public String toString() {
226        final StringBuilder sb = new StringBuilder("Polyline[");
227
228        String id = getId();
229        if (id != null) {
230            sb.append("id=").append(id).append(", ");
231        }
232
233        sb.append("points=").append(getPoints());
234
235        sb.append(", fill=").append(getFill());
236
237        Paint stroke = getStroke();
238        if (stroke != null) {
239            sb.append(", stroke=").append(stroke);
240            sb.append(", strokeWidth=").append(getStrokeWidth());
241        }
242
243        return sb.append("]").toString();
244    }
245}