Calculate the origin points that are used to draw a list of TextLayouts. - Java 2D Graphics

Java examples for 2D Graphics:Text

Description

Calculate the origin points that are used to draw a list of TextLayouts.

Demo Code


//package com.java2s;

import java.awt.Graphics2D;

import java.awt.Rectangle;
import java.awt.font.TextLayout;

import java.awt.geom.Point2D;

import java.util.ArrayList;

public class Main {
    /**//from w  w  w.j  ava2 s  . co  m
     * Calculate the origin points that are used to draw a list of TextLayouts.
     * @param textLayouts the drawn TextLayouts.
     * @param bounds the bounds surround the drawn TextLayouts.
     * @param buffer the extra space around the drawn text.
     * @param g2 the graphic context.
     * @return a list of Point2D.Float that are origin points for drawing TextLayouts.
     */
    public static java.util.List getDrawPoints(java.util.List textLayouts,
            Rectangle bounds, int buffer, Graphics2D g2) {
        java.util.List points = new ArrayList(textLayouts.size());
        float w = (float) bounds.getWidth();
        TextLayout layout = (TextLayout) textLayouts.get(0);
        float x = (float) (w - layout.getAdvance()) / 2 + bounds.x;
        float y = (float) (layout.getAscent() + buffer) + bounds.y;
        points.add(new Point2D.Float(x, y));
        y += (layout.getDescent() + layout.getLeading());
        for (int i = 1; i < textLayouts.size(); i++) {
            layout = (TextLayout) textLayouts.get(i);
            x = (float) (w - layout.getAdvance()) / 2 + bounds.x;
            y += layout.getAscent();
            points.add(new Point2D.Float(x, y));
            y += (layout.getDescent() + layout.getLeading());
        }
        return points;
    }
}

Related Tutorials