14

如何在 AWT 中的任何文本周围绘制轮廓,类似于这张图片?

大纲

4

5 回答 5

9

两个例子

此绘制的输出将是BufferedImage,对于 AWT 组件使用方法paint(),对于 Swing JComponents 就在那里paintComponet()

此外,来自评论中链接的代码:

于 2012-04-04T18:21:16.663 回答
8

尝试以下操作:

public void paintTextWithOutline(Graphics g) {
    String text = "some text";

    Color outlineColor = Color.white;
    Color fillColor = Color.black;
    BasicStroke outlineStroke = new BasicStroke(2.0f);

    if (g instanceof Graphics2D) {
        Graphics2D g2 = (Graphics2D) g;

        // remember original settings
        Color originalColor = g2.getColor();
        Stroke originalStroke = g2.getStroke();
        RenderingHints originalHints = g2.getRenderingHints();


        // create a glyph vector from your text
        GlyphVector glyphVector = getFont().createGlyphVector(g2.getFontRenderContext(), text);
        // get the shape object
        Shape textShape = glyphVector.getOutline();

        // activate anti aliasing for text rendering (if you want it to look nice)
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setRenderingHint(RenderingHints.KEY_RENDERING,
                RenderingHints.VALUE_RENDER_QUALITY);

        g2.setColor(outlineColor);
        g2.setStroke(outlineStroke);
        g2.draw(textShape); // draw outline

        g2.setColor(fillColor);
        g2.fill(textShape); // fill the shape

        // reset to original settings after painting
        g2.setColor(originalColor);
        g2.setStroke(originalStroke);
        g2.setRenderingHints(originalHints);
    }
}
于 2016-02-05T10:42:11.320 回答
2

不知道你现在是如何绘制文本的,但你可以这样做的一种方法是使用 BufferedImage 作为你正在绘制的任何内容的覆盖。

  1. 使用要绘制的字符串和字体的尺寸创建 BufferedImage(查看 FontMetrics 类)。
  2. 用透明度填充 BufferedImage。
  3. 使用您想要的任何颜色将您的字符串绘制到 BufferedImage 上。
  4. 遍历 BufferedImage 中的每个像素,查看它与文本颜色像素的距离。如果它在一定距离内,则将该像素绘制为黑色,如果它远离文本颜色,则可能更透明。当然,如果像素已经与您的文本颜色相同,则忽略它。
  5. 将 BufferedImage 绘制到您正在绘制的任何物体上。

编辑

可能有一些库已经这样做了,但是如果我必须从头开始编写代码,这就是我尝试这样做的方式。

于 2012-04-04T18:11:31.667 回答
1

这是一个骇人听闻的例子。它不像其他的那样复杂,但更易于理解,并且它的行为类似于 JLabel。

public class OutlineLabel extends JLabel {

    private Color outlineColor = Color.WHITE;
    private boolean isPaintingOutline = false;
    private boolean forceTransparent = false;

    public OutlineLabel() {
        super();
    }

    public OutlineLabel(String text) {
        super(text);
    }

    public OutlineLabel(String text, int horizontalAlignment) {
        super(text, horizontalAlignment);
    }

    public Color getOutlineColor() {
        return outlineColor;
    }

    public void setOutlineColor(Color outlineColor) {
        this.outlineColor = outlineColor;
        this.invalidate();
    }

    @Override
    public Color getForeground() {
        if ( isPaintingOutline ) {
            return outlineColor;
        } else {
            return super.getForeground();
        }
    }

    @Override
    public boolean isOpaque() {
        if ( forceTransparent ) {
            return false;
        } else {
            return super.isOpaque();
        }
    }

    @Override
    public void paint(Graphics g) {

        String text = getText();
        if ( text == null || text.length() == 0 ) {
            super.paint(g);
            return;
        }

        // 1 2 3
        // 8 9 4
        // 7 6 5

        if ( isOpaque() )
            super.paint(g);

        forceTransparent = true;
        isPaintingOutline = true;
        g.translate(-1, -1); super.paint(g); // 1 
        g.translate( 1,  0); super.paint(g); // 2 
        g.translate( 1,  0); super.paint(g); // 3 
        g.translate( 0,  1); super.paint(g); // 4
        g.translate( 0,  1); super.paint(g); // 5
        g.translate(-1,  0); super.paint(g); // 6
        g.translate(-1,  0); super.paint(g); // 7
        g.translate( 0, -1); super.paint(g); // 8
        g.translate( 1,  0); // 9
        isPaintingOutline = false;

        super.paint(g);
        forceTransparent = false;

    }

    public static void main(String[] args) {
        JFrame w = new JFrame();
        w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        OutlineLabel label = new OutlineLabel("Test", OutlineLabel.CENTER);
        label.setOpaque(true);
        w.setContentPane(new JPanel(new BorderLayout()));
        w.add(label, BorderLayout.CENTER);
        w.pack();
        w.setVisible(true);
    }
}
于 2014-05-07T14:52:20.903 回答
0

一些最愚蠢的解决方法: - 输入相同的单词两次,但其中一个是黑色的,另一个是白色的,把白色放在黑色的上面,你可能会得到类似的东西。- 找到一个看起来像上面例子的字体,并使用它。

于 2012-04-04T17:15:04.640 回答