Here you can find the source of drawRightMultiLineText(String text, Graphics G, int x, int y)
public static void drawRightMultiLineText(String text, Graphics G, int x, int y)
//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)); } }