Menu

LabelRenderer bug?

Help
Gary Yngve
2007-03-13
2013-05-29
  • Gary Yngve

    Gary Yngve - 2007-03-13

    The first line of render(VisualItem item) calls getShape(), which doesn't necessarily return a RectangularShape.  Is it supposed to call getRawShape() instead?  Or does getShape() need to be rewritten to always return a RectangularShape()?

    In my case, I have polygon nodes and am trying to subclass LabelRenderer to draw rotated labels that are decorating said nodes...

     
    • Björn Kruse

      Björn Kruse - 2007-03-13

      If you need labels on polygons, I'd use a polygonrenderer for your nodes, which will draw the polygons. Additionally, add decorators to your nodes and use a labelrenderer for the decorators. See TreeMap demo for a sample use of decorators or come back with further questions ;-)

       
    • Gary Yngve

      Gary Yngve - 2007-05-09

      Forgot about this thread... I did what I wanted much more simply... extending LabelRenderer and overriding:

      public void render(Graphics2D g, VisualItem item) {
        AffineTransform at=g.getTransform();
        ... // calculate theta
        g.rotate(theta,item.getX(),item.getY());
        super.render(g,item);
        g.setTransform(at);
      }

      But there's still an issue with the API.  Here it is:
      LabelRenderer.render calls getShape() but casts Shape into RectangularShape.
      LabelRenderer gets away with it by having getRawShape return a bounding box (a RectangularShape).
      There's nothing stopping the user from overriding getTransform.  A nonnull transform causes
      AbstractRenderer.getShape() to call AffineTransform.createTransformedShape(Shape), which does not
      necessarily return a RectangularShape... in this case, a Path2D, I believe.

      If there's no need to ever override getTransform for a class derived from LabelRenderer (which there may not be), maybe it's best to have LabelRenderer have
      protected final java.awt.geom.AffineTransform getTransform(VisualItem item) { return null; }

       

Log in to post a comment.