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






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

           
       








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