1 /*
   2  * Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javafx.scene.shape;
  27 
  28 import com.sun.javafx.collections.TrackableObservableList;
  29 import com.sun.javafx.geom.BaseBounds;
  30 import com.sun.javafx.geom.Path2D;
  31 import com.sun.javafx.geom.transform.BaseTransform;
  32 import com.sun.javafx.scene.DirtyBits;
  33 import com.sun.javafx.scene.NodeHelper;
  34 import com.sun.javafx.scene.shape.PolygonHelper;
  35 import com.sun.javafx.scene.shape.ShapeHelper;
  36 import com.sun.javafx.sg.prism.NGNode;
  37 import com.sun.javafx.sg.prism.NGPolygon;
  38 import com.sun.javafx.sg.prism.NGShape;
  39 import javafx.collections.ListChangeListener.Change;
  40 import javafx.collections.ObservableList;
  41 import javafx.scene.Node;
  42 import javafx.scene.paint.Paint;
  43 
  44 /**
  45  * Creates a polygon, defined by an array of x,y coordinates. The Polygon
  46  * class is similar to the Polyline class, except that the Polyline class
  47  * is not automatically closed.
  48  *
  49 <PRE>
  50 import javafx.scene.shape.*;
  51 
  52 Polygon polygon = new Polygon();
  53 polygon.getPoints().addAll(new Double[]{
  54     0.0, 0.0,
  55     20.0, 10.0,
  56     10.0, 20.0 });
  57 </PRE>
  58  * @since JavaFX 2.0
  59  */
  60 public  class Polygon extends Shape {
  61     static {
  62         PolygonHelper.setPolygonAccessor(new PolygonHelper.PolygonAccessor() {
  63             @Override
  64             public NGNode doCreatePeer(Node node) {
  65                 return ((Polygon) node).doCreatePeer();
  66             }
  67 
  68             @Override
  69             public void doUpdatePeer(Node node) {
  70                 ((Polygon) node).doUpdatePeer();
  71             }
  72 
  73             @Override
  74             public com.sun.javafx.geom.Shape doConfigShape(Shape shape) {
  75                 return ((Polygon) shape).doConfigShape();
  76             }
  77         });
  78     }
  79 
  80     private final Path2D shape = new Path2D();
  81 
  82     {
  83         // To initialize the class helper at the begining each constructor of this class
  84         PolygonHelper.initHelper(this);
  85     }
  86 
  87     /**
  88      * Creates an empty instance of Polygon.
  89      */
  90     public Polygon() {
  91     }
  92 
  93     /**
  94      * Creates a new instance of Polygon.
  95      * @param points the coordinates of the polygon vertices
  96      */
  97     public Polygon(double... points) {
  98         if (points != null) {
  99             for (double p : points) {
 100                 this.getPoints().add(p);
 101             }
 102         }
 103     }
 104 
 105     /**
 106      * Defines the coordinates of the polygon vertices.
 107      *
 108      * @defaultValue empty
 109      */
 110     private final ObservableList<Double> points = new TrackableObservableList<Double>() {
 111         @Override
 112         protected void onChanged(Change<Double> c) {
 113             NodeHelper.markDirty(Polygon.this, DirtyBits.NODE_GEOMETRY);
 114             impl_geomChanged();
 115         }
 116     };
 117 
 118     /**
 119      * Gets the coordinates of the {@code Polygon} vertices.
 120      * @return An observable list of vertices of this {@code Polygon}
 121      */
 122     public final ObservableList<Double> getPoints() { return points; }
 123 
 124     /*
 125      * Note: This method MUST only be called via its accessor method.
 126      */
 127     private NGNode doCreatePeer() {
 128         return new NGPolygon();
 129     }
 130 
 131     /**
 132      * @treatAsPrivate implementation detail
 133      * @deprecated This is an internal API that is not intended for use and will be removed in the next version
 134      */
 135     @Deprecated
 136     public BaseBounds impl_computeGeomBounds(BaseBounds bounds, BaseTransform tx) {
 137         if (getMode() == NGShape.Mode.EMPTY || getPoints().size() <= 1) {
 138             return bounds.makeEmpty();
 139         }
 140 
 141         if (getPoints().size() == 2) {
 142             if (getMode() == NGShape.Mode.FILL || getStrokeType() == StrokeType.INSIDE) {
 143                 return bounds.makeEmpty();
 144             }
 145             double upad = getStrokeWidth();
 146             if (getStrokeType() == StrokeType.CENTERED) {
 147                 upad /= 2.0f;
 148             }
 149             return computeBounds(bounds, tx, upad, 0.5f,
 150                 getPoints().get(0), getPoints().get(1), 0.0f, 0.0f);
 151         } else {
 152             return computeShapeBounds(bounds, tx, ShapeHelper.configShape(this));
 153         }
 154     }
 155 
 156     /*
 157      * Note: This method MUST only be called via its accessor method.
 158      */
 159     private Path2D doConfigShape() {
 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         shape.closePath();
 170         return shape;
 171     }
 172 
 173     /*
 174      * Note: This method MUST only be called via its accessor method.
 175      */
 176     private void doUpdatePeer() {
 177         if (NodeHelper.isDirty(this, DirtyBits.NODE_GEOMETRY)) {
 178             final int numValidPoints = getPoints().size() & ~1;
 179             float points_array[] = new float[numValidPoints];
 180             for (int i = 0; i < numValidPoints; i++) {
 181                 points_array[i] = (float)getPoints().get(i).doubleValue();
 182             }
 183             final NGPolygon peer = NodeHelper.getPeer(this);
 184             peer.updatePolygon(points_array);
 185         }
 186     }
 187 
 188     /**
 189      * Returns a string representation of this {@code Polygon} object.
 190      * @return a string representation of this {@code Polygon} object.
 191      */
 192     @Override
 193     public String toString() {
 194         final StringBuilder sb = new StringBuilder("Polygon[");
 195 
 196         String id = getId();
 197         if (id != null) {
 198             sb.append("id=").append(id).append(", ");
 199         }
 200 
 201         sb.append("points=").append(getPoints());
 202 
 203         sb.append(", fill=").append(getFill());
 204 
 205         Paint stroke = getStroke();
 206         if (stroke != null) {
 207             sb.append(", stroke=").append(stroke);
 208             sb.append(", strokeWidth=").append(getStrokeWidth());
 209         }
 210 
 211         return sb.append("]").toString();
 212     }
 213 }
 214