Java Graphics2D 画出文字描边效果
Posted 落枫飘飘
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java Graphics2D 画出文字描边效果相关的知识,希望对你有一定的参考价值。
在CSDN看到的,在此记下。
(http://bbs.csdn.net/topics/390703095)
import javax.swing.*; import java.awt.*; import java.awt.font.GlyphVector; public class Test extends JComponent { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { JFrame test = new JFrame("Test"); test.setContentPane(new Test()); test.pack(); test.setLocationRelativeTo(null); test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); test.setVisible(true); } }); } Test() { setPreferredSize(new Dimension(800, 600)); } @Override protected void paintComponent(Graphics g) { Font f = new Font("Courier New", Font.BOLD, 140); GlyphVector v = f.createGlyphVector(getFontMetrics(f).getFontRenderContext(), "Hello"); Shape shape = v.getOutline(); Rectangle bounds = shape.getBounds(); Graphics2D gg = (Graphics2D) g; gg.translate( (getWidth() - bounds.width) / 2 - bounds.x, (getHeight() - bounds.height) / 2 - bounds.y ); gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); gg.setColor(Color.WHITE); gg.fill(shape); gg.setColor(Color.BLUE.darker().darker()); gg.setStroke(new BasicStroke(3)); gg.draw(shape); } }
以上是关于Java Graphics2D 画出文字描边效果的主要内容,如果未能解决你的问题,请参考以下文章