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!

dannYonkeys
  • 81
  • 1
  • 1
  • 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? – Oliver Charlesworth Apr 07 '14 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? – Epiglottal Axolotl Apr 07 '14 at 21:33
  • 3
    What `import` statement are you using at the top of your file? – Sam Apr 07 '14 at 21:35
  • Maybe you should take a look at this answer. http://stackoverflow.com/a/9132226/1571550 – MMendes Apr 07 '14 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). – Harald K Apr 08 '14 at 10:23
  • Thanks, commenters. Yeah, that's the problem- I've been using javafx.scene.image.Image, I'll edit my post to be more clear. – dannYonkeys Apr 09 '14 at 19:16

4 Answers4

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!

Josh
  • 7
  • 4
dannYonkeys
  • 81
  • 1
  • 1
  • 6
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
Shreyos Adikari
  • 12,348
  • 19
  • 73
  • 82
  • 1
    I agree, it *should* just work. Except that the OP states that their compiler complains at this – Sam Apr 07 '14 at 21:45
-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.)

Laurel
  • 5,965
  • 14
  • 31
  • 57
Julian
  • 1,665
  • 2
  • 15
  • 33
-2
Image image = new ImageIcon(bufferedImage).getImage();

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

Image image = (Image) bufferedImage;
Chris Smith
  • 2,928
  • 4
  • 27
  • 59
  • There's no need to do anything. A `BufferedImage` *is an* `Image` (ie. `BufferedImage extends Image`). – Harald K Apr 08 '14 at 10:18
  • 2
    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). – Chris Clark Feb 21 '17 at 14:10
  • `The method ImageIcon(BufferedImage) is undefined`. – Hasen Mar 09 '22 at 06:23