TextLayout: getVisibleAdvance() : TextLayout « java.awt.font « Java by API

Java by API
1. com.sun.image.codec.jpeg
2. java.applet
3. java.awt
4. java.awt.datatransfer
5. java.awt.dnd
6. java.awt.event
7. java.awt.font
8. java.awt.geom
9. java.awt.image
10. java.awt.print
11. java.beans
12. java.io
13. java.lang
14. java.lang.instrument
15. java.lang.management
16. java.lang.reflect
17. java.math
18. java.net
19. java.nio
20. java.nio.channels
21. java.nio.charset
22. java.rmi.server
23. java.security
24. java.security.cert
25. java.sql
26. java.text
27. java.util
28. java.util.concurrent
29. java.util.logging
30. java.util.prefs
31. java.util.regex
32. java.util.zip
33. javax.accessibility
34. javax.comm
35. javax.naming
36. javax.net
37. javax.net.ssl
38. javax.print
39. javax.servlet
40. javax.servlet.http
41. javax.sql
42. javax.sql.rowset
43. javax.swing
44. javax.swing.border
45. javax.swing.colorchooser
46. javax.swing.event
47. javax.swing.filechooser
48. javax.swing.plaf.basic
49. javax.swing.plaf.metal
50. javax.swing.plaf.synth
51. javax.swing.table
52. javax.swing.text
53. javax.swing.text.html
54. javax.swing.text.html.parser
55. javax.swing.tree
56. javax.swing.undo
57. javax.xml
58. javax.xml.parsers
59. javax.xml.transform
60. javax.xml.transform.dom
61. javax.xml.transform.stream
62. javax.xml.validation
63. javax.xml.xpath
64. org.apache.commons.lang
65. org.apache.commons.lang.builder
66. org.apache.commons.lang.exception
67. org.apache.commons.lang.time
68. org.apache.commons.logging
69. org.apache.commons.math
70. org.eclipse.jface.action
71. org.eclipse.jface.dialogs
72. org.eclipse.jface.operation
73. org.eclipse.jface.viewers
74. org.eclipse.jface.window
75. org.eclipse.jface.wizard
76. org.eclipse.swt
77. org.eclipse.swt.browser
78. org.eclipse.swt.custom
79. org.eclipse.swt.dnd
80. org.eclipse.swt.events
81. org.eclipse.swt.graphics
82. org.eclipse.swt.layout
83. org.eclipse.swt.ole.win32
84. org.eclipse.swt.printing
85. org.eclipse.swt.program
86. org.eclipse.swt.widgets
87. org.w3c.dom
88. org.xml.sax
89. org.xml.sax.helpers
90. sun.audio
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java by API » java.awt.font » TextLayout 
TextLayout: getVisibleAdvance()

import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
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.JFrame;
import javax.swing.JPanel;

public class MainClass {
  public static void main(String[] args) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container c = f.getContentPane();
    c.add(new Justify());
    f.pack();
    f.setVisible(true);
  }
}

class Justify extends JPanel {
  final static int LEFT = 0;

  final static int RIGHT = 1;

  final static int CENTER = 2;

  final static int EQUALITY = 3;

  int justify = EQUALITY;

  public void paint(Graphics g) {
    Dimension size = getSize();

    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, 3352);

    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 = (floatsize.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()) - layout.getAdvance();
        break;

      case EQUALITY:
        layout = layout.getJustifiedLayout(wrappingWidth);
      }

      layout.draw((Graphics2Dg, X, Y);

      Y += layout.getDescent() + layout.getLeading();
    }
  }
}

           
       
Related examples in the same category
1. new TextLayout(String string, Font font, FontRenderContext frc)
2. TextLayout: draw(Graphics2D g2, float x, float y)
3. TextLayout: getAdvance()
4. TextLayout: getAscent()
5. TextLayout: getDescent()
6. TextLayout: getJustifiedLayout(float justificationWidth)
7. TextLayout: getLeading()
8. TextLayout: hitTestChar(float x, float y)
9. TextLayout: isLeftToRight()
w___w___w_.__j__a_v_a_2s.c_o_m
Home | Contact Us
Copyright 2003 - 07 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.