This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Casting problem. Image to BufferedImage Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Casting problem. Image to BufferedImage" Watch "Casting problem. Image to BufferedImage" New topic
Author

Casting problem. Image to BufferedImage

Luka Juric
Greenhorn

Joined: Mar 06, 2007
Posts: 8



I get this exception -> java.lang.ClassCastException: sun.awt.image.ToolkitImage cannot be cast to java.awt.image.BufferedImage
So problem is that I have image and I want to write it on local disc as xxx.jpg file.
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
if you just want to draw to the image you import...

use...

Graphics myGraphics = image.getGraphics();

then in paintComponent you can just do

myGraphics.drawString("blah",20,30);

and etc..


Justin


You down with OOP? Yeah you know me!
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
if you want to write the file to local disk

just say:

File file = new File(URL url);

and it will create a file;

Justin
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Pseudo suggestion.

edit: removed erroneous comment.
[ March 09, 2007: Message edited by: Craig Wood ]
Luka Juric
Greenhorn

Joined: Mar 06, 2007
Posts: 8
Thanks Craig, but still doesn't work. It saves me all black picture(384,288).

I test loading picture with

public void paint(Graphics g) {
g.drawImage(image, 0, 0, this);
}

and it works ok.

I try to test drawing with adding this

g2.drawImage(image, 0, 0, this);
g2.setColor(Color.white);
g2.fillOval(0, 0, width, height);
g2.dispose();

and than it save me white circle on black background, but this line of code g2.drawImage(image, 0, 0, this); like just don't exsist.
What can I try now?
Luka Juric
Greenhorn

Joined: Mar 06, 2007
Posts: 8
Justin,

You didn't understand me. I need to save xxx.jpg file on my local disc, but problem is that this line of code

ImageIO.write(rendImage, "jpg", file);

works only with RendererImage, and not with Image , and I have Image.
Luka Juric
Greenhorn

Joined: Mar 06, 2007
Posts: 8
You write good code, it works, I make some mistake.

Thanks Craig,

Luka
Ricky Clarkson
Ranch Hand

Joined: Jul 27, 2006
Posts: 131
Just a small note - any time you're doing casting there's something wrong with the API you're using, or you're using it wrong. The core APIs aren't immune to this (Object.equals for example - it's not possible to sensibly override that without a cast).

In other words, casting is for magicians, not programmers!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Casting problem. Image to BufferedImage
 
Similar Threads
Convert between AWT Image and byte array
Java Code to Convert RTF to JPG
Image Coordinates and Scaling
Dynamic JPG with java
Saving an ImageIcon to a file