if (rectMeasurer == null) {
AttributedCharacterIterator paragraph = attStr.getIterator();
paragraphStart = paragraph.getBeginIndex();
paragraphEnd = paragraph.getEndIndex();
FontRenderContext frc = g2d.getFontRenderContext();
rectMeasurer = new LineBreakMeasurer(paragraph, frc);
}
// Set break width to width of Component.
breakWidth = itModel.getTextBoxWidth();
// Set position to the index of the first character in the paragraph.
rectMeasurer.setPosition(paragraphStart);
// make a copy of measurer and loop through to get dimensions
// calculate the rectangle size ant set variables
LineBreakMeasurer lmCopy = rectMeasurer;
// Get lines until the entire paragraph has been displayed.
while (lmCopy.getPosition() < paragraphEnd) {
// Retrieve next layout. A cleverer program would also cache
// these layouts until the component is re-sized.
TextLayout layout = lmCopy.nextLayout(breakWidth);
// Compute pen x position. If the paragraph is right-to-left we
// will align the TextLayouts to the right edge of the panel.
// Note: this won't occur for the English text in this sample.
// Note: drawPosX is always where the LEFT of the text is placed.
// TODO adjust the RightToLeft case with destx also
// LeftToRight has been fixed
rectPosX = layout.isLeftToRight()
? (0 + destx) : breakWidth - layout.getAdvance();
// Move y-coordinate by the ascent of the layout.
rectPosY += layout.getAscent();
// Draw the TextLayout at (drawPosX, drawPosY).
// layout.draw(g2d, drawPosX, drawPosY);
// Move y-coordinate in preparation for next layout.
rectPosY += layout.getDescent() + layout.getLeading();
// add the y move to rectangle height
System.out.println("rectHeight was " + rectHeight);
rectHeight += layout.getAscent() + layout.getDescent() +
layout.getLeading();
System.out.println("rectHeight is now " + rectHeight);
// if text line width is greater than rectWidth
// set rectWidth to line width
if (layout.getAdvance() > rectWidth) {
rectWidth = layout.getAdvance();
}
}
System.out.println("rectangle position and size= " +
rectPosX + ", " + desty + ", " +
rectWidth + ", " + rectHeight);
// draw a rectangle around text with padding
g2d.fill(new RoundRectangle2D.Double((destx - 4) , (desty - 4),
(rectWidth + 8),
(rectHeight + 8), 10, 10));
// finish
lmCopy = null;
rectMeasurer = null;
// end text rectangle
// color for text
g2d.setColor(itModel.getColor());
// Create a new LineBreakMeasurer from the paragraph.
// It will be cached and re-used.
if (lineMeasurer == null) {
AttributedCharacterIterator paragraph = attStr.getIterator();
paragraphStart = paragraph.getBeginIndex();
paragraphEnd = paragraph.getEndIndex();
FontRenderContext frc = g2d.getFontRenderContext();
lineMeasurer = new LineBreakMeasurer(paragraph, frc);
}
// Set break width to width of Component.
breakWidth = itModel.getTextBoxWidth();
float drawPosY = desty;