如何获得bufferedImage的缩放实例

编程入门 行业动态 更新时间:2024-10-13 20:19:31
本文介绍了如何获得bufferedImage的缩放实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想得到缓存图像的缩放实例,我做了:

I wanted to get scaled instance of a buffered image and I did:

public void analyzePosition(BufferedImage img, int x, int y){ img = (BufferedImage) img.getScaledInstance(getWidth(), getHeight(), Image.SCALE_SMOOTH); .... }

但我得到一个例外:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: sun.awt.image.ToolkitImage cannot be cast to java.awt.image.BufferedImage at ImagePanel.analyzePosition(ImagePanel.java:43)

我想要转换为 ToolkitImage 然后使用方法 getBufferedImage 我在其他文章中读到过。问题是没有类,如 sun.awt.image.ToolkitImage 我无法转换它,因为Eclipse甚至没有看到这个类。我使用 Java 1.7 和 jre1.7 。

I wanted then to cast to ToolkitImage then use the method getBufferedImage I read about in other articles. The problem is there is no class such as sun.awt.image.ToolkitImage I cannot cast to it because Eclipse does not even see this class. I use Java 1.7 and jre1.7.

推荐答案

你可以使用TookitImage创建一个新的图像,一个BufferedImage。

You can create a new image, a BufferedImage with the TookitImage.

Image toolkitImage = img.getScaledInstance(getWidth(), getHeight(), Image.SCALE_SMOOTH); int width = toolkitImage.getWidth(null); int height = toolkitImage.getHeight(null); // width and height are of the toolkit image BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics g = newImage.getGraphics(); g.drawImage(toolkitImage, 0, 0, null); g.dispose(); // now use your new BufferedImage

更多推荐

如何获得bufferedImage的缩放实例

本文发布于:2023-11-25 20:37:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631265.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:缩放   如何获得   实例   bufferedImage

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!