use of org.geotoolkit.display2d.canvas.painter.SolidColorPainter 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.canvas.painter.SolidColorPainter in project geotoolkit by Geomatys.
the class J2DCanvasBuffered method createBufferedImage.
/**
* This will try to create the most efficient bufferedImage knowing
* the different rendering parameters and hints.
*/
private BufferedImage createBufferedImage(final Dimension dim) {
// See if a color model has been set, if so use it.
final ColorModel cm = (ColorModel) getRenderingHint(GO2Hints.KEY_COLOR_MODEL);
if (cm != null) {
return new BufferedImage(cm, cm.createCompatibleWritableRaster(dim.width, dim.height), cm.isAlphaPremultiplied(), null);
}
// Get the Anti-aliasing value;
final boolean AA;
final Object val = getRenderingHint(RenderingHints.KEY_ANTIALIASING);
if (RenderingHints.VALUE_ANTIALIAS_ON == val) {
AA = true;
} else {
// force AA off, to replace default value.
setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
AA = false;
}
// check background painter.
if (painter == null) {
if (AA) {
// Anti-aliasing enable, unpredictable colors
return new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_ARGB);
}
// check graphic object and see if we can predict colors
final Set<Integer> colors = extractColors(getContainer().flatten(true));
if (colors != null) {
// translucent background
colors.add(0);
// we succesfully predicted the colors, makes an index color model
return createBufferedImage(dim, colors);
} else {
// we can't use a index color model, use an ARGB palette
return new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_ARGB);
}
} else {
if (painter.isOpaque()) {
// see of we can determinate the background color, only if there is no AA.
if (!AA && painter instanceof SolidColorPainter) {
// check graphic object and see if we can predict colors
final Set<Integer> colors = extractColors(getContainer().flatten(true));
if (colors != null) {
final Color bgColor = ((SolidColorPainter) painter).getColor();
colors.add(bgColor.getRGB());
// we succesfully predicted the colors, makes an index color model
return createBufferedImage(dim, colors);
} else {
// Bug OpenJDK : issue with TYPE_INT_RGB, replace by TYPE_3BYTE_BGR
return new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_3BYTE_BGR);
}
} else {
// Bug OpenJDK : issue with TYPE_INT_RGB, replace by TYPE_3BYTE_BGR
return new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_3BYTE_BGR);
}
} else {
// we can't determinate the background colors, use an ARGB palette
return new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_ARGB);
}
}
}
use of org.geotoolkit.display2d.canvas.painter.SolidColorPainter in project geotoolkit by Geomatys.
the class Portrayer method portray.
public BufferedImage portray(final CanvasDef canvasDef, final SceneDef sceneDef) throws PortrayalException {
final Envelope contextEnv = canvasDef.getEnvelope();
final CoordinateReferenceSystem crs = contextEnv.getCoordinateReferenceSystem();
canvas.setSize(canvasDef.getDimension());
canvas.setRenderingHints(sceneDef.getHints());
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 MapLayers context = sceneDef.getContext();
container.setContext(context);
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());
try {
canvas.setVisibleArea(contextEnv);
if (canvasDef.getAzimuth() != 0) {
canvas.rotate(-Math.toRadians(canvasDef.getAzimuth()));
}
} catch (NoninvertibleTransformException ex) {
throw new PortrayalException(ex);
} catch (TransformException ex) {
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);
}
}
canvas.repaint();
final BufferedImage buffer = canvas.getSnapShot();
container.setContext(EMPTY_CONTEXT);
return buffer;
}
Aggregations