Java Utililty Methods Graphics Draw String

List of utility methods to do Graphics Draw String

Description

The list of methods to do Graphics Draw String are organized into topic(s).

Method

voiddrawRightText(Graphics g, String str, int x, int y, int width, int height)
Draw a string at a given location on screen right-aligned in a given rectangle.
FontMetrics fm = g.getFontMetrics();
Shape oldClip = g.getClip();
g.clipRect(x, y, width, height);
g.drawString(str, x + width - fm.stringWidth(str), y + (height + fm.getAscent()) / 2);
g.setClip(oldClip);
voiddrawRotatedShape(final Shape shape, final Graphics2D g2, final float x, final float y, final double angle)
Draws a rotated shape.
final AffineTransform saved = g2.getTransform();
final AffineTransform rotate = AffineTransform.getRotateInstance(angle, x, y);
g2.transform(rotate);
g2.draw(shape);
g2.setTransform(saved);
voiddrawScaleLabel(Graphics g, String label, int x, int y, boolean yAxisP)

Draws the scale label in the String label using the graphics in the Graphics object g.

if ((g == null) || (label == null) || (label.length() <= 0)) {
    return;
FontMetrics metrics = g.getFontMetrics();
int fontHeight = metrics.getHeight();
int fontWidth = metrics.stringWidth(label);
if (yAxisP) {
    g.drawString(label, x - fontWidth, y + (fontHeight / 2));
...
voiddrawSystemNameLabel(Graphics2D g, String sysName, Color color, double safetyOffset, boolean isLocationKnownUpToDate)
draw System Name Label
Graphics2D g2 = (Graphics2D) g.create();
int useTransparency = (isLocationKnownUpToDate ? 255 : AGE_TRANSPARENCY);
if (useTransparency != 255)
    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, useTransparency / 255f));
g2.setColor(Color.BLACK);
g2.drawString(sysName, (int) (12 * safetyOffset / 20) + 1, 1);
g2.setColor(color);
g2.drawString(sysName, (int) (12 * safetyOffset / 20), 0);
...
RectangledrawText(Graphics graphics, int x, int y, String text)
Draw a text to a graphics context.
Rectangle bounds = getTextBounds(graphics, text);
graphics.drawString(text, x + bounds.x, y + bounds.y);
bounds.y = 0;
bounds.translate(x, y);
return bounds;
voiddrawText(Graphics2D graphics, Font font, Dimension2D dimension, String text)
draws the given unicode characters horizontally using the specified font.
FontMetrics metrics = graphics.getFontMetrics(font);
Rectangle2D bounds2d = metrics.getStringBounds(text, graphics);
double x = (dimension.getWidth() - bounds2d.getWidth()) / 2.0;
double y = bounds2d.getHeight() / 2.0 - metrics.getLeading();
graphics.setFont(font);
graphics.drawString(text, (float) x, (float) y);
voiddrawTextCenter(Graphics2D g2, String str, int x, int y)
draw Text Center
int strWidth = getStringWidth(g2, str);
drawText(g2, str, x - strWidth / 2, y + g2.getFontMetrics().getHeight() / 2);
voiddrawTextInBoundedArea(Graphics2D g2d, int x1, int y1, int x2, int y2, String text)
draw Text In Bounded Area
float interline = 1;
float width = x2 - x1;
AttributedString as = new AttributedString(text);
as.addAttribute(TextAttribute.FOREGROUND, g2d.getPaint());
as.addAttribute(TextAttribute.FONT, g2d.getFont());
AttributedCharacterIterator aci = as.getIterator();
FontRenderContext frc = new FontRenderContext(null, true, false);
LineBreakMeasurer lbm = new LineBreakMeasurer(aci, frc);
...
voiddrawTuplet(Graphics g, int x, int y, int x2, int y2, int bi, String s1, String s2)
draw Tuplet
Font font = g.getFont();
g.setFont(new Font(font.getName(), font.getStyle(), font.getSize() / 2));
g.drawString(s1, x + 1, y + 1 + bi / 2);
g.drawString(s2, x2, y2 + bi / 2);
g.setFont(font);
voiddrawUnderlineCharAt(Graphics g, String text, int underlinedIndex, int x, int y)
draw Underline Char At
if (underlinedIndex >= 0 && underlinedIndex < text.length()) {
    FontMetrics fm = g.getFontMetrics();
    int underlineRectX = x + fm.stringWidth(text.substring(0, underlinedIndex));
    int underlineRectY = y;
    int underlineRectWidth = fm.charWidth(text.charAt(underlinedIndex));
    int underlineRectHeight = 1;
    g.fillRect(underlineRectX, underlineRectY + 1, underlineRectWidth, underlineRectHeight);