Line break for textlayout : Text Layout « 2D Graphics GUI « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » 2D Graphics GUI » Text Layout 




Line break for textlayout
Line break for textlayout
     

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.font.FontRenderContext;
import java.awt.font.LineBreakMeasurer;
import java.awt.font.TextAttribute;
import java.awt.font.TextLayout;
import java.text.AttributedCharacterIterator;
import java.text.AttributedString;
import java.util.Hashtable;

import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class LineBreakSample extends JPanel {

  private LineBreakMeasurer lineMeasurer;

  // the first character in the paragraph.
  private int paragraphStart;

  // the first character after the end of the paragraph.
  private int paragraphEnd;

  private static final Hashtable map = new Hashtable();
  static {
    map.put(TextAttribute.SIZE, new Float(18.0));
  }

  private static AttributedString vanGogh = new AttributedString(
      "Many people believe that Vincent van Gogh painted his best works "
          "during the two-year period he spent in Provence. Here is where he "
          "painted The Starry Night--which some consider to be his greatest "
          "work of all. However, as his artistic brilliance reached new heights "
          "in Provence, his physical and mental health plummeted. ",
      map);

  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));
  }
  public void paintComponent(Graphics g) {

    super.paintComponent(g);
    setBackground(Color.white);

    Graphics2D graphics2D = (Graphics2Dg;

    // Set formatting width to width of Component.
    Dimension size = getSize();
    float formatWidth = (floatsize.width;

    float drawPosY = 0;

    lineMeasurer.setPosition(paragraphStart);

    // Get lines from lineMeasurer until the entire
    // paragraph has been displayed.
    while (lineMeasurer.getPosition() < paragraphEnd) {

      // Retrieve next layout.
      TextLayout layout = lineMeasurer.nextLayout(formatWidth);
      // 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 = formatWidth - layout.getAdvance();
      }

      // Draw the TextLayout at (drawPosX, drawPosY).
      layout.draw(graphics2D, drawPosX, drawPosY);

      // Move y-coordinate in preparation for next layout.
      drawPosY += layout.getDescent() + layout.getLeading();
    }
  }
  public static void main(String[] args) {
    JFrame f = new JFrame("");

    f.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });

    LineBreakSample controller = new LineBreakSample();
    f.getContentPane().add(controller,"Center");
    f.setSize(new Dimension(400250));
    f.setVisible(true);
  }
}


           
         
    
    
    
    
  














Related examples in the same category
1.JFreeChart: Draw String DemoJFreeChart: Draw String Demo
2.Unicode: test layoutUnicode: test layout
3.Unicode displayUnicode display
4.Mouse hit and textlayoutMouse hit and textlayout
5.TextLayout demoTextLayout demo
6.Draw text along a curveDraw text along a curve
7.TextHitInfo Demo: tell you which is the letter you are clickingTextHitInfo Demo: tell you which is the letter you are clicking
8.TextAttribute: Underline and strike throughTextAttribute: Underline and strike through
9.TextAttribute: color and fontTextAttribute: color and font
10.Hightlight text by drag and selectionHightlight text by drag and selection
11.LineMetrics: the metrics to layout characters along a lineLineMetrics: the metrics to layout characters along a line
12.Paragraph LayoutParagraph Layout
13.Caret actionCaret action
14.Caret and TextLayoutCaret and TextLayout
15.Another Line Break Demo
16.A display of text, formatted by us instead of by AWT/SwingA display of text, formatted by us instead of by AWT/Swing
17.Draw text with TextLayout
18.Drawing a Paragraph of Text
19.Getting the Shape from the Outline of Text
20.Drawing Text with Mixed Styles
21.Drawing multi-line text with AttributedString and LineBreakMeasurer
22.Draws the string at the specified location underlining the specified character
23.Renders a paragraph of text (line breaks ignored) to an image (created and returned).
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.