[Imagetools-commit] SF.net SVN: imagetools:[74] trunk/imagetools/src/net/codebuilders/desktop/ imag
Status: Beta
Brought to you by:
cmarcum
From: <cm...@us...> - 2009-07-25 14:21:34
|
Revision: 74 http://imagetools.svn.sourceforge.net/imagetools/?rev=74&view=rev Author: cmarcum Date: 2009-07-25 14:21:26 +0000 (Sat, 25 Jul 2009) Log Message: ----------- ticket:13 - added white rounded rectangle background behind text annotations. Modified Paths: -------------- trunk/imagetools/src/net/codebuilders/desktop/imagetools/ImageArea.java Modified: trunk/imagetools/src/net/codebuilders/desktop/imagetools/ImageArea.java =================================================================== --- trunk/imagetools/src/net/codebuilders/desktop/imagetools/ImageArea.java 2009-07-24 21:58:37 UTC (rev 73) +++ trunk/imagetools/src/net/codebuilders/desktop/imagetools/ImageArea.java 2009-07-25 14:21:26 UTC (rev 74) @@ -29,6 +29,7 @@ import java.awt.font.LineBreakMeasurer; import java.awt.font.TextAttribute; import java.awt.font.TextLayout; +import java.awt.geom.RoundRectangle2D; import java.text.AttributedCharacterIterator; import java.text.AttributedString; import java.beans.PropertyChangeEvent; @@ -86,7 +87,6 @@ * for drawing text */ private Point2D pointSelection; - /** * Ready to crop, usually after rectangle selection */ @@ -103,6 +103,9 @@ MouseMotionListener mml; // The LineBreakMeasurer used to line-break the paragraph. private LineBreakMeasurer lineMeasurer; + // The LineBreakMeasurer used to get the size the paragraph + // for the background rectangle + private LineBreakMeasurer rectMeasurer; private float breakWidth = (float) 200.0; // index of the first character in the paragraph. private int paragraphStart; @@ -763,6 +766,13 @@ int width = image.getWidth(null); int height = image.getHeight(null); + // variables for text background rectangle + double rectPosX = destx; + double rectPosY = desty; + double rectWidth = 0; + double rectHeight = 0; + + BufferedImage biNew = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g2d = biNew.createGraphics(); @@ -780,6 +790,78 @@ } + // begin text background rectangle + + // color for background of text + g2d.setColor(Color.WHITE); + + // Create a new LineBreakMeasurer from the paragraph. + // It will be cached and re-used. + 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. @@ -798,10 +880,12 @@ // Set position to the index of the first character in the paragraph. lineMeasurer.setPosition(paragraphStart); + + // Get lines until the entire paragraph has been displayed. while (lineMeasurer.getPosition() < paragraphEnd) { - // Retrieve next layout. A clever program would also cache + // Retrieve next layout. A cleverer program would also cache // these layouts until the component is re-sized. TextLayout layout = lineMeasurer.nextLayout(breakWidth); @@ -823,8 +907,12 @@ // Move y-coordinate in preparation for next layout. drawPosY += layout.getDescent() + layout.getLeading(); + + } + + setImage(biNew, true); g2d.dispose(); lineMeasurer = null; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |