Interface PointShapeFactory

  1 /*
  2  * Copyright (c) 2016 Vivid Solutions.
  3  *
  4  * All rights reserved. This program and the accompanying materials
  5  * are made available under the terms of the Eclipse Public License 2.0
  6  * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
  7  * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html
  8  * and the Eclipse Distribution License is available at
  9  *
 10  * http://www.eclipse.org/org/documents/edl-v10.php.
 11  */
 12 package org.locationtech.jts.awt;
 13  
 14 import java.awt.Shape;
 15 import java.awt.geom.Ellipse2D;
 16 import java.awt.geom.GeneralPath;
 17 import java.awt.geom.Line2D;
 18 import java.awt.geom.Point2D;
 19 import java.awt.geom.Rectangle2D;
 20  
 21 /**
 22  * An interface for classes which create {@link Shape}s to represent 
 23  * {@link Point}
 24  * geometries. Java2D does not provide an actual point shape, so some other
 25  * shape must be used to render points (e.g. such as a Rectangle or Ellipse).
 26  * 
 27  * @author Martin Davis
 28  * 
 29  */
 30 public interface PointShapeFactory {
 31   /**
 32    * Creates a shape representing a {@link Point}.
 33    * 
 34    * @param point
 35    *          the location of the point
 36    * @return a shape
 37    */
 38   Shape createPoint(Point2D point);
 39  
 40   public static abstract class BasePointShapeFactory implements
 41       PointShapeFactory {
 42     /**
 43      * The default size of the shape
 44      */
 45     public static final double DEFAULT_SIZE = 3.0;
 46  
 47     protected double size = DEFAULT_SIZE;
 48  
 49     /**
 50      * Creates a new factory for points with default size.
 51      * 
 52      */
 53     public BasePointShapeFactory() {
 54     }
 55  
 56     /**
 57      * Creates a factory for points of given size.
 58      * 
 59      * @param size
 60      *          the size of the points
 61      */
 62     public BasePointShapeFactory(double size) {
 63       this.size = size;
 64     }
 65  
 66     /**
 67      * Creates a shape representing a point.
 68      * 
 69      * @param point
 70      *          the location of the point
 71      * @return a shape
 72      */
 73     public abstract Shape createPoint(Point2D point);
 74   }
 75  
 76   public static class Point extends BasePointShapeFactory {
 77     /**
 78      * Creates a new factory for points with default size.
 79      * 
 80      */
 81     public Point() {
 82       super();
 83     }
 84  
 85     /**
 86      * Creates a factory for points of given size.
 87      * 
 88      * @param size
 89      *          the size of the points
 90      */
 91     public Point(double size) {
 92       super(size);
 93     }
 94  
 95     /**
 96      * Creates a shape representing a point.
 97      * 
 98      * @param point
 99      *          the location of the point
100      * @return a shape
101      */
102     public Shape createPoint(Point2D point) {
103       Line2D.Double pointMarker =
104         new Line2D.Double(
105             point.getX(),
106             point.getY(),
107           point.getX(),
108           point.getY());
109       return pointMarker;
110     }
111   }
112   
113   public static class Square extends BasePointShapeFactory {
114     /**
115      * Creates a new factory for squares with default size.
116      * 
117      */
118     public Square() {
119       super();
120     }
121  
122     /**
123      * Creates a factory for squares of given size.
124      * 
125      * @param size
126      *          the size of the points
127      */
128     public Square(double size) {
129       super(size);
130     }
131  
132     /**
133      * Creates a shape representing a point.
134      * 
135      * @param point
136      *          the location of the point
137      * @return a shape
138      */
139     public Shape createPoint(Point2D point) {
140       Rectangle2D.Double pointMarker =
141         new Rectangle2D.Double(
142           0.0,
143           0.0,
144           size,
145           size);
146       pointMarker.x = (point.getX() - (size / 2));
147       pointMarker.y = (point.getY() - (size / 2));
148  
149       return pointMarker;
150     }
151   }
152   
153   public static class Star extends BasePointShapeFactory {
154     /**
155      * Creates a new factory for points with default size.
156      * 
157      */
158     public Star() {
159       super();
160     }
161  
162     /**
163      * Creates a factory for points of given size.
164      * 
165      * @param size
166      *          the size of the points
167      */
168     public Star(double size) {
169       super(size);
170     }
171  
172     /**
173      * Creates a shape representing a point.
174      * 
175      * @param point
176      *          the location of the point
177      * @return a shape
178      */
179     public Shape createPoint(Point2D point) {
180       GeneralPath path = new GeneralPath();
181       path.moveTo((float) point.getX(), (float) (point.getY() - size/2));
182       path.lineTo((float) (point.getX() + size * 1/8), (float) (point.getY() - size * 1/8));
183       path.lineTo((float) (point.getX() + size/2), (float) (point.getY() - size * 1/8));
184       path.lineTo((float) (point.getX() + size * 2/8), (float) (point.getY() + size * 1/8));
185       path.lineTo((float) (point.getX() + size * 3/8), (float) (point.getY() + size/2));
186       path.lineTo((float) (point.getX()), (float) (point.getY() + size * 2/8));
187       path.lineTo((float) (point.getX() - size * 3/8), (float) (point.getY() + size/2));
188       path.lineTo((float) (point.getX() - size * 2/8), (float) (point.getY() + size * 1/8));
189       path.lineTo((float) (point.getX() - size/2), (float) (point.getY() - size * 1/8));
190       path.lineTo((float) (point.getX() - size * 1/8), (float) (point.getY() - size * 1/8));
191       path.closePath();
192       return path;
193     }
194   }
195   
196   public static class Triangle extends BasePointShapeFactory {
197     /**
198      * Creates a new factory for points with default size.
199      * 
200      */
201     public Triangle() {
202       super();
203     }
204  
205     /**
206      * Creates a factory for points of given size.
207      * 
208      * @param size
209      *          the size of the points
210      */
211     public Triangle(double size) {
212       super(size);
213     }
214  
215     /**
216      * Creates a shape representing a point.
217      * 
218      * @param point
219      *          the location of the point
220      * @return a shape
221      */
222     public Shape createPoint(Point2D point) {
223  
224       GeneralPath path = new GeneralPath();
225       path.moveTo((float) (point.getX()), (float) (point.getY() - size / 2));
226       path.lineTo((float) (point.getX() + size / 2), (float) (point.getY() + size / 2));
227       path.lineTo((float) (point.getX() - size / 2), (float) (point.getY() + size / 2));
228       path.lineTo((float) (point.getX()), (float) (point.getY() - size / 2));
229  
230       return path;
231     }
232  
233   }
234   public static class Circle extends BasePointShapeFactory {
235     /**
236      * Creates a new factory for points with default size.
237      * 
238      */
239     public Circle() {
240       super();
241     }
242  
243     /**
244      * Creates a factory for points of given size.
245      * 
246      * @param size
247      *          the size of the points
248      */
249     public Circle(double size) {
250       super(size);
251     }
252  
253     /**
254      * Creates a shape representing a point.
255      * 
256      * @param point
257      *          the location of the point
258      * @return a shape
259      */
260     public Shape createPoint(Point2D point) {
261       Ellipse2D.Double pointMarker =
262         new Ellipse2D.Double(
263           0.0,
264           0.0,
265           size,
266           size);
267       pointMarker.x = (point.getX() - (size / 2));
268       pointMarker.y = (point.getY() - (size / 2));
269  
270       return pointMarker;
271     }
272  
273   }
274   public static class Cross extends BasePointShapeFactory {
275     /**
276      * Creates a new factory for points with default size.
277      * 
278      */
279     public Cross() {
280       super();
281     }
282  
283     /**
284      * Creates a factory for points of given size.
285      * 
286      * @param size
287      *          the size of the points
288      */
289     public Cross(double size) {
290       super(size);
291     }
292  
293     /**
294      * Creates a shape representing a point.
295      * 
296      * @param point
297      *          the location of the point
298      * @return a shape
299      */
300     public Shape createPoint(Point2D point) {
301  
302       float x1 = (float) (point.getX() - size/2f);
303       float x2 = (float) (point.getX() - size/4f);
304       float x3 = (float) (point.getX() + size/4f);
305       float x4 = (float) (point.getX() + size/2f);
306  
307       float y1 = (float) (point.getY() - size/2f);
308       float y2 = (float) (point.getY() - size/4f);
309       float y3 = (float) (point.getY() + size/4f);
310       float y4 = (float) (point.getY() + size/2f);
311  
312   GeneralPath path = new GeneralPath();
313       path.moveTo(x2, y1);
314       path.lineTo(x3, y1);
315       path.lineTo(x3, y2);
316       path.lineTo(x4, y2);
317       path.lineTo(x4, y3);
318       path.lineTo(x3, y3);
319       path.lineTo(x3, y4);
320       path.lineTo(x2, y4);
321       path.lineTo(x2, y3);
322       path.lineTo(x1, y3);
323       path.lineTo(x1, y2);
324       path.lineTo(x2, y2);
325       path.lineTo(x2, y1);
326  
327       return path;
328     }
329  
330   }
331   public static class X extends BasePointShapeFactory {
332     /**
333      * Creates a new factory for points with default size.
334      * 
335      */
336     public X() {
337       super();
338     }
339  
340     /**
341      * Creates a factory for points of given size.
342      * 
343      * @param size
344      *          the size of the points
345      */
346     public X(double size) {
347       super(size);
348     }
349  
350     /**
351      * Creates a shape representing a point.
352      * 
353      * @param point
354      *          the location of the point
355      * @return a shape
356      */
357     public Shape createPoint(Point2D point) {
358       GeneralPath path = new GeneralPath();
359       path.moveTo((float) (point.getX()), (float) (point.getY() - size * 1/8));
360       path.lineTo((float) (point.getX() + size * 2/8), (float) (point.getY() - size/2));
361       path.lineTo((float) (point.getX() + size/2), (float) (point.getY() - size/2));
362       path.lineTo((float) (point.getX() + size * 1/8), (float) (point.getY()));
363       path.lineTo((float) (point.getX() + size/2), (float) (point.getY() + size/2));
364       path.lineTo((float) (point.getX() + size * 2/8), (float) (point.getY() + size/2));
365       path.lineTo((float) (point.getX()), (float) (point.getY() + size * 1/8));
366       path.lineTo((float) (point.getX() - size * 2/8), (float) (point.getY() + size/2));
367       path.lineTo((float) (point.getX() - size/2), (float) (point.getY() + size/2));
368       path.lineTo((float) (point.getX() - size * 1/8), (float) (point.getY()));
369       path.lineTo((float) (point.getX() - size/2), (float) (point.getY() - size/2));
370       path.lineTo((float) (point.getX() - size * 2/8), (float) (point.getY() - size/2));
371       path.closePath();
372       return path;
373     }
374  
375   }
376 }
377