view gnv-artifacts/src/main/java/de/intevation/gnv/jfreechart/PolygonRenderer.java @ 437:b624879d2902

Added vectorizer rings callback to generate iso lines. gnv-artifacts/trunk@485 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sun, 27 Dec 2009 05:25:40 +0000
parents f426f55d4f7a
children f5a041000357
line wrap: on
line source
package de.intevation.gnv.jfreechart;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Shape;
import java.awt.geom.GeneralPath;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Rectangle2D.Double;

import org.jfree.data.Range;

/**
 * @author Ingo Weinzierl <ingo.weinzierl@intevation.de>
 */
public class PolygonRenderer
{
    public static final int AREA           = 1;
    public static final int LINES          = 2;
    public static final int AREA_AND_LINES = AREA | LINES;

    public interface PaintLookup {

        Paint getPaint(int index);

    } // interface PaintLookup

    protected int type;

    protected PaintLookup lookup;

    protected PolygonPlot plot;


    public PolygonRenderer(PaintLookup lookup) {
        this(lookup, AREA);
    }

    public PolygonRenderer(PaintLookup lookup, int type) {
        this.lookup = lookup;
        this.type   = type;
    }

    public void draw(
        Graphics2D     graphics,
        Rectangle2D    rectangle,
        PolygonDataset dataset
    ) {
        Rectangle2D bbox = getBoundingBox(dataset);

        double sx = (double)rectangle.getWidth()/bbox.getWidth();
        double sy = (double)rectangle.getHeight()/bbox.getHeight();
        double tx = rectangle.getMinX();
        double ty = rectangle.getMinY();

        graphics.translate(tx, ty);
        graphics.scale(sx, sy);

        int seriesCount = dataset.getSeriesCount();
        for (int i = 0; i < seriesCount; i++) {
            PolygonSeries series   = dataset.getSeries(i);
            Integer       colorIdx = (Integer)series.getAttribute("fill");

            if (colorIdx != null) {
                Paint paint = lookup.getPaint(colorIdx.intValue());
                graphics.setPaint(paint != null ? paint : Color.black);
                graphics.fill(constructShape(series, true));
            }
            else {
                graphics.setPaint(Color.black);
                graphics.draw(constructShape(series, false));
            }
        }
    }

    protected Shape constructShape(PolygonSeries series, boolean close) {
        CompactXYItems [] rings = series.getRings();
        GeneralPath path = new GeneralPath();
        for (int i = 0; i < rings.length; ++i) {
            CompactXYItems ring = rings[i];
            double [] data = ring.getData();
            if (data.length >= 2) {
                path.moveTo((float)data[0], (float)data[1]);
            }
            for (int j = 2; j < data.length;) {
                float x = (float)data[j++];
                float y = (float)data[j++];
                path.lineTo(x, y);
            }
            if (close) {
                path.closePath();
            }
        }
        return path;
    }

    public Rectangle2D getBoundingBox(PolygonDataset dataset) {
        Rectangle2D bbox = null;

        for (int i = 0, N = dataset.getSeriesCount(); i < N; i++) {
            Range domain = dataset.getSeries(i).getDomainBounds();
            Range range  = dataset.getSeries(i).getRangeBounds();

            double x = domain.getLowerBound();
            double y = range.getLowerBound();
            double w = Math.abs(domain.getUpperBound() - x);
            double h = Math.abs(range.getUpperBound() - y);

            if (bbox == null) {
                bbox = new Rectangle2D.Double(x, y, w, h);
            }
            else {
                bbox.add(new Rectangle2D.Double(x, y, w, h));
            }
        }

        return bbox;
    }

    public Rectangle2D getBounds(PolygonSeries series) {

        Range domain = series.getDomainBounds();
        Range range  = series.getRangeBounds();

        return new Rectangle2D.Double(
            domain.getLowerBound(), range.getLowerBound(),
            domain.getUpperBound(), range.getUpperBound()
        );
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org