0

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
    @Zavior I'd say not, you can pass a `BufferedImage` to any method that accepts `Image` as `BufferedImage` extends `Image`, but a `List` of `BufferedImage`s is in no way compatible for with `Image`... – MadProgrammer Feb 27 '14 at 07:14
  • 1
    This is clearly *not* a duplicate of the question that so far, 4 inattentive users have voted to close this question for... Reviewers, *please* pay attention when reviewing close votes. – Sheridan Feb 27 '14 at 10:03

2 Answers2

3

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...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

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.

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64