Java中Shape对象转换为Image对象

发布于 2024-10-27 04:45:15 字数 87 浏览 3 评论 0原文

如何将 Rectangle2D.Double 等 Shape 对象转换为 Image 对象?

这样我就可以使用 Shape 对象来替换鼠标光标。

How do I convert a Shape object like Rectangle2D.Double to an Image object?

This way I can use a Shape object for a mousecursor replacement.

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

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

发布评论

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

评论(2

謌踐踏愛綪 2024-11-03 04:45:15

执行 < code>draw(shape)BufferedImage 中,如此处所示。

Do draw(shape) in a BufferedImage, as shown here.

感性 2024-11-03 04:45:15

您必须创建一个图像对象,其中在正确的位置包含正确的像素。

一种方法是这样的:

public Image makeImage(Shape s) {
    Rectangle r = s.getBounds();
    Image image = new BufferedImage(r.width, r.height, BufferedImage.TYPE_BYTE_BINARY);
    Graphics2D gr = image.createGraphics();
    // move the shape in the region of the image
    gr.translate(-r.x, -r.y);
    gr.draw(s);
    gr.dispose();
    return image;
}

不过,您可能想要使用另一种颜色模型,让您的形状以透明背景显示,而不是白底黑字或其他颜色。

You'll have to create an image object which contains the right pixels at the right locations.

One way would be something like this:

public Image makeImage(Shape s) {
    Rectangle r = s.getBounds();
    Image image = new BufferedImage(r.width, r.height, BufferedImage.TYPE_BYTE_BINARY);
    Graphics2D gr = image.createGraphics();
    // move the shape in the region of the image
    gr.translate(-r.x, -r.y);
    gr.draw(s);
    gr.dispose();
    return image;
}

You may want to use another color model, though, to have your shape show up with transparent background, instead of black-on-white or otherwise around.

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