Exemple #1
0
  /**
   * 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);
        }
      }
    }
  }
  /**
   * 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);
          }
        }
      }
    }
  }