use of org.geotoolkit.display2d.canvas.RenderingContext2D in project geotoolkit by Geomatys.
the class ProjectedGeometryTest method createProjectedGeometry.
private static ProjectedGeometry createProjectedGeometry(Geometry geometry, Dimension canvasBounds, AffineTransform objToDisp) throws NoninvertibleTransformException, TransformException, FactoryException {
final int canvasWidth = canvasBounds.width;
final int canvasHeight = canvasBounds.height;
// build a maplayer
final FeatureTypeBuilder ftb = new FeatureTypeBuilder();
ftb.setName("test");
ftb.addAttribute(Geometry.class).setName("geom").setCRS(CommonCRS.WGS84.normalizedGeographic());
final FeatureType type = ftb.build();
final Feature feature = type.newInstance();
JTS.setCRS(geometry, CommonCRS.WGS84.normalizedGeographic());
feature.setPropertyValue("geom", geometry);
final FeatureSet col = new InMemoryFeatureSet(type, Arrays.asList(feature));
final List<GraphicalSymbol> symbols = new ArrayList<>();
symbols.add(SF.mark(StyleConstants.MARK_SQUARE, SF.fill(Color.BLACK), SF.stroke(Color.BLACK, 0)));
final Graphic graphic = SF.graphic(symbols, StyleConstants.LITERAL_ONE_FLOAT, FF.literal(2), StyleConstants.LITERAL_ZERO_FLOAT, null, null);
final PointSymbolizer ps = SF.pointSymbolizer(graphic, null);
final MutableStyle style = SF.style(ps);
final MapLayer layer = MapBuilder.createLayer(col);
layer.setStyle(style);
// build a rendering canvas
final J2DCanvasBuffered canvas = new J2DCanvasBuffered(CommonCRS.WGS84.normalizedGeographic(), new Dimension(canvasWidth, canvasHeight));
canvas.applyTransform(objToDisp);
final RenderingContext2D context = canvas.prepareContext(new BufferedImage(canvasWidth, canvasHeight, BufferedImage.TYPE_INT_ARGB).createGraphics());
final ProjectedGeometry pg = new ProjectedGeometry(context);
pg.setDataGeometry(geometry, CommonCRS.WGS84.normalizedGeographic());
Envelope env = canvas.getVisibleEnvelope();
System.out.println(env.getMinimum(0) + " " + env.getMaximum(0));
System.out.println(env.getMinimum(1) + " " + env.getMaximum(1));
return pg;
}
use of org.geotoolkit.display2d.canvas.RenderingContext2D in project geotoolkit by Geomatys.
the class CachedPointSymbolizerTest method testMargin.
@Test
public void testMargin() throws FactoryException {
final GridGeometry grid = new GridGeometry(new GridExtent(1, 1), CRS.getDomainOfValidity(CommonCRS.WGS84.normalizedGeographic()), GridOrientation.HOMOTHETY);
final RenderingContext2D ctx = new RenderingContext2D(grid, null);
{
// NO ANCHOR, NO DISPLACEMENT
final List<GraphicalSymbol> symbols = new ArrayList<>();
final Stroke stroke = SF.stroke(Color.BLACK, 2);
final Fill fill = SF.fill(Color.RED);
final Mark mark = SF.mark(MARK_CIRCLE, fill, stroke);
symbols.add(mark);
final Graphic graphic = SF.graphic(symbols, LITERAL_ONE_FLOAT, FF.literal(12), LITERAL_ONE_FLOAT, DEFAULT_ANCHOR_POINT, DEFAULT_DISPLACEMENT);
final PointSymbolizer symbolizer = SF.pointSymbolizer("mySymbol", (String) null, DEFAULT_DESCRIPTION, Units.POINT, graphic);
final CachedSymbolizer cached = GO2Utilities.getCached(symbolizer, null);
final float margin = cached.getMargin(null, ctx);
// 12/2 + 2*2(stroke width)
assertEquals(8f, margin, DELTA);
}
{
// NO ANCHOR
final List<GraphicalSymbol> symbols = new ArrayList<>();
final Stroke stroke = SF.stroke(Color.BLACK, 2);
final Fill fill = SF.fill(Color.RED);
final Mark mark = SF.mark(MARK_CIRCLE, fill, stroke);
symbols.add(mark);
final Graphic graphic = SF.graphic(symbols, LITERAL_ONE_FLOAT, FF.literal(12), LITERAL_ONE_FLOAT, DEFAULT_ANCHOR_POINT, SF.displacement(10, 15));
final PointSymbolizer symbolizer = SF.pointSymbolizer("mySymbol", (String) null, DEFAULT_DESCRIPTION, Units.POINT, graphic);
final CachedSymbolizer cached = GO2Utilities.getCached(symbolizer, null);
final float margin = cached.getMargin(null, ctx);
// 12/2 + 2*2(stroke width) + 15(disp)
assertEquals(23f, margin, DELTA);
}
{
final List<GraphicalSymbol> symbols = new ArrayList<>();
final Stroke stroke = SF.stroke(Color.BLACK, 2);
final Fill fill = SF.fill(Color.RED);
final Mark mark = SF.mark(MARK_CIRCLE, fill, stroke);
symbols.add(mark);
final Graphic graphic = SF.graphic(symbols, LITERAL_ONE_FLOAT, FF.literal(12), LITERAL_ONE_FLOAT, SF.anchorPoint(0, 1.7), SF.displacement(10, 15));
final PointSymbolizer symbolizer = SF.pointSymbolizer("mySymbol", (String) null, DEFAULT_DESCRIPTION, Units.POINT, graphic);
final CachedSymbolizer cached = GO2Utilities.getCached(symbolizer, null);
final float margin = cached.getMargin(null, ctx);
// 12/2 + 2*2(stroke width) + 15(disp) + 16*(1.7-0.5)
assertEquals(23f + 19.2f, margin, DELTA);
}
}
use of org.geotoolkit.display2d.canvas.RenderingContext2D in project geotoolkit by Geomatys.
the class CanvasRenderer method repaint.
/**
* {@inheritDoc }
*/
@Override
public boolean repaint(final Shape area) {
monitor.renderingStarted();
fireRenderingStateChanged(RENDERING);
final Graphics2D output = g2d;
output.addRenderingHints(getHints(true));
final RenderingContext2D context = prepareContext(output);
// paint background if there is one.
if (painter != null) {
painter.paint(context);
}
boolean dataPainted = false;
final GraphicContainer container = getContainer();
if (container != null) {
dataPainted |= render(context, container.flatten(true));
}
/**
* End painting, erase dirtyArea
*/
output.dispose();
fireRenderingStateChanged(ON_HOLD);
monitor.renderingFinished();
return dataPainted;
}
use of org.geotoolkit.display2d.canvas.RenderingContext2D in project geotoolkit by Geomatys.
the class DefaultPortrayalService method present.
// //////////////////////////////////////////////////////////////////////////
// PRESENTING A CONTEXT ////////////////////////////////////////////////////
// //////////////////////////////////////////////////////////////////////////
/**
* Generate presentation objects for a scene.
* @param canvasDef
* @param sceneDef
* @return stream of Presentation instance.
*/
public static Stream<Presentation> present(final CanvasDef canvasDef, final SceneDef sceneDef) throws PortrayalException {
Stream<Presentation> stream = Stream.empty();
final Envelope contextEnv = canvasDef.getEnvelope();
final CoordinateReferenceSystem crs = contextEnv.getCoordinateReferenceSystem();
final Dimension dim = canvasDef.getDimension();
final BufferedImage img = new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_ARGB);
final J2DCanvasBuffered canvas = new J2DCanvasBuffered(crs, canvasDef.getDimension(), sceneDef.getHints());
prepareCanvas(canvas, canvasDef, sceneDef);
final RenderingContext2D renderContext = canvas.prepareContext(img.createGraphics());
final MapLayers context = sceneDef.getContext();
final List<MapLayer> layers = MapBuilder.getLayers(context);
for (MapLayer layer : layers) {
if (!layer.isVisible())
continue;
final Resource resource = layer.getData();
stream = Stream.concat(stream, present(layer, resource, renderContext));
}
return stream;
}
use of org.geotoolkit.display2d.canvas.RenderingContext2D in project geotoolkit by Geomatys.
the class MapLayerJ2D method paintLayer.
/**
* Render layer, will only be painted if an appropriate graphic builder is attached
* to it.
*/
public Stream<Presentation> paintLayer(final RenderingContext2D context) throws PortrayalException, DataStoreException {
// we abort painting if the layer is not visible.
if (!item.isVisible())
return Stream.empty();
// we abort if opacity is to low
final double opacity = item.getOpacity();
if (opacity < 1e-6)
return Stream.empty();
if (1 - opacity < 1e-6) {
// we are very close to opacity one, no need to create a intermediate image
return streamPresentations(context);
} else {
// create an intermediate layer which will be painted on the main context
// after with the given opacity
final Rectangle rect = context.getCanvasDisplayBounds();
final BufferedImage inter = createBufferedImage(ColorModel.getRGBdefault(), ColorModel.getRGBdefault().createCompatibleSampleModel(rect.width, rect.height));
final Graphics2D g2d = inter.createGraphics();
final RenderingContext2D interContext = context.create(g2d);
final J2DPainter painter = new J2DPainter();
try (Stream<Presentation> stream = streamPresentations(interContext)) {
painter.paint(interContext, stream, true);
}
final GridCoverageBuilder gcb = new GridCoverageBuilder();
gcb.setDomain(interContext.getGridGeometry());
gcb.setValues(inter);
final GridCoverage coverage = gcb.build();
final RasterPresentation rp = new RasterPresentation(layer, null, coverage);
rp.forGrid(context);
rp.composite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float) opacity);
return Stream.of(rp);
}
}
Aggregations