Convert List<BufferedImage> to Image

混江龙づ霸主 提交于 2020-01-03 04:32:15

问题


I have the following .ico image, read using image4j library:

List<BufferedImage> BI = ICODecoder.read("aImage.ico");

Next I want to set this image as a frame icon:

myFrame.setIconImage((Image)BI);

Error: java.lang.ClassCastException

I need to convert the type List<\BufferedImage> to the type Image. Any help would be appreciated.


回答1:


You could consider using...

myFrame.setIconImage(BI.get(0));

List is a list of stuff (or technically Objects, in your case, BufferedImages), where as setIconImage expects just one...

Alternatively, you could take advantage of of JFrame's capability of providing multiple different images at different resolutions by using...

myFrame.setIconImages(BI);

Which is probably what you were after in the first place...




回答2:


In this code

 List<BufferedImage> BI = ICODecoder.read("aImage.ico");

you are loading into a List

so when you try to do myFrame.setIconImage((Image)BI); you will not be able to convert a list into an image.

try a .get(0) on the list to return the Image.



来源:https://stackoverflow.com/questions/22061361/convert-listbufferedimage-to-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!