Ok, I know this is a stupid-easy question, but how do I "gather" (best word I could think of) an image?

JFrame frame = new JFrame();
Image programIcon = (...?...);
frame.setIconImage(programIcon);

My main issue is that since Image is abstract, I can't exactly go " = new Image("yadda.png");" - like I could with javax.swing.ImageIcon.

Recommended Answers

All 6 Replies

Ok, I know this is a stupid-easy question, but how do I "gather" (best word I could think of) an image?

JFrame frame = new JFrame();
Image programIcon = (...?...);
frame.setIconImage(programIcon);

My main issue is that since Image is abstract, I can't exactly go " = new Image("yadda.png");" - like I could with javax.swing.ImageIcon.

Hi!

Use the derived class ImageIcon.

this.setIconImage(new ImageIcon("...???...")).getImage());

Greetz

If you do want to ever create an Image, you can use: JPanel.createImage(width, height, null) (it's a member function of quite a few panels, frames, etc). If you want to load an image from a file or webserver use Toolkit.getDefaultToolkit().getImage((url|path)). Images/Graphics can be weird, I've never understood why Image is an abstract class.

I've never understood why Image is an abstract class.

That's pretty simple. You can use a derived class of Image for every kind of image format, even for own formats. So you need to implement some functions, like getGraphics(), on your own...

E.g. you are able to write a class "ZippedImage", which supports reading a single GZipped image file from somewhere. As this class is derived from Image, you can handle it like every other Image object, and it makes no difference to your (e.g.) JFrame, whether you are using ImageIcon or ZippedImage.

Mm indeed, but it doesn't have to be abstract, I suppose it helps programmers write the extensions (can't compile unless you override abstracts) and even more so in an IDE.

I have an app, and the means I used for creating buffers was to pass a JPanel (or generated Image) right to the "bottom" of the system, either to generate images or work on existing images; not ideal I know. Incidently, what's the actual class of Image that's returned with a call to createImage()?

Matt

Mm indeed, but it doesn't have to be abstract, I suppose it helps programmers write the extensions (can't compile unless you override abstracts) and even more so in an IDE.

I have an app, and the means I used for creating buffers was to pass a JPanel (or generated Image) right to the "bottom" of the system, either to generate images or work on existing images; not ideal I know. Incidently, what's the actual class of Image that's returned with a call to createImage()?

Matt

Well, making Image abstract was one way to do this. Making it an interface would have been the other... but IMHO worse...

Of course, getImage() returns an Image-object, so it doesn't matte what the instance of the object's type is, as long as it's derived from Image.

That's the idea if making it abstract: As long as it's overwriting the abstract methods from Image, it's usable as an image. (Basically that's the idea of any abstract class. :lol: )

Thanks alot for your replies :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.