/** * DOCUMENT ME! * * @param graphics DOCUMENT ME! * @param shape DOCUMENT ME! * @param style DOCUMENT ME! */ public static void drawShape(Graphics2D graphics, Shape shape, Style2D style) { if (style instanceof MarkStyle2D) { // get the point onto the shape has to be painted float[] coords = new float[2]; PathIterator iter = shape.getPathIterator(IDENTITY_TRANSFORM); iter.currentSegment(coords); MarkStyle2D ms2d = (MarkStyle2D) style; Shape transformedShape = ms2d.getTransformedShape(coords[0], coords[1]); if (transformedShape != null) { if (ms2d.getFill() != null) { graphics.setPaint(ms2d.getFill()); graphics.setComposite(ms2d.getFillComposite()); graphics.fill(transformedShape); } if (ms2d.getContour() != null) { graphics.setPaint(ms2d.getContour()); graphics.setStroke(ms2d.getStroke()); graphics.setComposite(ms2d.getContourComposite()); graphics.draw(transformedShape); } } } else if (style instanceof GraphicStyle2D) { // get the point onto the shape has to be painted float[] coords = new float[2]; PathIterator iter = shape.getPathIterator(IDENTITY_TRANSFORM); iter.currentSegment(coords); GraphicStyle2D gs2d = (GraphicStyle2D) style; renderImage( graphics, coords[0], coords[1], (Image) gs2d.getImage(), gs2d.getRotation(), gs2d.getOpacity()); } else if (style instanceof TextStyle2D) { // get the point onto the shape has to be painted float[] coords = new float[2]; PathIterator iter = shape.getPathIterator(IDENTITY_TRANSFORM); iter.currentSegment(coords); AffineTransform old = graphics.getTransform(); AffineTransform temp = new AffineTransform(old); TextStyle2D ts2d = (TextStyle2D) style; GlyphVector textGv = ts2d.getTextGlyphVector(graphics); Rectangle2D bounds = textGv.getVisualBounds(); temp.translate(coords[0], coords[1]); double x = 0; double y = 0; x = (ts2d.getAnchorX() * (-bounds.getWidth())) + ts2d.getDisplacementX(); y = (ts2d.getAnchorY() * (bounds.getHeight())) + ts2d.getDisplacementY(); temp.rotate(ts2d.getRotation()); temp.translate(x, y); graphics.setTransform(temp); if (ts2d.getHaloFill() != null) { float radious = ts2d.getHaloRadius(); // graphics.translate(radious, -radious); graphics.setPaint(ts2d.getHaloFill()); graphics.setComposite(ts2d.getHaloComposite()); graphics.fill(ts2d.getHaloShape(graphics)); // graphics.translate(radious, radious); } if (ts2d.getFill() != null) { graphics.setPaint(ts2d.getFill()); graphics.setComposite(ts2d.getComposite()); graphics.drawGlyphVector(textGv, 0, 0); } graphics.setTransform(old); } else { // if the style is a polygon one, process it even if the polyline is // not // closed (by SLD specification) if (style instanceof PolygonStyle2D) { PolygonStyle2D ps2d = (PolygonStyle2D) style; if (ps2d.getFill() != null) { Paint paint = ps2d.getFill(); if (paint instanceof TexturePaint) { TexturePaint tp = (TexturePaint) paint; BufferedImage image = tp.getImage(); Rectangle2D rect = tp.getAnchorRect(); AffineTransform at = graphics.getTransform(); double width = rect.getWidth() * at.getScaleX(); double height = rect.getHeight() * at.getScaleY(); Rectangle2D scaledRect = new Rectangle2D.Double(0, 0, width, height); paint = new TexturePaint(image, scaledRect); } graphics.setPaint(paint); graphics.setComposite(ps2d.getFillComposite()); graphics.fill(shape); } } if (style instanceof LineStyle2D) { LineStyle2D ls2d = (LineStyle2D) style; if (ls2d.getStroke() != null) { // see if a graphic stroke is to be used, the drawing method // is completely // different in this case Paint paint = ls2d.getContour(); if (paint instanceof TexturePaint) { TexturePaint tp = (TexturePaint) paint; BufferedImage image = tp.getImage(); Rectangle2D rect = tp.getAnchorRect(); AffineTransform at = graphics.getTransform(); double width = rect.getWidth() * at.getScaleX(); double height = rect.getHeight() * at.getScaleY(); Rectangle2D scaledRect = new Rectangle2D.Double(0, 0, width, height); paint = new TexturePaint(image, scaledRect); } graphics.setPaint(paint); graphics.setStroke(ls2d.getStroke()); graphics.setComposite(ls2d.getContourComposite()); graphics.draw(shape); } } } }
private void renderGraphicsStroke( Graphics2D graphics, double x, double y, Style2D style, double rotation, double graphicRotation, Composite composite, boolean isLabelObstacle) { if (LOGGER.isLoggable(Level.FINEST)) { LOGGER.finest("drawing GraphicsStroke@" + x + "," + y); } graphics.setComposite(composite); if (style instanceof GraphicStyle2D) { GraphicStyle2D gstyle = (GraphicStyle2D) style; BufferedImage image = gstyle.getImage(); double dx = -image.getWidth() * gstyle.getAnchorPointX() + gstyle.getDisplacementX(); double dy = -image.getHeight() * gstyle.getAnchorPointY() + gstyle.getDisplacementY(); renderImage(graphics, x, y, dx, dy, image, rotation, composite, isLabelObstacle); } else if (style instanceof MarkStyle2D) { // almost like the code in the main paint method, but // here we don't use the mark composite MarkStyle2D ms2d = (MarkStyle2D) style; Shape transformedShape = ms2d.getTransformedShape((float) x, (float) y, (float) rotation, (float) graphicRotation); if (transformedShape != null) { if (ms2d.getFill() != null) { graphics.setPaint(ms2d.getFill()); graphics.fill(transformedShape); } if (ms2d.getContour() != null) { graphics.setPaint(ms2d.getContour()); graphics.setStroke(ms2d.getStroke()); graphics.draw(transformedShape); } if (isLabelObstacle) { labelCache.put(transformedShape.getBounds2D()); } } } else if (style instanceof IconStyle2D) { IconStyle2D icons = (IconStyle2D) style; Icon icon = icons.getIcon(); AffineTransform markAT = new AffineTransform(graphics.getTransform()); markAT.translate(x, y); markAT.rotate(rotation); // the displacement to be applied to all points, centers the icon and applies the // Graphic displacement as well float dx = -(icon.getIconWidth() * icons.getAnchorPointX()) + icons.getDisplacementX(); float dy = -(icon.getIconHeight() * icons.getAnchorPointY()) + icons.getDisplacementY(); markAT.translate(dx, dy); AffineTransform temp = graphics.getTransform(); try { graphics.setTransform(markAT); icon.paintIcon(null, graphics, 0, 0); } finally { graphics.setTransform(temp); } if (isLabelObstacle) { labelCache.put( new Rectangle2D.Double(x + dx, y + dy, icon.getIconWidth(), icon.getIconHeight())); } } }
/** * Invoked automatically when a polyline is about to be draw. This implementation paints the * polyline according to the rendered style * * @param graphics The graphics in which to draw. * @param shape The polygon to draw. * @param style The style to apply, or <code>null</code> if none. * @param scale The scale denominator for the current zoom level * @throws FactoryException * @throws TransformException */ public void paint( final Graphics2D graphics, final LiteShape2 shape, final Style2D style, final double scale, boolean isLabelObstacle) { if (style == null) { // TODO: what's going on? Should not be reached... LOGGER.severe("ShapePainter has been asked to paint a null style!!"); return; } // Is the current scale within the style scale range? if (!style.isScaleInRange(scale)) { LOGGER.fine("Out of scale"); return; } if (style instanceof IconStyle2D) { AffineTransform temp = graphics.getTransform(); try { IconStyle2D icoStyle = (IconStyle2D) style; Icon icon = icoStyle.getIcon(); graphics.setComposite(icoStyle.getComposite()); // the displacement to be applied to all points, centers the icon and applies the // Graphic displacement as well float dx = icoStyle.getDisplacementX(); float dy = icoStyle.getDisplacementY(); // iterate over all points float[] coords = new float[2]; PathIterator citer = getPathIterator(shape); AffineTransform at = new AffineTransform(temp); while (!(citer.isDone())) { if (citer.currentSegment(coords) != PathIterator.SEG_MOVETO) { at.setTransform(temp); double x = coords[0] + dx; double y = coords[1] + dy; at.translate(x, y); at.rotate(icoStyle.getRotation()); at.translate( -(icon.getIconWidth() * icoStyle.getAnchorPointX()), (icon.getIconHeight() * (icoStyle.getAnchorPointY() - 1))); graphics.setTransform(at); icon.paintIcon(null, graphics, 0, 0); if (isLabelObstacle) { // TODO: rotation? labelCache.put( new Rectangle2D.Double(x, y, icon.getIconWidth(), icon.getIconHeight())); } } citer.next(); } } finally { graphics.setTransform(temp); } } else if (style instanceof MarkStyle2D) { PathIterator citer = getPathIterator(shape); // get the point onto the shape has to be painted float[] coords = new float[2]; MarkStyle2D ms2d = (MarkStyle2D) style; Shape transformedShape; while (!(citer.isDone())) { if (citer.currentSegment(coords) != PathIterator.SEG_MOVETO) { transformedShape = ms2d.getTransformedShape(coords[0], coords[1]); if (transformedShape != null) { if (ms2d.getFill() != null) { graphics.setPaint(ms2d.getFill()); graphics.setComposite(ms2d.getFillComposite()); graphics.fill(transformedShape); } if (ms2d.getContour() != null) { graphics.setPaint(ms2d.getContour()); graphics.setStroke(ms2d.getStroke()); graphics.setComposite(ms2d.getContourComposite()); graphics.draw(transformedShape); } if (isLabelObstacle) { labelCache.put(transformedShape.getBounds2D()); } } } citer.next(); } } else if (style instanceof GraphicStyle2D) { float[] coords = new float[2]; PathIterator iter = getPathIterator(shape); iter.currentSegment(coords); GraphicStyle2D gs2d = (GraphicStyle2D) style; BufferedImage image = gs2d.getImage(); double dx = gs2d.getDisplacementX() - gs2d.getAnchorPointX() * image.getWidth(); double dy = gs2d.getDisplacementY() - ((1 - gs2d.getAnchorPointY()) * image.getHeight()); while (!(iter.isDone())) { if (iter.currentSegment(coords) != PathIterator.SEG_MOVETO) { renderImage( graphics, coords[0], coords[1], dx, dy, image, gs2d.getRotation(), gs2d.getComposite(), isLabelObstacle); } iter.next(); } } else { if (isLabelObstacle) { labelCache.put(shape.getBounds2D()); } // if the style is a polygon one, process it even if the polyline is // not closed (by SLD specification) if (style instanceof PolygonStyle2D) { PolygonStyle2D ps2d = (PolygonStyle2D) style; if (ps2d.getFill() != null) { Paint paint = ps2d.getFill(); if (paint instanceof TexturePaint) { TexturePaint tp = (TexturePaint) paint; BufferedImage image = tp.getImage(); Rectangle2D cornerRect = tp.getAnchorRect(); Point2D anchorPoint = (Point2D) graphics.getRenderingHint(TEXTURE_ANCHOR_HINT_KEY); Rectangle2D alignedRect = null; if (anchorPoint != null) { alignedRect = new Rectangle2D.Double( Math.round(anchorPoint.getX()), Math.round(anchorPoint.getY()), cornerRect.getWidth(), cornerRect.getHeight()); } else { alignedRect = new Rectangle2D.Double(0.0, 0.0, cornerRect.getWidth(), cornerRect.getHeight()); } paint = new TexturePaint(image, alignedRect); } graphics.setPaint(paint); graphics.setComposite(ps2d.getFillComposite()); fillLiteShape(graphics, shape); } if (ps2d.getGraphicFill() != null) { Shape oldClip = graphics.getClip(); try { paintGraphicFill(graphics, shape, ps2d.getGraphicFill(), scale); } finally { graphics.setClip(oldClip); } } } if (style instanceof LineStyle2D) { LineStyle2D ls2d = (LineStyle2D) style; if (ls2d.getStroke() != null) { // see if a graphic stroke is to be used, the drawing method // is completely // different in this case if (ls2d.getGraphicStroke() != null) { drawWithGraphicsStroke( graphics, dashShape(shape, ls2d.getStroke()), ls2d.getGraphicStroke(), isLabelObstacle); } else { Paint paint = ls2d.getContour(); if (paint instanceof TexturePaint) { TexturePaint tp = (TexturePaint) paint; BufferedImage image = tp.getImage(); Rectangle2D rect = tp.getAnchorRect(); AffineTransform at = graphics.getTransform(); double width = rect.getWidth() * at.getScaleX(); double height = rect.getHeight() * at.getScaleY(); Rectangle2D scaledRect = new Rectangle2D.Double(0, 0, width, height); paint = new TexturePaint(image, scaledRect); } // debugShape(shape); Stroke stroke = ls2d.getStroke(); if (graphics.getRenderingHint(RenderingHints.KEY_ANTIALIASING) == RenderingHints.VALUE_ANTIALIAS_ON) { if (stroke instanceof BasicStroke) { BasicStroke bs = (BasicStroke) stroke; stroke = new BasicStroke( bs.getLineWidth() + 0.5f, bs.getEndCap(), bs.getLineJoin(), bs.getMiterLimit(), bs.getDashArray(), bs.getDashPhase()); } } graphics.setPaint(paint); graphics.setStroke(stroke); graphics.setComposite(ls2d.getContourComposite()); graphics.draw(shape); } } } } }
// draws the image along the path private void drawWithGraphicsStroke( Graphics2D graphics, Shape shape, Style2D graphicStroke, boolean isLabelObstacle) { PathIterator pi = shape.getPathIterator(null); double[] coords = new double[4]; int type; // I suppose the image has been already scaled and its square double imageSize; double graphicRotation = 0; // rotation in radians if (graphicStroke instanceof MarkStyle2D) { imageSize = ((MarkStyle2D) graphicStroke).getSize(); graphicRotation = ((MarkStyle2D) graphicStroke).getRotation(); } else if (graphicStroke instanceof IconStyle2D) { imageSize = ((IconStyle2D) graphicStroke).getIcon().getIconWidth(); graphicRotation = ((IconStyle2D) graphicStroke).getRotation(); } else { GraphicStyle2D gs = (GraphicStyle2D) graphicStroke; imageSize = gs.getImage().getWidth() - gs.getBorder(); graphicRotation = ((GraphicStyle2D) graphicStroke).getRotation(); } Composite composite = ((PointStyle2D) graphicStroke).getComposite(); if (composite == null) { composite = AlphaComposite.SrcOver; } double[] first = new double[2]; double[] previous = new double[2]; type = pi.currentSegment(coords); first[0] = coords[0]; first[1] = coords[1]; previous[0] = coords[0]; previous[1] = coords[1]; if (LOGGER.isLoggable(Level.FINEST)) { LOGGER.finest("starting at " + first[0] + "," + first[1]); } pi.next(); double remainder, dx, dy, len; remainder = imageSize / 2.0; while (!pi.isDone()) { type = pi.currentSegment(coords); switch (type) { case PathIterator.SEG_MOVETO: // nothing to do? if (LOGGER.isLoggable(Level.FINEST)) { LOGGER.finest("moving to " + coords[0] + "," + coords[1]); } first[0] = coords[0]; first[1] = coords[1]; remainder = imageSize / 2.0; break; case PathIterator.SEG_CLOSE: // draw back to first from previous coords[0] = first[0]; coords[1] = first[1]; remainder = imageSize / 2.0; if (LOGGER.isLoggable(Level.FINEST)) { LOGGER.finest( "closing from " + previous[0] + "," + previous[1] + " to " + coords[0] + "," + coords[1]); } // no break here - fall through to next section case PathIterator.SEG_LINETO: // draw from previous to coords if (LOGGER.isLoggable(Level.FINEST)) { LOGGER.finest( "drawing from " + previous[0] + "," + previous[1] + " to " + coords[0] + "," + coords[1]); } dx = coords[0] - previous[0]; dy = coords[1] - previous[1]; len = Math.sqrt((dx * dx) + (dy * dy)); // - imageWidth; if (len < remainder) { remainder -= len; } else { double theta = Math.atan2(dx, dy); dx = (Math.sin(theta) * imageSize); dy = (Math.cos(theta) * imageSize); if (LOGGER.isLoggable(Level.FINEST)) { LOGGER.finest( "dx = " + dx + " dy " + dy + " step = " + Math.sqrt((dx * dx) + (dy * dy))); } double rotation = -(theta - (Math.PI / 2d)); double x = previous[0] + (Math.sin(theta) * remainder); double y = previous[1] + (Math.cos(theta) * remainder); if (LOGGER.isLoggable(Level.FINEST)) { LOGGER.finest("len =" + len + " imageSize " + imageSize); } double dist = 0; for (dist = remainder; dist < len; dist += imageSize) { renderGraphicsStroke( graphics, x, y, graphicStroke, rotation, graphicRotation, composite, isLabelObstacle); x += dx; y += dy; } remainder = dist - len; if (LOGGER.isLoggable(Level.FINEST)) { LOGGER.finest("loop end dist " + dist + " len " + len + " " + (len - dist)); } } break; default: LOGGER.warning("default branch reached in drawWithGraphicStroke"); } previous[0] = coords[0]; previous[1] = coords[1]; pi.next(); } }
/** Unsure if this is the paint for the border, or the fill? */ private void paint(ViewportGraphics g, SimpleFeature feature, LiteShape shape, Symbolizer symb) { if (symb instanceof PolygonSymbolizer) { PolygonSymbolizer polySymb = (PolygonSymbolizer) symb; double opacity = SLDs.polyFillOpacity(polySymb); Color fillColor = SLDs.polyFill(polySymb); Stroke stroke = SLDs.stroke(polySymb); Color strokeColor = SLDs.polyColor(polySymb); int width = SLDs.width(stroke); if (width == SLDs.NOTFOUND) width = 1; if (Double.isNaN(opacity)) opacity = 1.0; if (fillColor != null) { fillColor = new Color( fillColor.getRed(), fillColor.getGreen(), fillColor.getBlue(), (int) (255 * opacity)); g.setColor(fillColor); g.fill(shape); } if (stroke != null && strokeColor != null) { Graphics2D g2d = g.getGraphics(Graphics2D.class); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setColor(strokeColor); float[] dashArray = stroke.getDashArray(); Float dashOffset = stroke.getDashOffset().evaluate(null, Float.class); if (dashOffset == null) { dashOffset = 0f; } String cap = stroke.getLineCap().evaluate(null, String.class); int capInt = Utilities.sld2awtCap(cap); String join = stroke.getLineJoin().evaluate(null, String.class); int joinInt = Utilities.sld2awtJoin(join); BasicStroke bStroke = new BasicStroke(width, capInt, joinInt, 1f, dashArray, dashOffset); g2d.setStroke(bStroke); g2d.draw(shape); } } if (symb instanceof LineSymbolizer) { LineSymbolizer lineSymbolizer = (LineSymbolizer) symb; Color c = SLDs.color(lineSymbolizer); int w = SLDs.width(lineSymbolizer); Stroke stroke = SLDs.stroke(lineSymbolizer); if (c != null && w > 0) { Graphics2D g2d = g.getGraphics(Graphics2D.class); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setColor(c); float[] dashArray = stroke.getDashArray(); Float dashOffset = stroke.getDashOffset().evaluate(null, Float.class); if (dashOffset == null) { dashOffset = 0f; } String cap = stroke.getLineCap().evaluate(null, String.class); int capInt = Utilities.sld2awtCap(cap); String join = stroke.getLineJoin().evaluate(null, String.class); int joinInt = Utilities.sld2awtJoin(join); BasicStroke bStroke = new BasicStroke(w, capInt, joinInt, 1f, dashArray, dashOffset); g2d.setStroke(bStroke); g2d.draw(shape); } } if (symb instanceof PointSymbolizer) { PointSymbolizer pointSymbolizer = (PointSymbolizer) symb; AffineTransform transform = g.getTransform(); // offset Point2D offset = Utilities.getOffset(pointSymbolizer); if (offset != null) { java.awt.Point off = new java.awt.Point((int) offset.getX(), -1 * (int) offset.getY()); g.translate(off); } // rotation Graphic graphic = SLDs.graphic(pointSymbolizer); Double rotation = graphic.getRotation().evaluate(null, Double.class); Double gSize = graphic.getSize().evaluate(null, Double.class); if (gSize != null && rotation != null) { g.setTransform( AffineTransform.getRotateInstance(toRadians(rotation), gSize / 2, gSize / 2)); } Color c = SLDs.pointStrokeColorWithAlpha(pointSymbolizer); Color fill = SLDs.pointFillWithAlpha(pointSymbolizer); int width = SLDs.width(SLDs.stroke(pointSymbolizer)); if (width < 0) { width = 0; } float[] point = new float[6]; PathIterator pathIterator = shape.getPathIterator(null); pathIterator.currentSegment(point); SLDStyleFactory styleFactory = new SLDStyleFactory(); Style2D tmp = null; try { tmp = styleFactory.createStyle( feature, pointSymbolizer, new NumberRange(Double.class, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY)); } catch (Exception e) { PointSymbolizerWrapper tmpPs = new PointSymbolizerWrapper(pointSymbolizer, null); tmp = styleFactory.createStyle( feature, pointSymbolizer, new NumberRange(Double.class, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY)); } if (tmp instanceof MarkStyle2D) { MarkStyle2D style = (MarkStyle2D) tmp; Shape shape2 = style.getTransformedShape(point[0], point[1]); if (c == null && fill == null) { // g.setColor(Color.GRAY); // g.fill(shape2); } if (fill != null) { g.setColor(fill); g.fill(shape2); } // else { // g.setColor(Color.GRAY); // g.fill(shape2); // } if (c != null) { g.setStroke(ViewportGraphics.LINE_SOLID, width); g.setColor(c); g.draw(shape2); } // else { // g.setStroke(ViewportGraphics.LINE_SOLID, width); // g.setColor(Color.DARK_GRAY); // g.draw(shape2); // } } else if (tmp instanceof GraphicStyle2D) { GraphicStyle2D style = (GraphicStyle2D) tmp; RenderedImage image = (RenderedImage) style.getImage(); g.drawImage( image, (int) (point[0] - ((double) image.getWidth()) / (double) 2), (int) (point[1] - ((double) image.getHeight()) / (double) 2)); } } }