Java Utililty Methods Draw Outline

List of utility methods to do Draw Outline

Description

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

Method

voiddrawOutlinedString(String s, int x, int y, Color fill, Color outline, Graphics g)
Draws basic nice looking outlined string
g.setColor(outline);
g.drawString(s, x + 1, y - 1);
g.drawString(s, x - 1, y + 1);
g.drawString(s, x + 1, y - 1);
g.drawString(s, x + 1, y + 1);
g.setColor(fill);
g.drawString(s, x, y);
intdrawOutlinedStringSimple(Graphics2D g, String str, int x, int y)
draw Outlined String Simple
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);
...
voiddrawOutlineText(Graphics graphics, String text, Color textColor, int x, int y, Color outlineColor, int outlineWidth)
Draw an outlined text.
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);
voiddrawStringWithOutline(final Graphics g, final String str, int x, int y, Color fillColor, Color outlineColor)
draw String With Outline
g.setColor(outlineColor);
for (int i = 1; i <= 1; i++) {
    g.drawString(str, x + i, y);
    g.drawString(str, x - i, y);
    g.drawString(str, x, y + i);
    g.drawString(str, x, y - i);
g.setColor(fillColor);
...
voiddrawStringOutline(Graphics g, String s, int x, int y, Color c, Color cOutLine)
draw String Outline
if (c == null)
    c = g.getColor();
if (cOutLine == null)
    cOutLine = Color.black;
if (!(g instanceof Graphics2D)) {
    g.drawString(s, x, y);
    return;
Graphics2D g2 = (Graphics2D) g;
Color cb = g2.getColor();
Object aliasing = g2.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
AffineTransform t = g2.getTransform();
try {
    Font f = g2.getFont();
    FontMetrics fm = g2.getFontMetrics(f);
    GlyphVector v = f.createGlyphVector(fm.getFontRenderContext(), s);
    Shape s1 = v.getOutline();
    g2.translate(x, y);
    Stroke st = g2.getStroke();
    g2.setStroke(new BasicStroke(1.6f, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_BEVEL));
    g.setColor(cOutLine);
    g2.draw(s1);
    g2.setStroke(st);
    g2.setColor(c);
    g2.fill(s1);
} finally {
    g2.setTransform(t);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aliasing);
g2.setColor(cb);