All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nl.cloudfarming.client.geoviewer.render.SplitPolygonDrawingRenderer Maven / Gradle / Ivy

Go to download

AgroSense geoviewer render - contains classes for rendering of various geo objects

The newest version!
/**
 * Copyright (C) 2008-2012 AgroSense Foundation.
 *
 * AgroSense is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * There are special exceptions to the terms and conditions of the GPLv3 as it is applied to
 * this software, see the FLOSS License Exception
 * .
 *
 * AgroSense is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with AgroSense.  If not, see .
 */
package nl.cloudfarming.client.geoviewer.render;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.Polygon;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Point2D;
import java.util.List;

/**
 *
 * @author johan
 */
public class SplitPolygonDrawingRenderer implements DrawingRenderer {
    private final List coords;
    private final Polygon source;
    private final List results;

    /**
     * 
     * @param coords drawn splitline coordinates
     * @param source geometry to split
     * @param results split result geometries
     */
    public SplitPolygonDrawingRenderer(List coords, Polygon source, List results) {
        this.coords = coords;
        this.source = source;
        this.results = results;
    }

    @Override
    public void render(Graphics2D g, Rectangle canvas, GeoTranslator translator) {
        
        // show either the split results when available (shades of green),
        // or the source geometry to split (red):
        if (results.size() == 2) {
            renderGeometry(g, results.get(0), new Color(0, 255, 127, 127), canvas, translator);
            renderGeometry(g, results.get(1), new Color(127, 255, 0, 127), canvas, translator);
        }
        else {
            renderGeometry(g, source, new Color(255, 0, 0, 127), canvas, translator);
        }

        // show the splitline:
        if (coords != null && !coords.isEmpty()) {
            
            int len = coords.size();
            int xs[] = new int[len];
            int ys[] = new int[len];
            
            // TODO: calculate bounding box, 
            // no problems yet since the currently used translator doesn't need it.
            
            for (int i = 0; i < len; i++) {
                Point2D point = translator.geoToPixel(canvas, null, coords.get(i));
                xs[i] = (int) (point.getX() - canvas.x);
                ys[i] = (int) (point.getY() - canvas.y);
            }
            if (len >= 2) {
                renderLine(g, xs, ys);          
            }
            renderPoints(g, xs, ys);
        }
    }

    //TODO FP copied methods from PolygonDrawingRenederer
    //some AbstractDrawing Renderer could be created
    private void renderPoints(Graphics2D g, int[] xs, int[]  ys) {
        int len = xs.length;

        g.setColor(Color.DARK_GRAY);
        for (int i = 0; i < len - 1; i++) {
            g.fillRect(xs[i] - 1, ys[i] - 1, 3, 3);
        }
        // alt. style for last point:
        g.setColor(Color.YELLOW);
        g.fillRect(xs[len - 1] - 1, ys[len - 1] - 1, 3, 3);
    }

    private void renderLine(Graphics2D g, int[] xs, int[]  ys) {
        g.setColor(Color.RED);
        g.drawPolyline(xs, ys, xs.length);
    }
    
    private void renderGeometry(Graphics2D g, Geometry geometry, Color color, Rectangle canvas, GeoTranslator translator) {
        int len = geometry.getNumPoints();
        if (len > 2) {
            g.setColor(color);
            int xs[] = new int[len];
            int ys[] = new int[len];
            for (int i = 0; i < len; i++) {
                Point2D point2D = translator.geoToPixel(canvas, geometry.getEnvelope(), geometry.getCoordinates()[i]);
                xs[i] = (int) (point2D.getX() - canvas.getBounds().getX());
                ys[i] = (int) (point2D.getY() - canvas.getBounds().getY());
            }
            g.fillPolygon(xs, ys, len);
            
            // show geometry outline points;
            // make them a little larger than the split line points to show where they coincide:
            g.setColor(new Color(255, 255, 255, 191));
            for (int i = 0; i < len; i++) {
                g.fillRect(xs[i] - 2, ys[i] - 2, 5, 5);
            }
        }
    }    
        
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy