Java Graphics Draw String drawMessage(Graphics2D g, String message)

Here you can find the source of drawMessage(Graphics2D g, String message)

Description

draw Message

License

Apache License

Declaration

public static void drawMessage(Graphics2D g, String message) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.util.StringTokenizer;

public class Main {
    public static void drawMessage(Graphics2D g, String message) {
        final int gap = 5;

        Rectangle2D textArea = g.getFont().getStringBounds(message, g.getFontRenderContext());

        int x = gap - (int) textArea.getMinX();
        int y = gap - (int) textArea.getMinY();

        StringTokenizer messageTokenizer = new StringTokenizer(message, "\n");

        while (messageTokenizer.hasMoreTokens()) {
            String currentLine = messageTokenizer.nextToken();
            drawOutlinedString(g, currentLine, x, y, 0);

            y = y + (int) textArea.getHeight();
        }/*from  w w  w.j a  v  a2  s .c  o  m*/
    }

    public static void drawOutlinedString(Graphics2D g, String str, int x, int y, int extraSpace) {
        int currentX = x;

        for (int i = 0; i < str.length(); i++) {
            currentX += drawOutlinedStringSimple(g, str.substring(i, i + 1), currentX, y);
            currentX += extraSpace;
        }
    }

    public static int drawOutlinedStringSimple(Graphics2D g, String str, int x, int y) {
        Rectangle2D textArea = g.getFont().getStringBounds(str, g.getFontRenderContext());

        Color textColor = g.getColor();
        g.setColor(g.getBackground());

        g.drawString(str, x + 1, y + 1);
        g.drawString(str, x + 1, y - 1);
        g.drawString(str, x - 1, y + 1);
        g.drawString(str, x - 1, y - 1);

        g.drawString(str, x + 1, y);
        g.drawString(str, x - 1, y);
        g.drawString(str, x, y - 1);
        g.drawString(str, x, y + 1);

        g.setColor(textColor);
        g.drawString(str, x, y);

        return (int) textArea.getWidth() + 1;
    }
}

Related

  1. drawFormattedString(Graphics2D g2, String htmlStr, int x, int y, int w, int h)
  2. drawGradient(Graphics g, JComponent c, String prefix)
  3. drawGradientText(Graphics g, String text, int x, int y, Color c)
  4. drawLine(String pLine, Graphics2D pG, int pX, int pY, int pWidth, String pJustification)
  5. drawLineDrawChar(Graphics g, int x, int y, int bi, char c, int charWidth, int charHeight)
  6. drawRightJustifiedText(String text, int right, int y, Graphics g)
  7. drawRightText(Graphics g, String str, int x, int y, int width, int height)
  8. drawRotatedShape(final Shape shape, final Graphics2D g2, final float x, final float y, final double angle)
  9. drawScaleLabel(Graphics g, String label, int x, int y, boolean yAxisP)