2

I'm trying to split up an image of a deck of cards and store an individual card into an Image.

Image image; BufferedImage deck = ImageIO.read( new URL( "http://www.jfitz.com/cards/classic-playing-cards.png" ) ); image = deck.getSubimage( x, y, width, height ); 

incompatible types: BufferedImage cannot be converted to Image

Everywhere I've looked says that BufferedImage is an Image, this problem shouldn't exist. (Note: if this comes off looking like a class assignment, it is, but all we're supposed to do is load an image from a predetermined directory. I'm trying to take it a step further by using the URL for fun.)

EDIT 04/09/2014
I've been using different imports, so I guess the question I'm really trying to ask is how to convert a java.awt.image.BufferedImage to a java.scene.image.Image. Thanks for the help so far!

6
  • 3
    Indeed, BufferedImage is a sub-class of Image. What version of Java compiler are you using? Are you sure you're importing the correct Image class? Commented Apr 7, 2014 at 21:33
  • 1
    Are you sure that image is of type java.awt.image.Image and isn't an image from some other package? Commented Apr 7, 2014 at 21:33
  • 3
    What import statement are you using at the top of your file? Commented Apr 7, 2014 at 21:35
  • Maybe you should take a look at this answer. stackoverflow.com/a/9132226/1571550 Commented Apr 7, 2014 at 21:36
  • 1
    All answers below seems to assume that you are referring to java.awt.Image, in which case any BufferedImage can be assigned to image (and the answers doesn't make much sense). The problem is most likely that you have the wrong import (like javafx.scene.image.Image or similar), or that you indeed want a different kind of Image (in that case, you have to tell us which one to receive any meaningful help). Commented Apr 8, 2014 at 10:23

4 Answers 4

6

Once I knew what question to ask, google answered this really quickly. I found a quick fix on this blog.

BufferedImage tempCard = deck.getSubimage( x, y, width, height ); Image card = SwingFXUtils.toFXImage(tempCard, null ); 

Tried it out and works great!

Sign up to request clarification or add additional context in comments.

Comments

0

BufferedImage is a subclass of Image. You don't need to do any conversion.

You can just assign like:

Image image; BufferedImage deck = ImageIO.read( new URL( "http://www.jfitz.com/cards/classic-playing-cards.png" ) ); image = deck;// That will do 

1 Comment

I agree, it should just work. Except that the OP states that their compiler complains at this
-1

A BufferedImage extends Image which means that you can declare a BufferedImage like so:

Image myImage = ImageIO.read(new URL("my/url")); 

This starts getting into polymorphism and inheritance. Your object may be a BufferedImage, but a BufferedImage is still an Image and it can be used as one.

A more recognizable example perhaps would be instantiating an ArrayList as a List

List<MyType> aList = new ArrayList<>(); 

This allows this ArrayList to be compatible with all types of List objects, just like the BufferedImage.

Take a look at the API page for Image.

It lists all known subclasses of this abstract class, BufferedImage included. (There's only 2.)

Comments

-2
Image image = new ImageIcon(bufferedImage).getImage(); 

That's the way I usually do it... OR

Image image = (Image) bufferedImage; 

3 Comments

There's no need to do anything. A BufferedImage is an Image (ie. BufferedImage extends Image).
As others have pointed out, that's not entirely true. There are different Image classes (AWT, SWT, etc.). BufferedImage extends AWT image, but not SWT image and there's no easy way to go from BI to SWT image, which is the problem I am having (which brought me here).
The method ImageIcon(BufferedImage) is undefined.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.