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;
030
031import com.sun.javafx.collections.TrackableObservableList;
032import com.sun.javafx.geom.BaseBounds;
033import com.sun.javafx.geom.Path2D;
034import com.sun.javafx.geom.transform.BaseTransform;
035import com.sun.javafx.scene.DirtyBits;
036import com.sun.javafx.sg.PGNode;
037import com.sun.javafx.sg.PGPolygon;
038import com.sun.javafx.sg.PGShape.Mode;
039import com.sun.javafx.tk.Toolkit;
040import javafx.scene.paint.Paint;
041
042/**
043 * Creates a polygon, defined by an array of x,y coordinates. The Polygon
044 * class is similar to the Polyline class, except that the Polyline class
045 * is not automatically closed.
046 *
047<PRE>
048import javafx.scene.shape.*;
049
050Polygon polygon = new Polygon();
051polygon.getPoints().addAll(new Double[]{
052    0.0, 0.0,
053    20.0, 10.0,
054    10.0, 20.0 });
055</PRE>
056 */
057public  class Polygon extends Shape {
058
059    private final Path2D shape = new Path2D();
060
061    /**
062     * Creates an empty instance of Polygon.
063     */
064    public Polygon() {
065    }
066
067    /**
068     * Creates a new instance of Polygon.
069     * @param points the coordinates of the polygon vertices
070     */
071    public Polygon(double... points) {
072        if (points != null) {
073            for (double p : points) {
074                this.getPoints().add(p);
075            }
076        }
077    }
078
079    /**
080     * Defines the coordinates of the polygon vertices.
081     *
082     * @defaultValue empty
083     */
084    private final ObservableList<Double> points = new TrackableObservableList<Double>() {
085        @Override
086        protected void onChanged(Change<Double> c) {
087            impl_markDirty(DirtyBits.NODE_GEOMETRY);
088            impl_geomChanged();
089        }
090    };
091
092    /**
093     * Gets the coordinates of the {@code Polygon} vertices.
094     * @return An observable list of vertices of this {@code Polygon}
095     */
096    public final ObservableList<Double> getPoints() { return points; }
097
098    /**
099     * @treatAsPrivate implementation detail
100     * @deprecated This is an internal API that is not intended for use and will be removed in the next version
101     */
102    @Deprecated
103    protected PGNode impl_createPGNode() {
104        return Toolkit.getToolkit().createPGPolygon();
105    }
106
107    PGPolygon getPGPolygon() {
108        return (PGPolygon)impl_getPGNode();
109    }
110
111    /**
112     * @treatAsPrivate implementation detail
113     * @deprecated This is an internal API that is not intended for use and will be removed in the next version
114     */
115    @Deprecated
116    public BaseBounds impl_computeGeomBounds(BaseBounds bounds, BaseTransform tx) {
117        if (impl_mode == Mode.EMPTY || getPoints().size() <= 1) {
118            return bounds.makeEmpty();
119        }
120
121        if (getPoints().size() == 2) {
122            if (impl_mode == Mode.FILL || getStrokeType() == StrokeType.INSIDE) {
123                return bounds.makeEmpty();
124            }
125            double upad = getStrokeWidth();
126            if (getStrokeType() == StrokeType.CENTERED) {
127                upad /= 2.0f;
128            }
129            return computeBounds(bounds, tx, upad, 0.5f,
130                getPoints().get(0), getPoints().get(1), 0.0f, 0.0f);
131        } else {
132            return computeShapeBounds(bounds, tx, impl_configShape());
133        }
134    }
135
136    /**
137     * @treatAsPrivate implementation detail
138     * @deprecated This is an internal API that is not intended for use and will be removed in the next version
139     */
140    @Deprecated
141    @Override
142        public Path2D impl_configShape() {
143        double p1 = getPoints().get(0);
144        double p2 = getPoints().get(1);
145        shape.reset();
146        shape.moveTo((float)p1, (float)p2);
147        final int numValidPoints = getPoints().size() & ~1;
148        for (int i = 2; i < numValidPoints; i += 2) {
149            p1 = getPoints().get(i); p2 = getPoints().get(i+1);
150            shape.lineTo((float)p1, (float)p2);
151        }
152        shape.closePath();
153        return shape;
154    }
155
156    /**
157     * @treatAsPrivate implementation detail
158     * @deprecated This is an internal API that is not intended for use and will be removed in the next version
159     */
160    @Deprecated
161    public void impl_updatePG() {
162        super.impl_updatePG();
163        if (impl_isDirty(DirtyBits.NODE_GEOMETRY)) {
164            final int numValidPoints = getPoints().size() & ~1;
165            float points_array[] = new float[numValidPoints];
166            for (int i = 0; i < numValidPoints; i++) {
167                points_array[i] = (float)getPoints().get(i).doubleValue();
168            }
169            getPGPolygon().updatePolygon(points_array);
170        }
171    }
172
173    /**
174     * Returns a string representation of this {@code Polygon} object.
175     * @return a string representation of this {@code Polygon} object.
176     */
177    @Override
178    public String toString() {
179        final StringBuilder sb = new StringBuilder("Polygon[");
180
181        String id = getId();
182        if (id != null) {
183            sb.append("id=").append(id).append(", ");
184        }
185
186        sb.append("points=").append(getPoints());
187
188        sb.append(", fill=").append(getFill());
189
190        Paint stroke = getStroke();
191        if (stroke != null) {
192            sb.append(", stroke=").append(stroke);
193            sb.append(", strokeWidth=").append(getStrokeWidth());
194        }
195
196        return sb.append("]").toString();
197    }
198}
199