绘制到图像的组件使用难看的字体渲染

发布于 2024-10-15 06:53:18 字数 810 浏览 0 评论 0原文

当我将 Swing 组件渲染到图像时,字体与在屏幕上渲染组件时的字体不同。这是显示差异的图像:

在此处输入图像描述

这是代码:

public static BufferedImage renderComponent(Component component) {
    int width = component.getWidth();
    int height = component.getHeight();
    BufferedImage buffImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = (Graphics2D) buffImage.getGraphics();
    g.setFont(component.getFont());
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    component.paint(g);
    g.dispose();
    return buffImage;
}

我应该更改什么才能完美Swing 组件的屏幕截图? (该应用程序在 Windows 7 上运行)

When I'm rendering a Swing component to an image the fonts are different then when the component is rendered on screen. Here is an image showing the difference:

enter image description here

And this is the code:

public static BufferedImage renderComponent(Component component) {
    int width = component.getWidth();
    int height = component.getHeight();
    BufferedImage buffImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = (Graphics2D) buffImage.getGraphics();
    g.setFont(component.getFont());
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    component.paint(g);
    g.dispose();
    return buffImage;
}

What should I change to make a perfect screen shot of a Swing component? (The app is running on Windows 7)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半枫 2024-10-22 06:53:18

我应该更改什么才能制作出完美的 Swing 组件屏幕截图?

我猜你不应该提供渲染提示。

我为此使用 Screen Image 类(它基本上是您的代码还内置了一些其他功能)。

What should I change to make a perfect screen shot of a Swing component?

I would guess you should not be providing rendering hints.

I use the Screen Image class for this (it is basically your code with a few other features built in).

凉城 2024-10-22 06:53:18

在比较ScreenImage类(由camickr建议)之后我找到了问题的原因。如果我创建一个类型设置为 TYPE_INT_RGB 而不是 TYPE_INT_ARGB 的 BufferedImage,字体就很好。

After comparing the ScreenImage class (suggested by camickr) I found the cause of the problem. If I create a BufferedImage with the type set to TYPE_INT_RGB instead of TYPE_INT_ARGB the fonts are fine.

無處可尋 2024-10-22 06:53:18

类机器人

public synchronized BufferedImage createScreenCapture(Rectangle screenRect)

Class Robot

public synchronized BufferedImage createScreenCapture(Rectangle screenRect)

?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文