sascha@3162: package de.intevation.flys.java2d;
sascha@3162: 
sascha@3162: import java.awt.Shape;
sascha@3162: 
sascha@3162: import java.awt.geom.AffineTransform;
sascha@3162: import java.awt.geom.Ellipse2D;
sascha@3162: import java.awt.geom.GeneralPath;
sascha@3162: import java.awt.geom.Rectangle2D;
sascha@3162: 
sascha@3162: import java.util.HashMap;
sascha@3162: import java.util.Map;
sascha@3162: 
sascha@3162: public class ShapeUtils
sascha@3162: {
sascha@3162:     // TODO: Use enum
sascha@3162:     public static final int MEASURED     = 0;
sascha@3162:     public static final int DIGITIZED    = 1;
sascha@3162:     public static final int INTERPOLATED = 2;
sascha@3162: 
sascha@3162:     public static final boolean DIGITIZED_FILL    = false;
sascha@3162:     public static final boolean MEASURED_FILL     = true;
sascha@3162:     public static final boolean INTERPOLATED_FILL = false;
sascha@3162: 
sascha@3162:    public static final Shape DIGITIZED_SHAPE =
sascha@3162:         createCross(4f);
sascha@3162: 
sascha@3162:     public static final Shape MEASURED_SHAPE =
sascha@3162:         new Rectangle2D.Double(-2, -2, 4, 4);
sascha@3162: 
sascha@3162:     public static final Shape INTERPOLATED_SHAPE =
sascha@3162:         new Ellipse2D.Double(-2, -2, 4, 4);
sascha@3162: 
sascha@3162:     protected static Map<Long, Shape> scaledShapesCache =
sascha@3162:         new HashMap<Long, Shape>();
sascha@3162: 
sascha@3162:     public static final Shape createCross(float size) {
sascha@3162:         float half = size * 0.5f;
sascha@3162:         GeneralPath p = new GeneralPath();
sascha@3162:         p.moveTo(-half, -half);
sascha@3162:         p.lineTo(half, half);
sascha@3162:         p.closePath();
sascha@3162:         p.moveTo(-half, half);
sascha@3162:         p.lineTo(half, -half);
sascha@3162:         p.closePath();
sascha@3162:         return p;
sascha@3162:     }
sascha@3162: 
sascha@3162:     public static Shape scale(Shape shape, float factor) {
sascha@3162:         if (factor == 1f) {
sascha@3162:             return shape;
sascha@3162:         }
sascha@3162:         AffineTransform xform =
sascha@3162:             AffineTransform.getScaleInstance(factor, factor);
sascha@3162: 
sascha@3162:         GeneralPath gp = new GeneralPath(shape);
sascha@3162:         return gp.createTransformedShape(xform);
sascha@3162:     }
sascha@3162: 
sascha@3162:     public static synchronized Shape getScaledShape(int type, float size) {
sascha@3162: 
sascha@3162:         Long hash = Long.valueOf(
sascha@3162:             (((long)type) << 32) | Float.floatToIntBits(size));
sascha@3162: 
sascha@3162:         Shape shape = scaledShapesCache.get(hash);
sascha@3162: 
sascha@3162:         if (shape == null) {
sascha@3162:             switch (type) {
sascha@3162:                 case MEASURED:
sascha@3162:                     shape = MEASURED_SHAPE;
sascha@3162:                     break;
sascha@3162:                 case DIGITIZED:
sascha@3162:                     shape = DIGITIZED_SHAPE;
sascha@3162:                     break;
sascha@3162:                 default:
sascha@3162:                     shape = INTERPOLATED_SHAPE;
sascha@3162:             }
sascha@3162:             scaledShapesCache.put(hash, shape = scale(shape, size));
sascha@3162:         }
sascha@3162: 
sascha@3162:         return shape;
sascha@3162:     }
sascha@3162: }
sascha@3162: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :