Java Draw Outline drawStringOutline(Graphics g, String s, int x, int y, Color c, Color cOutLine)

Here you can find the source of drawStringOutline(Graphics g, String s, int x, int y, Color c, Color cOutLine)

Description

draw String Outline

License

Open Source License

Declaration

static public void drawStringOutline(Graphics g, String s, int x, int y, Color c, Color cOutLine) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.awt.*;

import java.awt.font.GlyphVector;
import java.awt.geom.AffineTransform;

public class Main {
    static public void drawStringOutline(Graphics g, String s, int x, int y, Color c, Color cOutLine) {

        if (c == null)
            c = g.getColor();/* w ww  .j a v a  2 s .co m*/
        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.translate(-0.3,-0.3);
            g2.fill(s1);
        } finally {
            g2.setTransform(t);
        }
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aliasing);
        g2.setColor(cb);
    }
}

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. drawOutlineText(Graphics graphics, String text, Color textColor, int x, int y, Color outlineColor, int outlineWidth)
  4. drawStringWithOutline(final Graphics g, final String str, int x, int y, Color fillColor, Color outlineColor)