0

我使用此代码:勾勒字体:

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);
}

问题是我不知道如何调整轮廓的粗细。我尝试在第一个字符串上显示另一个更大的字符串,但结果非常糟糕(错误的像素......)。

你有什么想法吗?

提前致谢。

4

2 回答 2

1

您可以使用setStroke。例如

g2d.setStroke(new BasicStroke(4));
于 2013-04-27T21:33:24.970 回答
0

首先看一下Stroking and Filling Grapcs Primitives and Fun with Java2D - Strokes

于 2013-04-27T21:32:55.737 回答