Java wrap text to polygon

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Josh Argent
    New Member
    • Dec 2010
    • 28

    Java wrap text to polygon

    I am using Java Graphics2D to draw basic and complex polygons which will have text of varying lengths and fonts in it. What I'm trying to achieve is that the text drawn is perfectly wrapped and clipped to fit within the polygon.

    The code I have so far is this:

    Code:
    int[] xp = { x + width /2, x + width -1, x };
    int[] yp = { y, y + height - 1, y + height - 1 };
    g.setColor(fill.color1);
    g.fillPolygon(xp, yp, xp.length);
    g.setColor(border.color);
    g.setStroke(new BasicStroke((float) (border.width * zoom), BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER));
    g.drawPolygon(xp, yp, xp.length);
    
    // Later on in the method..
    g.drawString(text, textx, texty);
    This draws the shape and text fine but the text is just one long line. I want it to fit neatly into the polygon.

    Thanks,
    Josh.
Working...