List of usage examples for java.awt.font LineBreakMeasurer nextLayout
public TextLayout nextLayout(float wrappingWidth)
From source file:Main.java
public static int paintMultilineText(Graphics2D g2d, String text, int textX, int textWidth, int textY, int maxTextLineCount) { FontRenderContext frc = new FontRenderContext(new AffineTransform(), true, false); int fa = g2d.getFontMetrics().getAscent(); if (text.length() == 0) return textY; int currOffset = 0; AttributedString attributedDescription = new AttributedString(text); attributedDescription.addAttribute(TextAttribute.FONT, g2d.getFont()); LineBreakMeasurer lineBreakMeasurer = new LineBreakMeasurer(attributedDescription.getIterator(), frc); int lineCount = 0; while (true) { TextLayout tl = lineBreakMeasurer.nextLayout(textWidth); if (tl == null) break; int charCount = tl.getCharacterCount(); String line = text.substring(currOffset, currOffset + charCount); g2d.drawString(line, textX, textY); textY += fa;//from www . j ava 2 s.c om currOffset += charCount; lineCount++; if ((maxTextLineCount > 0) && (lineCount == maxTextLineCount)) break; } // textY += fh; return textY; }
From source file:Main.java
/** * Returns the {@link Dimension} for the given {@link JTextComponent} * subclass that will show the whole word wrapped text in the given width. * It won't work for styled text of varied size or style, it's assumed that * the whole text is rendered with the {@link JTextComponent}s font. * * @param textComponent the {@link JTextComponent} to calculate the {@link Dimension} for * @param width the width of the resulting {@link Dimension} * @param text the {@link String} which should be word wrapped * @return The calculated {@link Dimension} *///from w w w . j a v a 2 s .c o m public static Dimension getWordWrappedTextDimension(JTextComponent textComponent, int width, String text) { if (textComponent == null) { throw new IllegalArgumentException("textComponent cannot be null"); } if (width < 1) { throw new IllegalArgumentException("width must be 1 or greater"); } if (text == null) { text = textComponent.getText(); } if (text.isEmpty()) { return new Dimension(width, 0); } FontMetrics metrics = textComponent.getFontMetrics(textComponent.getFont()); FontRenderContext rendererContext = metrics.getFontRenderContext(); float formatWidth = width - textComponent.getInsets().left - textComponent.getInsets().right; int lines = 0; String[] paragraphs = text.split("\n"); for (String paragraph : paragraphs) { if (paragraph.isEmpty()) { lines++; } else { AttributedString attributedText = new AttributedString(paragraph); attributedText.addAttribute(TextAttribute.FONT, textComponent.getFont()); AttributedCharacterIterator charIterator = attributedText.getIterator(); LineBreakMeasurer lineMeasurer = new LineBreakMeasurer(charIterator, rendererContext); lineMeasurer.setPosition(charIterator.getBeginIndex()); while (lineMeasurer.getPosition() < charIterator.getEndIndex()) { lineMeasurer.nextLayout(formatWidth); lines++; } } } return new Dimension(width, metrics.getHeight() * lines + textComponent.getInsets().top + textComponent.getInsets().bottom); }
From source file:Utils.java
/** * Renders a paragraph of text (line breaks ignored) to an image (created and returned). * * @param font The font to use/*from w ww . j av a 2s . c o m*/ * @param textColor The color of the text * @param text The message * @param width The width the text should be limited to * @return An image with the text rendered into it */ public static BufferedImage renderTextToImage(Font font, Color textColor, String text, int width) { Hashtable map = new Hashtable(); map.put(TextAttribute.FONT, font); AttributedString attributedString = new AttributedString(text, map); AttributedCharacterIterator paragraph = attributedString.getIterator(); FontRenderContext frc = new FontRenderContext(null, false, false); int paragraphStart = paragraph.getBeginIndex(); int paragraphEnd = paragraph.getEndIndex(); LineBreakMeasurer lineMeasurer = new LineBreakMeasurer(paragraph, frc); float drawPosY = 0; //First time around, just determine the height while (lineMeasurer.getPosition() < paragraphEnd) { TextLayout layout = lineMeasurer.nextLayout(width); // Move it down drawPosY += layout.getAscent() + layout.getDescent() + layout.getLeading(); } BufferedImage image = createCompatibleImage(width, (int) drawPosY); Graphics2D graphics = (Graphics2D) image.getGraphics(); graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); drawPosY = 0; lineMeasurer.setPosition(paragraphStart); while (lineMeasurer.getPosition() < paragraphEnd) { TextLayout layout = lineMeasurer.nextLayout(width); // Move y-coordinate by the ascent of the layout. drawPosY += layout.getAscent(); /* Compute pen x position. If the paragraph is right-to-left, we want to align the TextLayouts to the right edge of the panel. */ float drawPosX; if (layout.isLeftToRight()) { drawPosX = 0; } else { drawPosX = width - layout.getAdvance(); } // Draw the TextLayout at (drawPosX, drawPosY). layout.draw(graphics, drawPosX, drawPosY); // Move y-coordinate in preparation for next layout. drawPosY += layout.getDescent() + layout.getLeading(); } graphics.dispose(); return image; }
From source file:Main.java
public void paint(Graphics g) { String input = "this is a test.this is a test.this is a test.this is a test."; AttributedString attributedString = new AttributedString(input); attributedString.addAttribute(TextAttribute.FONT, (Font) UIManager.get("Label.font")); Color color = (Color) UIManager.get("Label.foreground"); attributedString.addAttribute(TextAttribute.FOREGROUND, color); super.paint(g); Graphics2D g2d = (Graphics2D) g; int width = getSize().width; int x = 10;//from w w w. ja v a 2s. co m int y = 30; AttributedCharacterIterator characterIterator = attributedString.getIterator(); FontRenderContext fontRenderContext = g2d.getFontRenderContext(); LineBreakMeasurer measurer = new LineBreakMeasurer(characterIterator, fontRenderContext); while (measurer.getPosition() < characterIterator.getEndIndex()) { TextLayout textLayout = measurer.nextLayout(width); y += textLayout.getAscent(); textLayout.draw(g2d, x, y); y += textLayout.getDescent() + textLayout.getLeading(); } }
From source file:BasicShapes.java
License:asdf
void drawParagraph(Graphics2D g, String paragraph, float width) { LineBreakMeasurer linebreaker = new LineBreakMeasurer(new AttributedString(paragraph).getIterator(), g.getFontRenderContext());// w ww . j a v a 2 s . c o m float y = 0.0f; while (linebreaker.getPosition() < paragraph.length()) { TextLayout tl = linebreaker.nextLayout(width); y += tl.getAscent(); tl.draw(g, 0, y); y += tl.getDescent() + tl.getLeading(); } }
From source file:Main.java
License:asdf
void drawParagraph(Graphics2D g, String paragraph, float width) { LineBreakMeasurer linebreaker = new LineBreakMeasurer(new AttributedString(paragraph).getIterator(), g.getFontRenderContext());/*from www .j a va2 s. c o m*/ int y = 0; while (linebreaker.getPosition() < paragraph.length()) { TextLayout textLayout = linebreaker.nextLayout(width); y += textLayout.getAscent(); textLayout.draw(g, 0, y); y += textLayout.getDescent() + textLayout.getLeading(); } }
From source file:LineBreakMeasurerDemo.java
public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; attribCharIterator = attribString.getIterator(); FontRenderContext frc = new FontRenderContext(null, false, false); LineBreakMeasurer lbm = new LineBreakMeasurer(attribCharIterator, frc); int x = 10, y = 20; int w = getWidth(); float wrappingWidth = w - 15; while (lbm.getPosition() < text.length()) { TextLayout layout = lbm.nextLayout(wrappingWidth); y += layout.getAscent();/*w ww .j a v a 2 s . com*/ layout.draw(g2, x, y); y += layout.getDescent() + layout.getLeading(); } }
From source file:Main.java
public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; attribCharIterator = attribString.getIterator(); FontRenderContext frc = new FontRenderContext(null, false, false); LineBreakMeasurer lbm = new LineBreakMeasurer(attribCharIterator, frc); int x = 10, y = 20; int w = getWidth(); float wrappingWidth = w - 15; while (lbm.getPosition() < text.length()) { TextLayout layout = lbm.nextLayout(wrappingWidth); y += layout.getAscent();/*from ww w .j av a 2 s . c o m*/ layout.draw(g2, x, y); y += layout.getDescent() + layout.getLeading(); } }
From source file:MainClass.java
private void getLayouts(Graphics g) { layouts = new ArrayList(); Graphics2D g2d = (Graphics2D) g; FontRenderContext frc = g2d.getFontRenderContext(); AttributedString attrStr = new AttributedString(text); attrStr.addAttribute(TextAttribute.FONT, font, 0, text.length()); LineBreakMeasurer measurer = new LineBreakMeasurer(attrStr.getIterator(), frc); float wrappingWidth; wrappingWidth = getSize().width - 15; while (measurer.getPosition() < text.length()) { TextLayout layout = measurer.nextLayout(wrappingWidth); layouts.add(layout);// www. ja v a 2 s. com } }
From source file:ParagraphLayout.java
public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); String s = "Java components and products Directory for Java components " + "and applications.Hundreds of Java components and applications " + "are organized by topic. You can find what you need easily. " + "You may also compare your product with others. If your component " + "is not listed, just send your url to java2s@java2s.com. " + "http://www.java2s.com"; Font font = new Font("Serif", Font.PLAIN, 24); AttributedString as = new AttributedString(s); as.addAttribute(TextAttribute.FONT, font); AttributedCharacterIterator aci = as.getIterator(); FontRenderContext frc = g2.getFontRenderContext(); LineBreakMeasurer lbm = new LineBreakMeasurer(aci, frc); Insets insets = getInsets();/*from w w w . ja v a2 s .c o m*/ float wrappingWidth = getSize().width - insets.left - insets.right; float x = insets.left; float y = insets.top; while (lbm.getPosition() < aci.getEndIndex()) { TextLayout textLayout = lbm.nextLayout(wrappingWidth); y += textLayout.getAscent(); textLayout.draw(g2, x, y); y += textLayout.getDescent() + textLayout.getLeading(); x = insets.left; } }