• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Casting problem. Image to BufferedImage

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



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.
 
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pseudo suggestion.

edit: removed erroneous comment.
[ March 09, 2007: Message edited by: Craig Wood ]
 
Luka Juric
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You write good code, it works, I make some mistake.

Thanks Craig,

Luka
 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic