Unicode: test layout : Text Layout « 2D Graphics GUI « Java






Unicode: test layout

Unicode: test layout
     

/*
Java Internationalization
By Andy Deitsch, David Czarnecki

ISBN: 0-596-00019-7
O'Reilly
*/

import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.text.*;
import javax.swing.*;

public class TextLayoutDemo extends JFrame {

  String davidMessage = "David says, \"\u05E9\u05DC\u05D5\u05DD \u05E2\u05D5\u05DC\u05DD\" ";
  String andyMessage = "Andy also says, \"\u05E9\u05DC\u05D5\u05DD \u05E2\u05D5\u05DC\u05DD\" ";
  String textMessage = davidMessage + andyMessage + davidMessage + andyMessage +
                       davidMessage + andyMessage + davidMessage + andyMessage +
                       davidMessage + andyMessage + davidMessage + andyMessage +
                       davidMessage + andyMessage + davidMessage + andyMessage;

  public TextLayoutDemo() {
    super("TextLayoutDemo");
  }

  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();
    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;
    }
  }

  public static void main(String[] args) {
    JFrame frame = new TextLayoutDemo();
    frame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {System.exit(0);}
    });

    frame.pack();
    frame.setVisible(true);
  }
}


           
         
    
    
    
    
  








Related examples in the same category

1.JFreeChart: Draw String DemoJFreeChart: Draw String Demo
2.Unicode displayUnicode display
3.Line break for textlayoutLine break for textlayout
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).