Java examples for 2D Graphics:Text
Calculate the start point for drawing a TextLayout.
//package com.java2s; import java.awt.Point; import java.awt.Rectangle; import java.awt.font.TextLayout; import java.awt.geom.Rectangle2D; public class Main { /**/* w w w .j ava 2 s. co m*/ * Calculate the start point for drawing a TextLayout. * @param textLayout * @param bounds the constraints of rendering. * @param p the point's x y values will be reset if p is not null. * @return a new Point object will be returned if p is null. Otherwise, * the passed Point object will be returned. */ public static Point getDrawPoint(TextLayout textLayout, Rectangle bounds, Point p) { Rectangle2D textBounds = textLayout.getBounds(); int x = (int) (bounds.x + bounds.width / 2 - textBounds.getWidth() / 2); int y = (int) (bounds.y + bounds.height / 2 - textBounds .getHeight() / 2); y += textLayout.getAscent(); if (p == null) return new Point(x, y); else { p.x = x; p.y = y; return p; } } }