Ejemplo n.º 1
0
  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()));
      }
    }
  }
Ejemplo n.º 2
0
  /**
   * 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);
          }
        }
      }
    }
  }