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);
分享到:
相关推荐
- `setFont(Font font)`:设置按钮上文字的字体。 - `setForeground(Color color)`:设置按钮的前景色。 - `setBackground(Color color)`:设置按钮的背景色。 - `setImage(Image image)`:设置按钮上显示的图像。 -...
- **`setFont(Font font)`**:设置按钮上文字的字体。 - **`setForeground(Color color)`**:设置按钮的前景颜色。 - **`setBackground(Color color)`**:设置按钮的背景颜色。 - **`setImage(Image image)`**:设置...
12. **setFont(Font font)**: 设置字体。 13. **setData(Object data)**: 挂载数据到组件。 14. **dispose()**: 销毁组件。 15. **pack()**: 自动调整容器内的控件大小。 16. **layout()**: 更新布局。 ### 五、...
7. **`setBackground(Color color)`**:定义组件的背景色。同样地,参数`color`也是一个`Color`对象,它决定了组件的底色。 8. **`setCursor(Cursor cursor)`**:更改组件上的鼠标光标样式。参数`cursor`是一个`...
12.3 字体(Font) 250 12.4 光标(Cursor) 251 12.5 图像(Image) 252 12.5.1 画布类(Canvas) 252 12.5.2 图像类(Image) 254 12.5.3 图像数据类(ImageData) 255 12.5.4 保存图像类...
setSize(453, 317); final JLabel label = new JLabel(); label.setHorizontalAlignment(SwingConstants.CENTER); label.setForeground(new Color(128, 128, 0)); label.setFont(new Font("华文彩云", ...
3. **SWT (Standard Widget Toolkit)**:这是由IBM公司为Eclipse开发的一套组件库,主要用于Eclipse插件的开发。 #### 二、Java GUI API详解 Java GUI API包含了一系列用于构建图形用户界面的类。这些类可以分为三...