user2327769
user2327769

Reputation: 1

Java: Adjust outline thickness

I use this code: to outline a font:

public class MyText extends JPanel {
String text1 = null;
public MyText (String text) {
text1 = text;
}

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    setBackground(Color.white);
    int w = getSize().width;
    int h = getSize().height;
    Graphics2D g2d = (Graphics2D) g;
    FontRenderContext fontRendContext = g2d.getFontRenderContext();
    Font font = new Font("Verdana", 1, 72);
    String st = new String(text1);
    TextLayout text = new TextLayout(st, font, fontRendContext);

    Shape shape = text.getOutline(null);
    Rectangle rect = shape.getBounds();

    AffineTransform affineTransform = new AffineTransform();
    affineTransform = g2d.getTransform();
    affineTransform.translate(w / 2 - (rect.width / 2), h / 2
            + (rect.height / 2));
    g2d.transform(affineTransform);
    g2d.setColor(Color.black);
    g2d.draw(shape);
    g2d.setClip(shape);
}

The problem is I have no idea how to adjust the thickness of the outline. I tried displaying another bigger string over the first string, but the result is quite bad (wrong pixels...).

Hav you got any ideas?

Thanks in advance.

Upvotes: 0

Views: 2209

Answers (2)

Reimeus
Reimeus

Reputation: 159844

You can use setStroke. For example

g2d.setStroke(new BasicStroke(4));

Upvotes: 1

MadProgrammer
MadProgrammer

Reputation: 347314

Start by taking a look at Stroking and Filling Grapcs Primitives and Fun with Java2D - Strokes

Upvotes: 0

Related Questions