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.PolylineHelper; 35 import com.sun.javafx.scene.shape.ShapeHelper; 36 import com.sun.javafx.sg.prism.NGNode; 37 import com.sun.javafx.sg.prism.NGPolyline; 38 import com.sun.javafx.sg.prism.NGShape; 39 import javafx.collections.ListChangeListener.Change; 40 import javafx.collections.ObservableList; 41 import javafx.css.StyleableProperty; 42 import javafx.scene.Node; 43 import javafx.scene.paint.Color; 44 import javafx.scene.paint.Paint; 45 46 /** 47 * Creates a polyline, defined by the array of the segment points. The Polyline 48 * class is similar to the Polygon class, except that it is not automatically 49 * closed. 50 * 51 <PRE> 52 import javafx.scene.shape.*; 53 54 Polyline polyline = new Polyline(); 55 polyline.getPoints().addAll(new Double[]{ 56 0.0, 0.0, 57 20.0, 10.0, 58 10.0, 20.0 }); 59 </PRE> 60 * @since JavaFX 2.0 61 */ 62 public class Polyline extends Shape { 63 static { 64 PolylineHelper.setPolylineAccessor(new PolylineHelper.PolylineAccessor() { 65 @Override 66 public NGNode doCreatePeer(Node node) { 67 return ((Polyline) node).doCreatePeer(); 68 } 69 70 @Override 71 public void doUpdatePeer(Node node) { 72 ((Polyline) node).doUpdatePeer(); 73 } 74 75 @Override 76 public Paint doCssGetFillInitialValue(Shape shape) { 77 return ((Polyline) shape).doCssGetFillInitialValue(); 78 } 79 80 @Override 81 public Paint doCssGetStrokeInitialValue(Shape shape) { 82 return ((Polyline) shape).doCssGetStrokeInitialValue(); 83 } 84 85 @Override 86 public com.sun.javafx.geom.Shape doConfigShape(Shape shape) { 87 return ((Polyline) shape).doConfigShape(); 88 } 89 }); 90 } 91 92 private final Path2D shape = new Path2D(); 93 94 { 95 // To initialize the class helper at the begining each constructor of this class 96 PolylineHelper.initHelper(this); 97 98 // overriding default values for fill and stroke 99 // Set through CSS property so that it appears to be a UA style rather 100 // that a USER style so that fill and stroke can still be set from CSS. 101 ((StyleableProperty)fillProperty()).applyStyle(null, null); 102 ((StyleableProperty)strokeProperty()).applyStyle(null, Color.BLACK); 103 } 104 105 /** 106 * Creates an empty instance of Polyline. 107 */ 108 public Polyline() { 109 } 110 111 /** 112 * Creates a new instance of Polyline. 113 * @param points the coordinates of the polyline segments 114 */ 115 public Polyline(double... points) { 116 if (points != null) { 117 for (double p : points) { 118 this.getPoints().add(p); 119 } 120 } 121 } 122 123 /** 124 * Defines the coordinates of the polyline segments. 125 * 126 * @defaultValue empty 127 */ 128 private final ObservableList<Double> points = new TrackableObservableList<Double>() { 129 @Override 130 protected void onChanged(Change<Double> c) { 131 NodeHelper.markDirty(Polyline.this, DirtyBits.NODE_GEOMETRY); 132 impl_geomChanged(); 133 } 134 }; 135 136 /** 137 * Gets the coordinates of the {@code PolyLine} segments. 138 * @return An observable list of points constituting segments of this 139 * {@code PolyLine} 140 */ 141 public final ObservableList<Double> getPoints() { return points; } 142 143 /* 144 * Note: This method MUST only be called via its accessor method. 145 */ 146 private NGNode doCreatePeer() { 147 return new NGPolyline(); 148 } 149 150 /** 151 * @treatAsPrivate implementation detail 152 * @deprecated This is an internal API that is not intended for use and will be removed in the next version 153 */ 154 @Deprecated 155 public BaseBounds impl_computeGeomBounds(BaseBounds bounds, BaseTransform tx) { 156 if (getMode() == NGShape.Mode.EMPTY || getPoints().size() <= 1) { 157 return bounds.makeEmpty(); 158 } 159 160 if (getPoints().size() == 2) { 161 if (getMode() == NGShape.Mode.FILL || getStrokeType() == StrokeType.INSIDE) { 162 return bounds.makeEmpty(); 163 } 164 double upad = getStrokeWidth(); 165 if (getStrokeType() == StrokeType.CENTERED) { 166 upad /= 2.0f; 167 } 168 return computeBounds(bounds, tx, upad, 0.5f, 169 getPoints().get(0), getPoints().get(1), 0.0f, 0.0f); 170 } else { 171 return computeShapeBounds(bounds, tx, ShapeHelper.configShape(this)); 172 } 173 } 174 175 /* 176 * Note: This method MUST only be called via its accessor method. 177 */ 178 private Path2D doConfigShape() { 179 double p1 = getPoints().get(0); 180 double p2 = getPoints().get(1); 181 shape.reset(); 182 shape.moveTo((float)p1, (float)p2); 183 final int numValidPoints = getPoints().size() & ~1; 184 for (int i = 2; i < numValidPoints; i += 2) { 185 p1 = getPoints().get(i); p2 = getPoints().get(i+1); 186 shape.lineTo((float)p1, (float)p2); 187 } 188 return shape; 189 } 190 191 /* 192 * Note: This method MUST only be called via its accessor method. 193 */ 194 private void doUpdatePeer() { 195 if (NodeHelper.isDirty(this, DirtyBits.NODE_GEOMETRY)) { 196 final int numValidPoints = getPoints().size() & ~1; 197 float points_array[] = new float[numValidPoints]; 198 for (int i = 0; i < numValidPoints; i++) { 199 points_array[i] = (float)getPoints().get(i).doubleValue(); 200 } 201 final NGPolyline peer = NodeHelper.getPeer(this); 202 peer.updatePolyline(points_array); 203 } 204 } 205 206 /*************************************************************************** 207 * * 208 * Stylesheet Handling * 209 * * 210 **************************************************************************/ 211 212 /* 213 * Some sub-class of Shape, such as {@link Line}, override the 214 * default value for the {@link Shape#fill} property. This allows 215 * CSS to get the correct initial value. 216 * 217 * Note: This method MUST only be called via its accessor method. 218 */ 219 private Paint doCssGetFillInitialValue() { 220 return null; 221 } 222 223 /* 224 * Some sub-class of Shape, such as {@link Line}, override the 225 * default value for the {@link Shape#stroke} property. This allows 226 * CSS to get the correct initial value. 227 * 228 * Note: This method MUST only be called via its accessor method. 229 */ 230 private Paint doCssGetStrokeInitialValue() { 231 return Color.BLACK; 232 } 233 234 /** 235 * Returns a string representation of this {@code Polyline} object. 236 * @return a string representation of this {@code Polyline} object. 237 */ 238 @Override 239 public String toString() { 240 final StringBuilder sb = new StringBuilder("Polyline["); 241 242 String id = getId(); 243 if (id != null) { 244 sb.append("id=").append(id).append(", "); 245 } 246 247 sb.append("points=").append(getPoints()); 248 249 sb.append(", fill=").append(getFill()); 250 251 Paint stroke = getStroke(); 252 if (stroke != null) { 253 sb.append(", stroke=").append(stroke); 254 sb.append(", strokeWidth=").append(getStrokeWidth()); 255 } 256 257 return sb.append("]").toString(); 258 } 259 }