本文介绍了如何获得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的缩放实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!