List of usage examples for java.text AttributedCharacterIterator getEndIndex
public int getEndIndex();
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// ww w .ja v a 2s . c om * @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
/** * 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} */// w w w .j a v a 2s . co 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:LineBreakSample.java
public LineBreakSample() { AttributedCharacterIterator paragraph = vanGogh.getIterator(); paragraphStart = paragraph.getBeginIndex(); paragraphEnd = paragraph.getEndIndex(); // Create a new LineBreakMeasurer from the paragraph. lineMeasurer = new LineBreakMeasurer(paragraph, new FontRenderContext(null, false, false)); }
From source file:MainClass.java
public void paint(Graphics g) { Dimension size = getSize();/*from www . j av a 2 s.c o m*/ String s = "To java2s.com or not to java2s.com, that is a question"; Hashtable map = new Hashtable(); map.put(TextAttribute.SIZE, new Float(32.0f)); AttributedString as = new AttributedString(s, map); map = new Hashtable(); map.put(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE); as.addAttributes(map, 33, 52); AttributedCharacterIterator aci = as.getIterator(); int startIndex = aci.getBeginIndex(); int endIndex = aci.getEndIndex(); LineBreakMeasurer measurer; measurer = new LineBreakMeasurer(aci, new FontRenderContext(null, false, false)); measurer.setPosition(startIndex); float wrappingWidth = (float) size.width; float Y = 0.0f; while (measurer.getPosition() < endIndex) { TextLayout layout = measurer.nextLayout(wrappingWidth); Y += layout.getAscent(); float X = 0.0f; switch (justify) { case LEFT: if (layout.isLeftToRight()) X = 0.0f; else X = wrappingWidth - layout.getAdvance(); break; case RIGHT: if (layout.isLeftToRight()) X = wrappingWidth - layout.getVisibleAdvance(); else X = wrappingWidth; break; case CENTER: if (layout.isLeftToRight()) X = (wrappingWidth - layout.getVisibleAdvance()) / 2; else X = (wrappingWidth + layout.getAdvance()) / 2 - layout.getAdvance(); break; case EQUALITY: layout = layout.getJustifiedLayout(wrappingWidth); } layout.draw((Graphics2D) g, X, Y); Y += layout.getDescent() + layout.getLeading(); } }
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 ww w . j av a2 s .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:com.sander.verhagen.frame.LineWrapCellRenderer.java
private int getLineCount(JTextArea textArea) { AttributedString string = new AttributedString(textArea.getText()); FontRenderContext fontRenderContext = textArea.getFontMetrics(textArea.getFont()).getFontRenderContext(); AttributedCharacterIterator characterIterator = string.getIterator(); LineBreakMeasurer lineBreakMeasurer = new LineBreakMeasurer(characterIterator, fontRenderContext); lineBreakMeasurer.setPosition(characterIterator.getBeginIndex()); int lineCount = 0; while (lineBreakMeasurer.getPosition() < characterIterator.getEndIndex()) { lineBreakMeasurer.nextLayout(textArea.getSize().width); lineCount++;// ww w . ja v a2 s .c o m } return lineCount; }
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.j a v a 2s . 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; } }
From source file:TextLayoutLineBreakerMeasurer.java
public void paint(Graphics g) { Graphics2D graphics2D = (Graphics2D) g; GraphicsEnvironment.getLocalGraphicsEnvironment(); Font font = new Font("LucidaSans", Font.PLAIN, 14); AttributedString messageAS = new AttributedString(m); messageAS.addAttribute(TextAttribute.FONT, font); AttributedCharacterIterator messageIterator = messageAS.getIterator(); FontRenderContext messageFRC = graphics2D.getFontRenderContext(); LineBreakMeasurer messageLBM = new LineBreakMeasurer(messageIterator, messageFRC); Insets insets = getInsets();/* w w w . j a v a 2 s.c o m*/ float wrappingWidth = getSize().width - insets.left - insets.right; float x = insets.left; float y = insets.top; while (messageLBM.getPosition() < messageIterator.getEndIndex()) { TextLayout textLayout = messageLBM.nextLayout(wrappingWidth); y += textLayout.getAscent(); textLayout.draw(graphics2D, x, y); y += textLayout.getDescent() + textLayout.getLeading(); x = insets.left; } }
From source file:TextLayoutDemo.java
public void paint(Graphics g) { Graphics2D graphics2D = (Graphics2D) g; GraphicsEnvironment.getLocalGraphicsEnvironment(); Font font = new Font("LucidaSans", Font.PLAIN, 14); AttributedString messageAS = new AttributedString(textMessage); messageAS.addAttribute(TextAttribute.FONT, font); AttributedCharacterIterator messageIterator = messageAS.getIterator(); FontRenderContext messageFRC = graphics2D.getFontRenderContext(); LineBreakMeasurer messageLBM = new LineBreakMeasurer(messageIterator, messageFRC); Insets insets = getInsets();/*from w w w . j a va 2 s. co m*/ float wrappingWidth = getSize().width - insets.left - insets.right; float x = insets.left; float y = insets.top; while (messageLBM.getPosition() < messageIterator.getEndIndex()) { TextLayout textLayout = messageLBM.nextLayout(wrappingWidth); y += textLayout.getAscent(); textLayout.draw(graphics2D, x, y); y += textLayout.getDescent() + textLayout.getLeading(); x = insets.left; } }
From source file:forge.view.arcane.util.OutlinedLabel.java
/** {@inheritDoc} */ @Override//from ww w . ja va 2 s . c om public final void paint(final Graphics g) { if (getText().length() == 0) { return; } Dimension size = getSize(); // // if( size.width < 50 ) { // g.setColor(Color.cyan); // g.drawRect(0, 0, size.width-1, size.height-1); // } Graphics2D g2d = (Graphics2D) g; g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); int textX = outlineSize, textY = 0; int wrapWidth = Math.max(0, wrap ? size.width - outlineSize * 2 : Integer.MAX_VALUE); final String text = getText(); AttributedString attributedString = new AttributedString(text); if (!StringUtils.isEmpty(text)) { attributedString.addAttribute(TextAttribute.FONT, getFont()); } AttributedCharacterIterator charIterator = attributedString.getIterator(); FontRenderContext fontContext = g2d.getFontRenderContext(); LineBreakMeasurer measurer = new LineBreakMeasurer(charIterator, BreakIterator.getWordInstance(Locale.ENGLISH), fontContext); int lineCount = 0; while (measurer.getPosition() < charIterator.getEndIndex()) { measurer.nextLayout(wrapWidth); lineCount++; if (lineCount > 2) { break; } } charIterator.first(); // Use char wrap if word wrap would cause more than two lines of text. if (lineCount > 2) { measurer = new LineBreakMeasurer(charIterator, BreakIterator.getCharacterInstance(Locale.ENGLISH), fontContext); } else { measurer.setPosition(0); } while (measurer.getPosition() < charIterator.getEndIndex()) { TextLayout textLayout = measurer.nextLayout(wrapWidth); float ascent = textLayout.getAscent(); textY += ascent; // Move down to baseline. g2d.setColor(outlineColor); g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.8f)); textLayout.draw(g2d, textX + outlineSize, textY - outlineSize); textLayout.draw(g2d, textX + outlineSize, textY + outlineSize); textLayout.draw(g2d, textX - outlineSize, textY - outlineSize); textLayout.draw(g2d, textX - outlineSize, textY + outlineSize); g2d.setColor(getForeground()); g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f)); textLayout.draw(g2d, textX, textY); // Move down to top of next line. textY += textLayout.getDescent() + textLayout.getLeading(); } }