Java FontMetrics paintString(String s, Graphics2D g2, Rectangle2D rect, float horizontal, float vertical)

Here you can find the source of paintString(String s, Graphics2D g2, Rectangle2D rect, float horizontal, float vertical)

Description

horizontal is from -1 to 1 where -1 means text is aligned left, 0 centered and 1 right vertical likewise (-1 is bottom) shrink text if necessary!

License

Apache License

Parameter

Parameter Description
s a parameter
g2 a parameter
rect a parameter
vertical a parameter
horizontal a parameter

Declaration

public static Rectangle2D paintString(String s, Graphics2D g2, Rectangle2D rect, float horizontal,
        float vertical) 

Method Source Code

//package com.java2s;
/**/*from  w  ww.j  a  v a2s . co m*/
 * 
 *    Copyright 2017 Florian Erhard
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 * 
 */

import java.awt.Font;
import java.awt.Graphics2D;

import java.awt.Paint;

import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;

public class Main {
    /**
     * horizontal is from -1 to 1 where -1 means text is aligned left, 0 centered and 1 right
     * vertical likewise (-1 is bottom)
     * 
     * shrink text if necessary!
     * @param s
     * @param g2
     * @param rect
     * @param vertical
     * @param horizontal
     */
    public static Rectangle2D paintString(String s, Graphics2D g2, Rectangle2D rect, float horizontal,
            float vertical) {
        return paintString(s, g2, rect, 0, 0, 0, horizontal, vertical, null);
    }

    public static Rectangle2D paintString(String s, Graphics2D g2, Rectangle2D rect, double rotation,
            float horizontal, float vertical) {
        return paintString(s, g2, rect, rotation, rect.getCenterX(), rect.getCenterY(), horizontal, vertical, null);
    }

    public static Rectangle2D paintString(String s, Graphics2D g2, Rectangle2D rect, double rotation, double rotX,
            double rotY, float horizontal, float vertical, Paint outline) {

        if (s == null)
            return new Rectangle2D.Double(rect.getX(), rect.getY(), 0, 0);

        AffineTransform trans = g2.getTransform();
        g2.setTransform(new AffineTransform());

        Font original = g2.getFont();
        vertical = vertical / 2f + .5f;
        horizontal = horizontal / 2f + .5f;

        Rectangle2D des = g2.getFontMetrics().getStringBounds(s, g2);

        double w = rect.getWidth();
        double h = rect.getHeight();

        if (rotation != 0) {
            double w1 = w * Math.cos(rotation) + h * Math.sin(rotation);
            h = h * Math.cos(rotation) + w * Math.sin(rotation);
            w = w1;
        }

        double xratio = des.getWidth() / w;
        double yratio = des.getHeight() / h;
        double maxratio = Math.max(xratio, yratio);

        if (maxratio > 1) {
            g2.setFont(g2.getFont().deriveFont(AffineTransform.getScaleInstance(1 / maxratio, 1 / maxratio)));
            des = g2.getFontMetrics().getStringBounds(s, g2);
        }

        double x = rect.getX() + horizontal * (rect.getWidth() - des.getWidth());
        double y = rect.getY() + g2.getFontMetrics().getAscent() + vertical * (rect.getHeight() - des.getHeight());

        g2.rotate(rotation, rotX, rotY);
        g2.drawString(s, (float) x, (float) y);
        if (outline != null) {
            Shape outl = g2.getFont().createGlyphVector(g2.getFontRenderContext(), s).getOutline();
            g2.translate(x, y);
            g2.setPaint(outline);
            g2.draw(outl);

        }

        g2.setFont(original);

        des.setRect(x, y - des.getHeight(), des.getWidth(), des.getHeight());

        g2.setTransform(trans);

        return des;
    }
}

Related

  1. getTextCenterShear(final FontMetrics fm, final String text)
  2. getTextCenterShearX(final FontMetrics fm, final String text)
  3. limitWithEllipsis(String str, Font font, int maxWidth, Component c)
  4. maxSize(Component component, String maxString)
  5. maxStringPixelWidth(String[] strings, FontMetrics fm)
  6. render(Graphics2D graphics, String str, Rectangle2D box, double xalign, double yalign)
  7. setComponentSize(Component component, int rows, int columns)
  8. setSizedFont(Graphics g, String text, float maxFontSize, int maxWidth)
  9. shortenString(FontMetrics fm, String str, int maxWidth)