`
xiangyuan8603
  • 浏览: 54160 次
  • 性别: 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);
分享到:
评论

相关推荐

    SWT常用组件

    - `setFont(Font font)`:设置按钮上文字的字体。 - `setForeground(Color color)`:设置按钮的前景色。 - `setBackground(Color color)`:设置按钮的背景色。 - `setImage(Image image)`:设置按钮上显示的图像。 -...

    SWT控件知识学习笔记

    - **`setFont(Font font)`**:设置按钮上文字的字体。 - **`setForeground(Color color)`**:设置按钮的前景颜色。 - **`setBackground(Color color)`**:设置按钮的背景颜色。 - **`setImage(Image image)`**:设置...

    学习SWT,RCP的笔记.doc

    12. **setFont(Font font)**: 设置字体。 13. **setData(Object data)**: 挂载数据到组件。 14. **dispose()**: 销毁组件。 15. **pack()**: 自动调整容器内的控件大小。 16. **layout()**: 更新布局。 ### 五、...

    Eclipse开发经典教程:常用SWT组件.pdf

    7. **`setBackground(Color color)`**:定义组件的背景色。同样地,参数`color`也是一个`Color`对象,它决定了组件的底色。 8. **`setCursor(Cursor cursor)`**:更改组件上的鼠标光标样式。参数`cursor`是一个`...

    Eclipse_Swt_Jface_核心应用_部分19

    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("华文彩云", ...

    Java-GUI介绍和使用

    3. **SWT (Standard Widget Toolkit)**:这是由IBM公司为Eclipse开发的一套组件库,主要用于Eclipse插件的开发。 #### 二、Java GUI API详解 Java GUI API包含了一系列用于构建图形用户界面的类。这些类可以分为三...

Global site tag (gtag.js) - Google Analytics