< prev index next >

src/java.desktop/share/classes/java/awt/font/LineBreakMeasurer.java

Print this page




  98  * <p>
  99  * In general, if the text used to construct the
 100  * {@code LineBreakMeasurer} changes, a new
 101  * {@code LineBreakMeasurer} must be constructed to reflect
 102  * the change.  (The old {@code LineBreakMeasurer} continues to
 103  * function properly, but it won't be aware of the text change.)
 104  * Nevertheless, if the text change is the insertion or deletion of a
 105  * single character, an existing {@code LineBreakMeasurer} can be
 106  * 'updated' by calling {@code insertChar} or
 107  * {@code deleteChar}. Updating an existing
 108  * {@code LineBreakMeasurer} is much faster than creating a new one.
 109  * Clients who modify text based on user typing should take advantage
 110  * of these methods.
 111  * <p>
 112  * <strong>Examples</strong>:<p>
 113  * Rendering a paragraph in a component
 114  * <blockquote>
 115  * <pre>{@code
 116  * public void paint(Graphics graphics) {
 117  *
 118  *     Point2D pen = new Point2D(10, 20);
 119  *     Graphics2D g2d = (Graphics2D)graphics;
 120  *     FontRenderContext frc = g2d.getFontRenderContext();
 121  *
 122  *     // let styledText be an AttributedCharacterIterator containing at least
 123  *     // one character
 124  *
 125  *     LineBreakMeasurer measurer = new LineBreakMeasurer(styledText, frc);
 126  *     float wrappingWidth = getSize().width - 15;

 127  *
 128  *     while (measurer.getPosition() < fStyledText.length()) {
 129  *
 130  *         TextLayout layout = measurer.nextLayout(wrappingWidth);
 131  *
 132  *         pen.y += (layout.getAscent());
 133  *         float dx = layout.isLeftToRight() ?
 134  *             0 : (wrappingWidth - layout.getAdvance());
 135  *
 136  *         layout.draw(graphics, pen.x + dx, pen.y);
 137  *         pen.y += layout.getDescent() + layout.getLeading();
 138  *     }
 139  * }
 140  * }</pre>
 141  * </blockquote>
 142  * <p>
 143  * Rendering text with tabs.  For simplicity, the overall text
 144  * direction is assumed to be left-to-right
 145  * <blockquote>
 146  * <pre>{@code
 147  * public void paint(Graphics graphics) {
 148  *
 149  *     float leftMargin = 10, rightMargin = 310;
 150  *     float[] tabStops = { 100, 250 };
 151  *
 152  *     // assume styledText is an AttributedCharacterIterator, and the number
 153  *     // of tabs in styledText is tabCount
 154  *
 155  *     int[] tabLocations = new int[tabCount+1];
 156  *
 157  *     int i = 0;




  98  * <p>
  99  * In general, if the text used to construct the
 100  * {@code LineBreakMeasurer} changes, a new
 101  * {@code LineBreakMeasurer} must be constructed to reflect
 102  * the change.  (The old {@code LineBreakMeasurer} continues to
 103  * function properly, but it won't be aware of the text change.)
 104  * Nevertheless, if the text change is the insertion or deletion of a
 105  * single character, an existing {@code LineBreakMeasurer} can be
 106  * 'updated' by calling {@code insertChar} or
 107  * {@code deleteChar}. Updating an existing
 108  * {@code LineBreakMeasurer} is much faster than creating a new one.
 109  * Clients who modify text based on user typing should take advantage
 110  * of these methods.
 111  * <p>
 112  * <strong>Examples</strong>:<p>
 113  * Rendering a paragraph in a component
 114  * <blockquote>
 115  * <pre>{@code
 116  * public void paint(Graphics graphics) {
 117  *
 118  *     float dx = 0f, dy = 5f;
 119  *     Graphics2D g2d = (Graphics2D)graphics;
 120  *     FontRenderContext frc = g2d.getFontRenderContext();
 121  *
 122  *     AttributedString text = new AttributedString(".....");
 123  *     AttributedCharacterIterator paragraph = text.getIterator();
 124  *
 125  *     LineBreakMeasurer measurer = new LineBreakMeasurer(paragraph, frc);
 126  *     measurer.setPosition(paragraph.getBeginIndex());
 127  *     float wrappingWidth = (float)getSize().width;
 128  *
 129  *     while (measurer.getPosition() < paragraph.getEndIndex()) {
 130  *
 131  *         TextLayout layout = measurer.nextLayout(wrappingWidth);
 132  *
 133  *         dy += (layout.getAscent());
 134  *         float dx = layout.isLeftToRight() ?
 135  *             0 : (wrappingWidth - layout.getAdvance());
 136  *
 137  *         layout.draw(graphics, dx, dy);
 138  *         dy += layout.getDescent() + layout.getLeading();
 139  *     }
 140  * }
 141  * }</pre>
 142  * </blockquote>
 143  * <p>
 144  * Rendering text with tabs.  For simplicity, the overall text
 145  * direction is assumed to be left-to-right
 146  * <blockquote>
 147  * <pre>{@code
 148  * public void paint(Graphics graphics) {
 149  *
 150  *     float leftMargin = 10, rightMargin = 310;
 151  *     float[] tabStops = { 100, 250 };
 152  *
 153  *     // assume styledText is an AttributedCharacterIterator, and the number
 154  *     // of tabs in styledText is tabCount
 155  *
 156  *     int[] tabLocations = new int[tabCount+1];
 157  *
 158  *     int i = 0;


< prev index next >