Java Graphics Draw Multiline String drawRightMultiLineText(String text, Graphics G, int x, int y)

Here you can find the source of drawRightMultiLineText(String text, Graphics G, int x, int y)

Description

draw Right Multi Line Text

License

Apache License

Declaration

public static void drawRightMultiLineText(String text, Graphics G, int x, int y) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.awt.*;

import java.util.*;

public class Main {
    public static void drawRightMultiLineText(String text, Graphics G, int x, int y) {
        FontMetrics metrics = G.getFontMetrics();
        Dimension size = measureMultiLineText(text, G);

        StringTokenizer str = new StringTokenizer(text, "\n\r");
        int height = y;

        while (str.hasMoreTokens()) {
            String token = str.nextToken();

            G.drawString(token, x + size.width - metrics.stringWidth(token), height + metrics.getAscent());
            height += (metrics.getAscent() + metrics.getLeading());
        }//from   www.  ja va  2s  . com
    }

    public static Dimension measureMultiLineText(String text, Graphics G) {
        FontMetrics metrics = G.getFontMetrics();
        if (metrics == null) {
            return (new Dimension(0, 0));
        }
        return (measureMultiLineText(text, metrics));
    }

    @SuppressWarnings(value = "deprecated")
    public static Dimension measureMultiLineText(String text, Component c) {
        Font fn = c.getFont();
        FontMetrics metrics = Toolkit.getDefaultToolkit().getFontMetrics(fn);
        if (metrics == null) {
            return (new Dimension(0, 0));
        }
        return (measureMultiLineText(text, metrics));
    }

    public static Dimension measureMultiLineText(String text, FontMetrics metrics) {
        StringTokenizer str = new StringTokenizer(text, "\n\r");
        int width = 0;
        int height = 0;

        while (str.hasMoreTokens()) {
            String token = str.nextToken();
            height += (metrics.getAscent() + metrics.getLeading());
            width = Math.max(metrics.stringWidth(token), width);
        }
        return (new Dimension(width, height));
    }
}

Related

  1. draw(String _str, Graphics2D _g2, int _nX0, int _nY0, int _nX1, int _nY1)
  2. drawMultilineString(Graphics g, String s, int alignment, Rectangle r, boolean print)
  3. drawMultilineText(final Graphics2D gc, final int x, int y, final String text)
  4. drawMultiLineText(String text, Graphics G, int x, int y)
  5. drawString(Graphics g, String s, int x, int y)
  6. drawStringBiggest(final Graphics2D g2d, final Dimension dim, final String drawString)
  7. drawStringUnderlineCharAt(Graphics g, String s, int underlinedIndex, int x, int y)
  8. drawStyleString(final Graphics graphics, final String message, final int x, final int y, final Color color, final Color color1)