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 BaseBounds doComputeGeomBounds(Node node, 75 BaseBounds bounds, BaseTransform tx) { 76 return ((Polygon) node).doComputeGeomBounds(bounds, tx); 77 } 78 79 @Override 80 public com.sun.javafx.geom.Shape doConfigShape(Shape shape) { 81 return ((Polygon) shape).doConfigShape(); 82 } 83 }); 84 } 85 86 private final Path2D shape = new Path2D(); 87 88 { 89 // To initialize the class helper at the begining each constructor of this class 90 PolygonHelper.initHelper(this); 91 } 92 93 /** 94 * Creates an empty instance of Polygon. 95 */ 96 public Polygon() { 97 } 98 99 /** 100 * Creates a new instance of Polygon. 101 * @param points the coordinates of the polygon vertices 102 */ 103 public Polygon(double... points) { 104 if (points != null) { 105 for (double p : points) { 106 this.getPoints().add(p); 107 } 108 } 109 } 110 111 /** 112 * Defines the coordinates of the polygon vertices. 113 * 114 * @defaultValue empty 115 */ 116 private final ObservableList<Double> points = new TrackableObservableList<Double>() { 117 @Override 118 protected void onChanged(Change<Double> c) { 119 NodeHelper.markDirty(Polygon.this, DirtyBits.NODE_GEOMETRY); 120 NodeHelper.geomChanged(Polygon.this); 121 } 122 }; 123 124 /** 125 * Gets the coordinates of the {@code Polygon} vertices. 126 * @return An observable list of vertices of this {@code Polygon} 127 */ 128 public final ObservableList<Double> getPoints() { return points; } 129 130 /* 131 * Note: This method MUST only be called via its accessor method. 132 */ 133 private NGNode doCreatePeer() { 134 return new NGPolygon(); 135 } 136 137 /* 138 * Note: This method MUST only be called via its accessor method. 139 */ 140 private BaseBounds doComputeGeomBounds(BaseBounds bounds, BaseTransform tx) { 141 if (getMode() == NGShape.Mode.EMPTY || getPoints().size() <= 1) { 142 return bounds.makeEmpty(); 143 } 144 145 if (getPoints().size() == 2) { 146 if (getMode() == NGShape.Mode.FILL || getStrokeType() == StrokeType.INSIDE) { 147 return bounds.makeEmpty(); 148 } 149 double upad = getStrokeWidth(); 150 if (getStrokeType() == StrokeType.CENTERED) { 151 upad /= 2.0f; 152 } 153 return computeBounds(bounds, tx, upad, 0.5f, 154 getPoints().get(0), getPoints().get(1), 0.0f, 0.0f); 155 } else { 156 return computeShapeBounds(bounds, tx, ShapeHelper.configShape(this)); 157 } 158 } 159 160 /* 161 * Note: This method MUST only be called via its accessor method. 162 */ 163 private Path2D doConfigShape() { 164 double p1 = getPoints().get(0); 165 double p2 = getPoints().get(1); 166 shape.reset(); 167 shape.moveTo((float)p1, (float)p2); 168 final int numValidPoints = getPoints().size() & ~1; 169 for (int i = 2; i < numValidPoints; i += 2) { 170 p1 = getPoints().get(i); p2 = getPoints().get(i+1); 171 shape.lineTo((float)p1, (float)p2); 172 } 173 shape.closePath(); 174 return shape; 175 } 176 177 /* 178 * Note: This method MUST only be called via its accessor method. 179 */ 180 private void doUpdatePeer() { 181 if (NodeHelper.isDirty(this, DirtyBits.NODE_GEOMETRY)) { 182 final int numValidPoints = getPoints().size() & ~1; 183 float points_array[] = new float[numValidPoints]; 184 for (int i = 0; i < numValidPoints; i++) { 185 points_array[i] = (float)getPoints().get(i).doubleValue(); 186 } 187 final NGPolygon peer = NodeHelper.getPeer(this); 188 peer.updatePolygon(points_array); 189 } 190 } 191 192 /** 193 * Returns a string representation of this {@code Polygon} object. 194 * @return a string representation of this {@code Polygon} object. 195 */ 196 @Override 197 public String toString() { 198 final StringBuilder sb = new StringBuilder("Polygon["); 199 200 String id = getId(); 201 if (id != null) { 202 sb.append("id=").append(id).append(", "); 203 } 204 205 sb.append("points=").append(getPoints()); 206 207 sb.append(", fill=").append(getFill()); 208 209 Paint stroke = getStroke(); 210 if (stroke != null) { 211 sb.append(", stroke=").append(stroke); 212 sb.append(", strokeWidth=").append(getStrokeWidth()); 213 } 214 215 return sb.append("]").toString(); 216 } 217 } 218