Java Code Examples for java.awt.font.TextLayout.draw()

The following are Jave code examples for showing how to use draw() of the java.awt.font.TextLayout class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: jdk8u-jdk   File: SunGraphics2D.java   Source Code and License Vote up 6 votes
public void drawString(AttributedCharacterIterator iterator,
                       float x, float y) {
    if (iterator == null) {
        throw new NullPointerException("AttributedCharacterIterator is null");
    }
    if (iterator.getBeginIndex() == iterator.getEndIndex()) {
        return; /* nothing to draw */
    }
    TextLayout tl = new TextLayout(iterator, getFontRenderContext());
    tl.draw(this, x, y);
}
 
Example 2
Project: pdfbox-graphics2d   File: PdfBoxGraphics2D.java   Source Code and License Vote up 6 votes
private void drawStringUsingShapes(AttributedCharacterIterator iterator, float x, float y) {
	Stroke originalStroke = stroke;
	Paint originalPaint = paint;
	TextLayout textLayout = new TextLayout(iterator, getFontRenderContext());
	textLayout.draw(this, x, y);
	paint = originalPaint;
	stroke = originalStroke;
}
 
Example 3
Project: litiengine   File: SpeechBubble.java   Source Code and License Vote up 5 votes
@Override
public void render(final Graphics2D g) {
  if (this.displayedText == null || this.displayedText.isEmpty() || !Game.getRenderEngine().canRender(this.entity)) {
    return;
  }

  final Point2D location = Game.getCamera().getViewPortLocation(this.entity);
  RenderEngine.renderImage(g, this.bubble, new Point2D.Double(location.getX() + this.entity.getWidth() / 2.0 - this.textBoxWidth / 2.0 - PADDING, location.getY() - this.height - PADDING));

  g.setColor(SPEAK_FONT_COLOR);
  final FontRenderContext frc = g.getFontRenderContext();

  final String text = this.displayedText;
  final AttributedString styledText = new AttributedString(text);
  styledText.addAttribute(TextAttribute.FONT, this.font);
  final AttributedCharacterIterator iterator = styledText.getIterator();
  final LineBreakMeasurer measurer = new LineBreakMeasurer(iterator, frc);
  measurer.setPosition(0);
  final float x = (float) Game.getCamera().getViewPortLocation(this.entity).getX() + this.entity.getWidth() / 2.0f - this.textBoxWidth / 2.0f;
  float y = (float) Game.getCamera().getViewPortLocation(this.entity).getY() - this.height;
  while (measurer.getPosition() < text.length()) {
    final TextLayout layout = measurer.nextLayout(this.textBoxWidth);

    y += layout.getAscent();
    final float dx = layout.isLeftToRight() ? 0 : this.textBoxWidth - layout.getAdvance();
    layout.draw(g, x + dx, y);
    y += layout.getDescent() + layout.getLeading();
  }
}
 
Example 4
Project: FreeCol   File: FreeColToolTipUI.java   Source Code and License Vote up 5 votes
@Override
public void paint(Graphics g, JComponent c) {
    if (c.isOpaque()) {
        ImageLibrary.drawTiledImage("image.background.FreeColToolTip", g, c, null);
    }

    g.setColor(Color.BLACK); // FIXME: find out why this is necessary

    Graphics2D graphics = (Graphics2D)g;
    float x = margin;
    float y = margin;
    for (String line : lineBreak.split(((JToolTip) c).getTipText())) {
        if (line.isEmpty()) {
            y += LEADING;
            continue;
        }
        AttributedCharacterIterator styledText =
            new AttributedString(line).getIterator();

        LineBreakMeasurer measurer = new LineBreakMeasurer(styledText, frc);

        while (measurer.getPosition() < styledText.getEndIndex()) {

            TextLayout layout = measurer.nextLayout(maximumWidth);

            y += (layout.getAscent());
            float dx = layout.isLeftToRight() ?
                0 : (maximumWidth - layout.getAdvance());

            layout.draw(graphics, x + dx, y);
            y += layout.getDescent() + layout.getLeading();
        }
    }
 }
 
Example 5
Project: incubator-netbeans   File: HighlightsViewUtils.java   Source Code and License Vote up 5 votes
static void paintTextLayout(Graphics2D g, Rectangle2D textLayoutBounds,
        TextLayout textLayout, DocumentView docView)
{
    float x = (float) textLayoutBounds.getX();
    float ascentedY = (float) (textLayoutBounds.getY() + docView.op.getDefaultAscent());
    // TextLayout is unable to do a partial render
    // Both x and ascentedY should already be floor/ceil-ed
    textLayout.draw(g, x, ascentedY);
}
 
Example 6
Project: openjdk-jdk10   File: FontPanel.java   Source Code and License Vote up 5 votes
private void tlDrawLine( Graphics2D g2, TextLayout tl,
                                   float baseX, float baseY ) {
    /// ABP - keep track of old tform, restore it later
    AffineTransform oldTx = null;
    oldTx = g2.getTransform();
    g2.translate( baseX, baseY );
    g2.transform( getAffineTransform( g2Transform ) );

    tl.draw( g2, (float) 0, (float) 0 );

    /// ABP - restore old tform
    g2.setTransform ( oldTx );

}
 
Example 7
Project: openjdk-jdk10   File: GlyphListPipe.java   Source Code and License Vote up 5 votes
public void drawChars(SunGraphics2D sg2d,
                      char data[], int offset, int length,
                      int ix, int iy)
{
    FontInfo info = sg2d.getFontInfo();
    float x, y;
    if (info.pixelHeight > OutlineTextRenderer.THRESHHOLD) {
        SurfaceData.outlineTextRenderer.drawChars(
                                    sg2d, data, offset, length, ix, iy);
        return;
    }
    if (sg2d.transformState >= SunGraphics2D.TRANSFORM_TRANSLATESCALE) {
        double origin[] = {ix + info.originX, iy + info.originY};
        sg2d.transform.transform(origin, 0, origin, 0, 1);
        x = (float) origin[0];
        y = (float) origin[1];
    } else {
        x = ix + info.originX + sg2d.transX;
        y = iy + info.originY + sg2d.transY;
    }
    GlyphList gl = GlyphList.getInstance();
    if (gl.setFromChars(info, data, offset, length, x, y)) {
        drawGlyphList(sg2d, gl);
        gl.dispose();
    } else {
        gl.dispose(); // release this asap.
        TextLayout tl = new TextLayout(new String(data, offset, length),
                                       sg2d.getFont(),
                                       sg2d.getFontRenderContext());
        tl.draw(sg2d, ix, iy);

    }
}
 
Example 8
Project: openjdk-jdk10   File: GlyphVectorOutline.java   Source Code and License Vote up 5 votes
private static void drawString(Graphics2D g, Font font, String value, float x, float y) {
    AttributedString str = new AttributedString(value);
    str.addAttribute(TextAttribute.FOREGROUND, Color.BLACK);
    str.addAttribute(TextAttribute.FONT, font);
    FontRenderContext frc = new FontRenderContext(null, true, true);
    TextLayout layout = new LineBreakMeasurer(str.getIterator(), frc).nextLayout(Integer.MAX_VALUE);
    layout.draw(g, x, y);
}
 
Example 9
Project: openjdk-jdk10   File: CompositionArea.java   Source Code and License Vote up 5 votes
public void paint(Graphics g) {
    super.paint(g);
    g.setColor(getForeground());
    TextLayout layout = composedTextLayout;
    if (layout != null) {
        layout.draw((Graphics2D) g, TEXT_ORIGIN_X, TEXT_ORIGIN_Y);
    }
    if (caret != null) {
        Rectangle rectangle = getCaretRectangle(caret);
        g.setXORMode(getBackground());
        g.fillRect(rectangle.x, rectangle.y, 1, rectangle.height);
        g.setPaintMode();
    }
}
 
Example 10
Project: openjdk-jdk10   File: PathGraphics.java   Source Code and License Vote up 5 votes
public void drawString(AttributedCharacterIterator iterator,
                       float x, float y) {
    if (iterator == null) {
        throw
            new NullPointerException("attributedcharacteriterator is null");
    }
    TextLayout layout =
        new TextLayout(iterator, getFontRenderContext());
    layout.draw(this, x, y);
}
 
Example 11
Project: OpenJSharp   File: CompositionArea.java   Source Code and License Vote up 5 votes
public void paint(Graphics g) {
    super.paint(g);
    g.setColor(getForeground());
    TextLayout layout = composedTextLayout;
    if (layout != null) {
        layout.draw((Graphics2D) g, TEXT_ORIGIN_X, TEXT_ORIGIN_Y);
    }
    if (caret != null) {
        Rectangle rectangle = getCaretRectangle(caret);
        g.setXORMode(getBackground());
        g.fillRect(rectangle.x, rectangle.y, 1, rectangle.height);
        g.setPaintMode();
    }
}
 
Example 12
Project: OpenJSharp   File: GlyphListPipe.java   Source Code and License Vote up 5 votes
public void drawChars(SunGraphics2D sg2d,
                      char data[], int offset, int length,
                      int ix, int iy)
{
    FontInfo info = sg2d.getFontInfo();
    float x, y;
    if (info.pixelHeight > OutlineTextRenderer.THRESHHOLD) {
        SurfaceData.outlineTextRenderer.drawChars(
                                    sg2d, data, offset, length, ix, iy);
        return;
    }
    if (sg2d.transformState >= SunGraphics2D.TRANSFORM_TRANSLATESCALE) {
        double origin[] = {ix + info.originX, iy + info.originY};
        sg2d.transform.transform(origin, 0, origin, 0, 1);
        x = (float) origin[0];
        y = (float) origin[1];
    } else {
        x = ix + info.originX + sg2d.transX;
        y = iy + info.originY + sg2d.transY;
    }
    GlyphList gl = GlyphList.getInstance();
    if (gl.setFromChars(info, data, offset, length, x, y)) {
        drawGlyphList(sg2d, gl);
        gl.dispose();
    } else {
        gl.dispose(); // release this asap.
        TextLayout tl = new TextLayout(new String(data, offset, length),
                                       sg2d.getFont(),
                                       sg2d.getFontRenderContext());
        tl.draw(sg2d, ix, iy);

    }
}
 
Example 13
Project: openjdk-jdk10   File: PathGraphics.java   Source Code and License Vote up 5 votes
public void drawString(String str, float x, float y) {
    if (str.length() == 0) {
        return;
    }
    TextLayout layout =
        new TextLayout(str, getFont(), getFontRenderContext());
    layout.draw(this, x, y);
}
 
Example 14
Project: OpenJSharp   File: PathGraphics.java   Source Code and License Vote up 5 votes
public void drawString(String str, float x, float y) {
    if (str.length() == 0) {
        return;
    }
    TextLayout layout =
        new TextLayout(str, getFont(), getFontRenderContext());
    layout.draw(this, x, y);
}
 
Example 15
Project: jdk8u-jdk   File: FontPanel.java   Source Code and License Vote up 5 votes
private void tlDrawLine( Graphics2D g2, TextLayout tl,
                                   float baseX, float baseY ) {
    /// ABP - keep track of old tform, restore it later
    AffineTransform oldTx = null;
    oldTx = g2.getTransform();
    g2.translate( baseX, baseY );
    g2.transform( getAffineTransform( g2Transform ) );

    tl.draw( g2, (float) 0, (float) 0 );

    /// ABP - restore old tform
    g2.setTransform ( oldTx );

}
 
Example 16
Project: incubator-netbeans   File: FoldView.java   Source Code and License Vote up 4 votes
@Override
public void paint(Graphics2D g, Shape alloc, Rectangle clipBounds) {
    Rectangle2D.Double allocBounds = ViewUtils.shape2Bounds(alloc);
    if (allocBounds.intersects(clipBounds)) {
        Font origFont = g.getFont();
        Color origColor = g.getColor();
        Color origBkColor = g.getBackground();
        Shape origClip = g.getClip();
        try {
            // Leave component font
            
            g.setBackground(getBackgroundColor());

            int xInt = (int) allocBounds.getX();
            int yInt = (int) allocBounds.getY();
            int endXInt = (int) (allocBounds.getX() + allocBounds.getWidth() - 1);
            int endYInt = (int) (allocBounds.getY() + allocBounds.getHeight() - 1);
            g.setColor(getBorderColor());
            g.drawRect(xInt, yInt, endXInt - xInt, endYInt - yInt);
            
            g.setColor(getForegroundColor());
            g.clearRect(xInt + 1, yInt + 1, endXInt - xInt - 1, endYInt - yInt - 1);
            g.clip(alloc);
            TextLayout textLayout = getTextLayout();
            if (textLayout != null) {
                EditorView.Parent parent = (EditorView.Parent) getParent();
                float ascent = parent.getViewRenderContext().getDefaultAscent();
                String desc = fold.getDescription(); // For empty desc a single-space text layout is returned
                float x = (float) (allocBounds.getX() + EXTRA_MARGIN_WIDTH);
                float y = (float) allocBounds.getY();
                if (desc.length() > 0) {
                    
                    textLayout.draw(g, x, y + ascent);
                }
            }
        } finally {
            g.setClip(origClip);
            g.setBackground(origBkColor);
            g.setColor(origColor);
            g.setFont(origFont);
        }
    }
}
 
Example 17
Project: ramus   File: TextBounds.java   Source Code and License Vote up 4 votes
public void draw(Graphics2D g, int align, int pos, float fRectHeight,
                 MovingArea area) {
    float left = 0;
    float fromY = 0;

    if (pos == 1)
        fromY += fRectHeight / 2 - height / 2;
    else if (pos == 2)
        fromY += fRectHeight;

    if (fromY < 0)
        fromY = 0;

    int size = layouts.size();

    for (int i = 0; i < size; i++) {
        TextLayout textLayout = layouts.get(i);
        if (align == Line.CENTER_ALIGN)
            left = (float) maxWidth / 2f - textLayout.getAdvance() / 2f;
        else if (align == Line.RIGHT_ALIGN)
            left = (float) maxWidth - textLayout.getAdvance();

        fromY += textLayout.getAscent();

        if (fromY >= fRectHeight && i + 1 < size)
            break;

        if (fromY + textLayout.getAscent() + textLayout.getDescent()
                // + textLayout.getLeading()
                >= fRectHeight
                && i + 1 < size) {
            // textLayout= textLayout.getJustifiedLayout(maxWidth-20);
            textLayout.draw(g, left, fromY);
/*
 * Color color = g.getColor(); g.setColor(new Color(120, 120,
 * 120));
 * 
 * g.drawString("(.)", left + textLayout.getAdvance() + (float)
 * area.getIDoubleOrdinate(-0.0), fromY); g.setColor(color);
 */
            break;
        } else
            textLayout.draw(g, left, fromY);
        fromY += textLayout.getDescent() + textLayout.getLeading();
    }
}
 
Example 18
Project: ramus   File: TextBounds.java   Source Code and License Vote up 4 votes
public void drawWithSel(Graphics2D g, int align, int pos,
                        float fRectHeight, MovingArea area, int[][] found) {
    float left = 0;
    float fromY = 0;

    if (pos == 1)
        fromY += fRectHeight / 2 - height / 2;
    else if (pos == 2)
        fromY += fRectHeight;

    if (fromY < 0)
        fromY = 0;

    int size = layouts.size();
    in = false;
    int selIndex = 0;

    for (int i = 0; i < size; i++) {
        TextLayout textLayout = layouts.get(i);
        if (align == Line.CENTER_ALIGN)
            left = (float) maxWidth / 2f - textLayout.getAdvance() / 2f;
        else if (align == Line.RIGHT_ALIGN)
            left = (float) maxWidth - textLayout.getAdvance();

        fromY += textLayout.getAscent();

        if (fromY >= fRectHeight && i + 1 < size)
            break;

        if (fromY + textLayout.getAscent() + textLayout.getDescent() >= fRectHeight
                && i + 1 < size) {
            textLayout.draw(g, left, fromY);
            selIndex = drawSels(g, found, selIndex, i, textLayout, left,
                    fromY);
            break;
        } else {
            textLayout.draw(g, left, fromY);
            selIndex = drawSels(g, found, selIndex, i, textLayout, left,
                    fromY);
        }
        fromY += textLayout.getDescent() + textLayout.getLeading();
    }
}
 
Example 19
Project: openjdk-jdk10   File: PeekGraphics.java   Source Code and License Vote up 3 votes
/**
 * Draws the text given by the specified iterator, using this
 * graphics context's current color. The iterator has to specify a font
 * for each character. The baseline of the
 * first character is at position (<i>x</i>,&nbsp;<i>y</i>) in this
 * graphics context's coordinate system.
 * The rendering attributes applied include the clip, transform,
 * paint or color, and composite attributes.
 * For characters in script systems such as Hebrew and Arabic,
 * the glyphs may be draw from right to left, in which case the
 * coordinate supplied is the location of the leftmost character
 * on the baseline.
 * @param iterator the iterator whose text is to be drawn
 * @param x,y the coordinates where the iterator's text should be drawn.
 * @see #setPaint
 * @see java.awt.Graphics#setColor
 * @see #setTransform
 * @see #setComposite
 * @see #setClip
 */
public void drawString(AttributedCharacterIterator iterator,
                                float x, float y) {
    if (iterator == null) {
        throw new
            NullPointerException("AttributedCharacterIterator is null");
    }

    TextLayout layout = new TextLayout(iterator, getFontRenderContext());
    layout.draw(this, x, y);
}
 
Example 20
Project: OpenJSharp   File: PeekGraphics.java   Source Code and License Vote up 3 votes
/**
 * Draws the text given by the specified iterator, using this
 * graphics context's current color. The iterator has to specify a font
 * for each character. The baseline of the
 * first character is at position (<i>x</i>,&nbsp;<i>y</i>) in this
 * graphics context's coordinate system.
 * The rendering attributes applied include the clip, transform,
 * paint or color, and composite attributes.
 * For characters in script systems such as Hebrew and Arabic,
 * the glyphs may be draw from right to left, in which case the
 * coordinate supplied is the the location of the leftmost character
 * on the baseline.
 * @param iterator the iterator whose text is to be drawn
 * @param x,y the coordinates where the iterator's text should be drawn.
 * @see #setPaint
 * @see java.awt.Graphics#setColor
 * @see #setTransform
 * @see #setComposite
 * @see #setClip
 */
public void drawString(AttributedCharacterIterator iterator,
                                float x, float y) {
    if (iterator == null) {
        throw new
            NullPointerException("AttributedCharacterIterator is null");
    }

    TextLayout layout = new TextLayout(iterator, getFontRenderContext());
    layout.draw(this, x, y);
}