`
xiangyuan8603
  • 浏览: 52793 次
  • 性别: Icon_minigender_2
  • 来自: 深圳
社区版块
存档分类
最新评论

SWT font set

    博客分类:
  • java
阅读更多
When generate a image for Chinese character, if your computer has no song ti font style, should read a system file to load the font style.

find following file.
C:\WINDOWS\Fonts\simsun.ttc

Coding as follows:
Font ttfFont = Font.createFont(Font.TRUETYPE_FONT, new File("C:\WINDOWS\Fonts\simsun.ttc"));

Font ttfReal = ttfFont.deriveFont(Font.PLAIN, 12);
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = (Graphics2D) image.getGraphics();
g2.setBackground(Color.YELLOW);
g2.clearRect(0, 0, width, height);
g2.setPaint(Color.BLACK);
FontRenderContext context = g2.getFontRenderContext();
Rectangle2D bounds = ttfReal.getStringBounds(name, context);
double x = (width - bounds.getWidth()) / 2;
double y = (height - bounds.getHeight()) / 2;
double ascent = -bounds.getY();
double baseY = y + ascent;
g2.setFont(ttfReal);
g2.drawString(name, (int) x, (int) baseY);
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics