view artifacts/src/main/java/org/dive4elements/river/java2d/ShapeUtils.java @ 9496:d8e753d0fdb9

stripedArea introduced for Assessment Scheme/Bewertungsschema
author gernotbelger
date Wed, 26 Sep 2018 15:48:05 +0200
parents ddcd52d239cd
children
line wrap: on
line source
/* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
 * Software engineering by Intevation GmbH
 *
 * This file is Free Software under the GNU AGPL (>=v3)
 * and comes with ABSOLUTELY NO WARRANTY! Check out the
 * documentation coming with Dive4Elements River for details.
 */

package org.dive4elements.river.java2d;

import java.awt.Color;
import java.awt.Shape;
import java.awt.geom.Area;
import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Rectangle2D;
import java.util.HashMap;
import java.util.Map;

public class ShapeUtils {

    public static enum ShapeType {
        measured, digitized, interpolated, outlier
    }

    private static Map<Long, Shape> scaledShapesCache = new HashMap<>();

    private static final Shape createCross(final float size) {

        final GeneralPath p = new GeneralPath();

        p.moveTo(-size, -size);
        p.lineTo(size, size);
        p.closePath();
        p.moveTo(-size, size);
        p.lineTo(size, -size);
        p.closePath();

        return p;
    }

    private static Shape createBox(final float size) {
        return new Rectangle2D.Double(-size, -size, size * 2, size * 2);
    }

    private static Shape createCircle(final float size) {
        return new Ellipse2D.Float(-size, -size, size * 2, size * 2);
    }

    private static final Shape createTriangle(final float size) {
        final GeneralPath p = new GeneralPath();

        p.moveTo(-size, size);
        p.lineTo(size, size);
        p.lineTo(0, -size);
        p.closePath();

        return new Area(p);
    }

    public static synchronized Shape getScaledShape(final ShapeType type, final float size) {

        final Long hash = Long.valueOf((((long) type.ordinal()) << 32) | Float.floatToIntBits(size));

        final Shape shape = scaledShapesCache.get(hash);
        if (shape != null)
            return shape;

        final Shape newShape = createScaledShape(type, size);
        scaledShapesCache.put(hash, newShape);
        return newShape;
    }

    private static Shape createScaledShape(final ShapeType type, final float size) {
        switch (type) {
        case measured:
            return createBox(size);

        case digitized:
            return createCross(size);

        case outlier:
            return createTriangle(size);

        case interpolated:
        default:
            return createCircle(size);
        }
    }

    public static Color withAlpha(final Color color, final int transparencyPercent) {

        if (transparencyPercent <= 0 || transparencyPercent > 100 || color == null)
            return color;

        return new Color(color.getRed(), color.getGreen(), color.getBlue(), (int) ((100 - transparencyPercent) * 2.55f));
    }
}

http://dive4elements.wald.intevation.org