comparison artifacts/src/main/java/org/dive4elements/river/java2d/ShapeUtils.java @ 5838:5aa05a7a34b7

Rename modules to more fitting names.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 15:23:37 +0200
parents flys-artifacts/src/main/java/org/dive4elements/river/java2d/ShapeUtils.java@bd047b71ab37
children 4897a58c8746
comparison
equal deleted inserted replaced
5837:d9901a08d0a6 5838:5aa05a7a34b7
1 package org.dive4elements.river.java2d;
2
3 import java.awt.Shape;
4
5 import java.awt.geom.AffineTransform;
6 import java.awt.geom.Ellipse2D;
7 import java.awt.geom.GeneralPath;
8 import java.awt.geom.Rectangle2D;
9
10 import java.util.HashMap;
11 import java.util.Map;
12
13 public class ShapeUtils
14 {
15 // TODO: Use enum
16 public static final int MEASURED = 0;
17 public static final int DIGITIZED = 1;
18 public static final int INTERPOLATED = 2;
19
20 public static final boolean DIGITIZED_FILL = false;
21 public static final boolean MEASURED_FILL = true;
22 public static final boolean INTERPOLATED_FILL = false;
23
24 public static final Shape DIGITIZED_SHAPE =
25 createCross(4f);
26
27 public static final Shape MEASURED_SHAPE =
28 new Rectangle2D.Double(-2, -2, 4, 4);
29
30 public static final Shape INTERPOLATED_SHAPE =
31 new Ellipse2D.Double(-2, -2, 4, 4);
32
33 protected static Map<Long, Shape> scaledShapesCache =
34 new HashMap<Long, Shape>();
35
36 public static final Shape createCross(float size) {
37 float half = size * 0.5f;
38 GeneralPath p = new GeneralPath();
39 p.moveTo(-half, -half);
40 p.lineTo(half, half);
41 p.closePath();
42 p.moveTo(-half, half);
43 p.lineTo(half, -half);
44 p.closePath();
45 return p;
46 }
47
48 public static Shape scale(Shape shape, float factor) {
49 if (factor == 1f) {
50 return shape;
51 }
52 AffineTransform xform =
53 AffineTransform.getScaleInstance(factor, factor);
54
55 GeneralPath gp = new GeneralPath(shape);
56 return gp.createTransformedShape(xform);
57 }
58
59 public static synchronized Shape getScaledShape(int type, float size) {
60
61 Long hash = Long.valueOf(
62 (((long)type) << 32) | Float.floatToIntBits(size));
63
64 Shape shape = scaledShapesCache.get(hash);
65
66 if (shape == null) {
67 switch (type) {
68 case MEASURED:
69 shape = MEASURED_SHAPE;
70 break;
71 case DIGITIZED:
72 shape = DIGITIZED_SHAPE;
73 break;
74 default:
75 shape = INTERPOLATED_SHAPE;
76 }
77 scaledShapesCache.put(hash, shape = scale(shape, size));
78 }
79
80 return shape;
81 }
82 }
83 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org