Java FontMetrics render(Graphics2D graphics, String str, Rectangle2D box, double xalign, double yalign)

Here you can find the source of render(Graphics2D graphics, String str, Rectangle2D box, double xalign, double yalign)

Description

render text

License

Open Source License

Declaration

public static void render(Graphics2D graphics, String str, Rectangle2D box, double xalign, double yalign) 

Method Source Code

//package com.java2s;
/**/* w  w w . ja v a  2  s . c  o m*/
 * GenJ - GenealogyJ
 *
 * Copyright (C) 1997 - 2010 Nils Meier <nils@meiers.net>
 *
 * This piece of code is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This code is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import java.awt.FontMetrics;

import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.font.LineMetrics;

import java.awt.geom.Rectangle2D;

public class Main {
    /**
     * render text
     */
    public static void render(Graphics2D graphics, String str, Rectangle2D box, double xalign, double yalign) {

        FontMetrics fm = graphics.getFontMetrics();
        LineMetrics lm = fm.getLineMetrics(str, graphics);

        float h = 0;
        String[] lines = str.split("\\\n");
        float[] ws = new float[lines.length];
        for (int i = 0; i < lines.length; i++) {
            Rectangle2D r = fm.getStringBounds(lines[i], graphics);
            ws[i] = (float) r.getWidth();
            h = Math.max(h, (float) r.getHeight());
        }

        Shape clip = graphics.getClip();
        graphics.clip(box);

        for (int i = 0; i < lines.length; i++) {
            double x = Math.max(box.getX(), box.getCenterX() - ws[i] * xalign);
            double y = Math.max(box.getY(),
                    box.getY() + (box.getHeight() - lines.length * h) * yalign + i * h + h - lm.getDescent());

            graphics.drawString(lines[i], (float) x, (float) y);
        }

        graphics.setClip(clip);
    }

    /**
     * render text
     */
    public static Rectangle render(Graphics2D graphics, String str, double x, double y, double xalign,
            double yalign) {

        FontMetrics fm = graphics.getFontMetrics();
        Rectangle2D r = fm.getStringBounds(str, graphics);
        LineMetrics lm = fm.getLineMetrics(str, graphics);

        float h = (float) r.getHeight();
        float w = (float) r.getWidth();

        x = x - w * xalign;
        y = y - h * yalign;

        graphics.drawString(str, (float) x, (float) y + h - lm.getDescent());

        return new Rectangle((int) x, (int) y, (int) w, (int) h);
    }
}

Related

  1. getTextCenterShearX(final FontMetrics fm, final String text)
  2. limitWithEllipsis(String str, Font font, int maxWidth, Component c)
  3. maxSize(Component component, String maxString)
  4. maxStringPixelWidth(String[] strings, FontMetrics fm)
  5. paintString(String s, Graphics2D g2, Rectangle2D rect, float horizontal, float vertical)
  6. setComponentSize(Component component, int rows, int columns)
  7. setSizedFont(Graphics g, String text, float maxFontSize, int maxWidth)
  8. shortenString(FontMetrics fm, String str, int maxWidth)
  9. splitStringWithLength(String s, int length, FontMetrics fm)