By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,648 Members | 1,528 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,648 IT Pros & Developers. It's quick & easy.

Java wrap text to polygon

P: 28
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:

Expand|Select|Wrap|Line Numbers
  1. int[] xp = { x + width /2, x + width -1, x };
  2. int[] yp = { y, y + height - 1, y + height - 1 };
  3. g.setColor(fill.color1);
  4. g.fillPolygon(xp, yp, xp.length);
  5. g.setColor(border.color);
  6. g.setStroke(new BasicStroke((float) (border.width * zoom), BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER));
  7. g.drawPolygon(xp, yp, xp.length);
  9. // Later on in the method..
  10. 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.

Sep 16 '13 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.