[geotk] Adding GraphicBuilder to its own MapLayer

Thys Meintjes sthysel at gmail.com
Mon Jul 25 03:10:43 EDT 2011


Hi All,

I'm attempting to keep my special target graphics layer sperate from the
coverage or shape layers.
First I just added the builder to a EmptyMapLayer form MapBuilder - but that
failed to produce any output - maybe
I should have added a envelope ?

I then tried adding the GraphicsBuilder to the first map layer in the
context:

  public void addTargetGraphicsBuilder(TargetGraphicBuilder builder) {
        MapLayer targetLayer = context.layers().get(0);
        targetLayer.graphicBuilders().add(builder);
    }

The first maplayer is a coverage... That didn't work either. I kind of
expected it to...

The demo code uses a FeatureLayer with a GraphicsBuilder. Because I have a
bunch of points (targets)
I could conceivably make a Feature of each of them - add it to a store and
create a FeatureLayer from that.
These points are not static and they have a life time of about 10 seconds so
the data changes continuously.
I don't know if that is the correct approach though.

I guess my question is how do I use a GraphicsBuilder to build to a
dedicated maplayer. And If I can how do I construct
a maplayer with minimal ceremony for use with a GraphicsBuilder.


Here is my builder - in case I did something stupid:

public class TargetGraphicBuilder implements GraphicBuilder<GraphicJ2D> {

    private final static Logger logger =
LoggerFactory.getLogger(TargetGraphicBuilder.class);
    private TargetManager targetManager;

    public TargetGraphicBuilder(TargetManager manager) {
        targetManager = manager;
    }

    @Override
    public Collection<GraphicJ2D> createGraphics(MapLayer layer, Canvas
canvas) {

        if (canvas instanceof J2DCanvas) {
            final J2DCanvas j2dcanvas = (J2DCanvas) canvas;
            return makeTargets(j2dcanvas);
        }
        logger.debug("Canvas not a J2DCanvas");
        return Collections.emptyList();
    }

    private Collection makeTargets(J2DCanvas canvas) {
        ArrayList<TargetGraphic> targetGraphics = new
ArrayList<TargetGraphic>();
        for (Target t : targetManager.getAllTargets()) {
            targetGraphics.add(new TargetGraphic(canvas, t));
        }
        //return Collections.unmodifiableList(targetGraphics);
        return targetGraphics;
    }

    @Override
    public Class<GraphicJ2D> getGraphicType() {
        return GraphicJ2D.class;
    }

    @Override
    public Image getLegend(MapLayer layer) throws PortrayalException {
        return null;
    }

    public String getName() {
        return targetManager.getName();
    }

And the graphic:

public class TargetGraphic extends AbstractGraphicJ2D {

    private final static Logger logger =
LoggerFactory.getLogger(TargetGraphic.class);
    private Target target;

    public TargetGraphic(J2DCanvas canvas, Target target) {
        super(canvas, canvas.getObjectiveCRS2D());
        this.target = target;
    }

    @Override
    public void paint(RenderingContext2D context) {
        final Graphics2D g2d = context.getGraphics();

        // transform from geographic to screen space
        final AffineTransform2D transformer =
context.getObjectiveToDisplay();
        DirectPosition targetPos = target.getPosition();
        logger.debug(targetPos.toString());
        Point2D.Double center = new Point2D.Double(targetPos.getOrdinate(1),
targetPos.getOrdinate(0));
        transformer.transform(center, center);
        logger.debug("Transformed center: " + center);

        // paint a simple circle
        final Shape circle = new Ellipse2D.Double(center.x - 5, center.y -
5, 10, 10);
        g2d.setStroke(new BasicStroke(4));
        g2d.setColor(Color.WHITE);
        g2d.draw(circle);
        g2d.setPaint(Color.RED);
        g2d.fill(circle);
        g2d.setStroke(new BasicStroke(2));
        g2d.setColor(Color.BLACK);
        g2d.draw(circle);

    }

    @Override
    public List<Graphic> getGraphicAt(RenderingContext context, SearchArea
mask, VisitFilter filter, List<Graphic> graphics) {
        return graphics;
    }
}

Note that the graphic works correctly when I add it to the canvas container
directly:

   private void addTarget(Target target) {
        TargetGraphic tg = new TargetGraphic(canvas, target);
        canvas.getContainer().add(tg);
    }


-- 
Thys Meintjes
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/geotoolkit/attachments/20110725/7ece1636/attachment.html


More information about the Geotoolkit mailing list