Java Draw Outline drawOutlineText(Graphics graphics, String text, Color textColor, int x, int y, Color outlineColor, int outlineWidth)

Here you can find the source of drawOutlineText(Graphics graphics, String text, Color textColor, int x, int y, Color outlineColor, int outlineWidth)

Description

Draw an outlined text.

License

Open Source License

Parameter

Parameter Description
graphics The graphics context.
text The text.
textColor The text color (the inside).
x The text position x coordinate.
y The text position y coordinate.
outlineColor The outline color.
outlineWidth The width of the outline.

Declaration

public static void drawOutlineText(Graphics graphics, String text, Color textColor, int x, int y,
        Color outlineColor, int outlineWidth) 

Method Source Code


//package com.java2s;
import java.awt.*;

public class Main {
    /**//from   w  w  w.  j  a v  a 2s  . com
     * Draw an outlined text. This method simply draws the text multiple times. For an outline width
     * of 1, the text is drawn 9 times. It is drawn shifted 1 pixel up and left, shifted one pixel
     * up, one pixel up and right, one pixel left, etc. and finally once on the original position.
     * This is 9 times for an outline width of 1. The bigger the outline width, the more often the
     * text is drawn, so performance gets worse with the outline size.
     *
     * @param graphics     The graphics context.
     * @param text         The text.
     * @param textColor    The text color (the inside).
     * @param x            The text position x coordinate.
     * @param y            The text position y coordinate.
     * @param outlineColor The outline color.
     * @param outlineWidth The width of the outline.
     */
    public static void drawOutlineText(Graphics graphics, String text, Color textColor, int x, int y,
            Color outlineColor, int outlineWidth) {
        graphics.setColor(outlineColor);
        for (int dy = -outlineWidth; dy <= outlineWidth; dy++) {
            for (int dx = -outlineWidth; dx <= outlineWidth; dx++) {
                if (dx != 0 && dy != 0) {
                    graphics.drawString(text, x + dx, y + dy);
                }
            }
        }
        graphics.setColor(textColor);
        graphics.drawString(text, x, y);
    }
}

Related

  1. drawOutlinedString(String s, int x, int y, Color fill, Color outline, Graphics g)
  2. drawOutlinedStringSimple(Graphics2D g, String str, int x, int y)
  3. drawStringWithOutline(final Graphics g, final String str, int x, int y, Color fillColor, Color outlineColor)
  4. drawStringOutline(Graphics g, String s, int x, int y, Color c, Color cOutLine)