简体   繁体   中英

Convert List<BufferedImage> to Image

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.

You could consider using...

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

List is a list of stuff (or technically Object s, in your case, BufferedImage s), 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...

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM