Troubleshooting

   

Justifying Text

How do I left-, center-, right-, and equally- (both left and right) justify text using Java 2D's AttributedString , LineBreakMeasurer , and TextLayout classes?

Sun's online Java 2D text tutorial discusses justification. You can read this tutorial, by pointing your browser to the following Web address:

 http://developer.java.sun.com/developer/onlineTraining/Media/2DText/other.html#just 

Listing 18.30 presents source code to a Justify application that demonstrates left-, center-, right-, and equality-justification. (Set the justify variable to the appropriate justification constant and watch the results.)

Listing 18.30 The Justify Application Source Code
 // Justify.java import java.awt.*; import java.awt.font.*; import java.text.*; import java.util.*; public class Justify extends java.applet.Applet {    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 plagiarize or not to plagiarize William " +                  "Shakespeare, that is the question!  Whether " +                  "'tis nobler in the mind to suffer the lack of " +                  "ideas for a decent paragraph, or to take arms " +                  "against Mr. Shakespeare by plagiarizing his work " +                  "... 'tis a consummation devoutly to be wished!";        Hashtable map = new Hashtable ();       map.put (TextAttribute.SIZE, new Float (18.0f));        AttributedString as = new AttributedString (s, map);        map = new Hashtable ();       map.put (TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE);        as.addAttributes (map, 4, 21);        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 ();       }    } } 

Equality justification requires a developer to do two things: First, an attributed string that is to be justified must not include a TextAttribute.JUSTIFICATION attribute with a value set to TextAttribute.JUSTIFICATION_NONE. Second, TextLayout 's getJustifiedLayout method must be called with the width of the justified line. Figure 18.31 shows the result.

Figure 18.31. Equality justification is used to left- and right-justify text.

graphics/18fig31.gif

Caution

When run under Windows 98, the previous code generates intermittent exception access violations. The error message indicates that these violations are detected in native code outside of the JVM. If the problem is not Windows- related , it's resulting from one of the JVM support DLLs.


   


Special Edition Using Java 2 Standard Edition
Special Edition Using Java 2, Standard Edition (Special Edition Using...)
ISBN: 0789724685
EAN: 2147483647
Year: 1999
Pages: 353

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net