BufferedImage convert to Image

Is there anyway to convert a BufferedImage into an Image. I've seen the other way around but not this way. Any help would be greatly appreciated.
[160 byte] By [ManReda] at [2007-11-26 18:02:12]
# 1
You must be confused because BufferedImage extends Image so you can just do this:BufferedImage bufferedImage...;Image image = bufferedImage;
Rodney_McKaya at 2007-7-9 5:32:00 > top of Java-index,Security,Cryptography...
# 2
Yeah. If you want to do something to the BufferedImage that exists in Image, just do it on BufferedImage.
Lord_Jirachia at 2007-7-9 5:32:00 > top of Java-index,Security,Cryptography...
# 3

thank you.

See, I've got this method (below), that actually creates this image for me.

private static BufferedImage loadImage(final URL url) throws Exception{ //IOException {

BufferedImage image;

// Load the image through Image I/O

image = ImageIO.read(url);

//image = imageIO.read(url);

// Check that the image was loaded and that we can get a GC

if (image != null && !GraphicsEnvironment.isHeadless()) {

final GraphicsConfiguration gc = GraphicsEnvironment

.getLocalGraphicsEnvironment()

.getDefaultScreenDevice()

.getDefaultConfiguration();

// This probably can't be null if it's not headless

// Not sure though so check anyway

if (gc != null) {

// Attempt to create compatible image with same transparency

final BufferedImage img = gc.createCompatibleImage(

image.getWidth(), image.getHeight(),

image.getTransparency());

// Check that img was created and isn't the same type

// If it's the same type we don't need to convert obviously

if (img != null && image.getType() != img.getType()) {

// Use ColorConvertOp to convert the rasters from one

// color space to the other

// This is faster than using Graphics to draw on the new

// image in many cases, sometimes 100x faster

ColorConvertOp op = new ColorConvertOp(

image.getColorModel().getColorSpace(),

img.getColorModel().getColorSpace(), null);

op.filter(image, img);

image = img;

}

}

}

return image;

}

But, I need to call this from within the same class:

URL base;

try{

base = new URL ("http://home.cogeco.ca/~someplace/folder");

catch(Exception n)

{

System.out.println("Can't construct URL, first time around.");

}

try{

ll = loadImage(base); // right exception after calling method?

}

catch(Exception n)

{

System.out.println("problems");

}

For some reason, I can't figure out if I'm handling the exception properly. Any ideas why I'm getting the following error?

load: tetrisapp.PictureData.class can't be instantiated.

java.lang.InstantiationException: tetrisapp.PictureData

at java.lang.Class.newInstance0(Class.java:340)

at java.lang.Class.newInstance(Class.java:308)

at sun.applet.AppletPanel.createApplet(AppletPanel.java:778)

at sun.applet.AppletPanel.runLoader(AppletPanel.java:707)

at sun.applet.AppletPanel.run(AppletPanel.java:361)

at java.lang.Thread.run(Thread.java:619)

Tell me if you need more code. I'm guessing that the error is coming from the improper handling of the exception.

Message was edited by:

ManRed

Message was edited by:

ManRed

ManReda at 2007-7-9 5:32:00 > top of Java-index,Security,Cryptography...
# 4
java.lang.InstantiationException: tetrisapp.PictureDataThis means PictureData cannot be instantiated, usually because it is either an interface or an abstract class. It does not seem to indicate anything wrong with your image-loading code.
crwooda at 2007-7-9 5:32:00 > top of Java-index,Security,Cryptography...