首页 > 代码库 > Java Graphics2D 画出文字描边效果
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 画出文字描边效果
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。