Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/java2d/ShapeUtils.java @ 3468:f37e7e8907cb
merged flys-artifacts/2.8.1
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:39 +0200 |
parents | 0d8146989012 |
children |
comparison
equal
deleted
inserted
replaced
3387:5ffad8bde8ad | 3468:f37e7e8907cb |
---|---|
1 package de.intevation.flys.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 : |