SWT font set

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

你可能感兴趣的:(C++,c,windows,C#)