use of org.geotoolkit.display2d.container.ContextContainer2D in project geotoolkit by Geomatys.
the class DefaultPortrayalService method prepareCanvas.
public static void prepareCanvas(final J2DCanvas canvas, final CanvasDef canvasDef, final SceneDef sceneDef) throws PortrayalException {
final ContextContainer2D renderer = new ContextContainer2D(canvas);
canvas.setContainer(renderer);
final Color bgColor = canvasDef.getBackground();
if (bgColor != null) {
canvas.setBackgroundPainter(new SolidColorPainter(bgColor));
}
final CanvasMonitor monitor = canvasDef.getMonitor();
if (monitor != null) {
canvas.setMonitor(monitor);
}
final Hints hints = sceneDef.getHints();
if (hints != null) {
for (Entry<?, ?> entry : hints.entrySet()) {
canvas.setRenderingHint((Key) entry.getKey(), entry.getValue());
}
}
final MapLayers context = sceneDef.getContext();
renderer.setContext(context);
GridGeometry gridGeometry = canvasDef.getGridGeometry();
if (gridGeometry != null) {
try {
canvas.setGridGeometry(gridGeometry);
} catch (FactoryException ex) {
throw new PortrayalException("Could not set objective crs", ex);
}
} else {
final Envelope contextEnv = canvasDef.getEnvelope();
final CoordinateReferenceSystem crs = contextEnv.getCoordinateReferenceSystem();
try {
canvas.setObjectiveCRS(crs);
} catch (TransformException | FactoryException ex) {
throw new PortrayalException("Could not set objective crs", ex);
}
// we specifically say to not repect X/Y proportions
canvas.setAxisProportions(!canvasDef.isStretchImage());
// setVisibleArea -> setAxisRange -> setRange.
if (contextEnv != null) {
try {
canvas.setGridGeometry(canvasDef.getOrCreateGridGeometry());
} catch (Exception e) {
// Rollback to previous behavior
try {
canvas.setVisibleArea(contextEnv);
if (canvasDef.getAzimuth() != 0) {
canvas.rotate(-Math.toRadians(canvasDef.getAzimuth()));
}
} catch (NoninvertibleTransformException | TransformException ex) {
ex.addSuppressed(e);
throw new PortrayalException(ex);
}
}
}
}
// paints all extensions
final List<PortrayalExtension> extensions = sceneDef.extensions();
if (extensions != null) {
for (final PortrayalExtension extension : extensions) {
if (extension != null)
extension.completeCanvas(canvas);
}
}
}
use of org.geotoolkit.display2d.container.ContextContainer2D in project geotoolkit by Geomatys.
the class DefaultPortrayalService method visit.
public static void visit(final CanvasDef canvasDef, final SceneDef sceneDef, final VisitDef visitDef) throws PortrayalException {
final Envelope contextEnv = canvasDef.getEnvelope();
final Dimension canvasDimension = canvasDef.getDimension();
final Hints hints = sceneDef.getHints();
final MapLayers context = sceneDef.getContext();
final boolean strechImage = canvasDef.isStretchImage();
final J2DCanvasBuffered canvas = new J2DCanvasBuffered(contextEnv.getCoordinateReferenceSystem(), canvasDimension, hints);
final ContextContainer2D renderer = new ContextContainer2D(canvas);
canvas.setContainer(renderer);
renderer.setContext(context);
try {
canvas.setObjectiveCRS(contextEnv.getCoordinateReferenceSystem());
} catch (TransformException | FactoryException ex) {
throw new PortrayalException("Could not set objective crs", ex);
}
// we specifically say to not repect X/Y proportions
canvas.setAxisProportions(!strechImage);
try {
canvas.setVisibleArea(contextEnv);
} catch (NoninvertibleTransformException | TransformException ex) {
throw new PortrayalException(ex);
}
final Shape selectedArea = visitDef.getArea();
final GraphicVisitor visitor = visitDef.getVisitor();
try {
canvas.getGraphicsIn(selectedArea, visitor);
} catch (Exception ex) {
if (ex instanceof PortrayalException) {
throw (PortrayalException) ex;
} else {
throw new PortrayalException(ex);
}
} finally {
visitor.endVisit();
canvas.clearCache();
}
}
use of org.geotoolkit.display2d.container.ContextContainer2D in project geotoolkit by Geomatys.
the class GraphicLegendJ2D method paint.
@Override
protected void paint(final RenderingContext2D context, final int position, final int[] offset) {
if (!isVisible()) {
return;
}
final GraphicContainer container = getCanvas().getContainer();
if (!(container instanceof ContextContainer2D))
return;
final ContextContainer2D cc = (ContextContainer2D) container;
final MapLayers mapContext = cc.getContext();
final Graphics2D g = context.getGraphics();
context.switchToDisplayCRS();
final Rectangle bounds = context.getCanvasDisplayBounds();
Dimension maxSize = J2DLegendUtilities.estimate(g, mapContext, template, true);
final int imgHeight = maxSize.height;
final int imgWidth = maxSize.width;
int x = 0;
int y = 0;
switch(position) {
case NORTH:
x = (bounds.width - imgWidth) / 2 + offset[0];
y = offset[1];
break;
case NORTH_EAST:
x = (bounds.width - imgWidth) - offset[0];
y = offset[1];
break;
case NORTH_WEST:
x = offset[0];
y = offset[1];
break;
case SOUTH:
x = (bounds.width - imgWidth) / 2 + offset[0];
y = (bounds.height - imgHeight) - offset[1];
break;
case SOUTH_EAST:
x = (bounds.width - imgWidth) - offset[0];
y = (bounds.height - imgHeight) - offset[1];
break;
case SOUTH_WEST:
x = offset[0];
y = (bounds.height - imgHeight) - offset[1];
break;
case CENTER:
x = (bounds.width - imgWidth) / 2 + offset[0];
y = (bounds.height - imgHeight) / 2 + offset[1];
break;
case EAST:
x = (bounds.width - imgWidth) - offset[0];
y = (bounds.height - imgHeight) / 2 + offset[1];
break;
case WEST:
x = offset[0];
y = (bounds.height - imgHeight) / 2 + offset[1];
break;
}
try {
// paint all labels, so that we avoid conflicts
context.getLabelRenderer(true).portrayLabels();
} catch (TransformException ex) {
context.getMonitor().exceptionOccured(ex, Level.WARNING);
}
final Rectangle area = new Rectangle(x, y, imgWidth, imgHeight);
J2DLegendUtilities.paintLegend(mapContext, g, area, template);
}
Aggregations