Which Image to Use
Asked Answered
C

2

6

I've read the java api, but I still do not understand the main difference between:

1) ImageIcon 2) BufferedImage 3) VolatileImage 4) Image

Can someone tell me when each one is used?

Coatbridge answered 8/5, 2012 at 22:21 Comment(0)
R
12

I wouldn't call this explanation below the absolute standard of using Java Image types, but these are the rules of thumb that I follow:

1. ImageIcon

This is typically used when you have small images that you want to add to buttons or to use as window icons. These may be created directly from anything that implements the Image interface.

2. BufferedImage

Typically used when you need to manipulate individual pixels within an image, or when you want to double-buffer a custom paint(Graphics g) method. The image resides in RAM, so it can take up a lot of space, and modifications to BufferedImage instances are not typically hardware-accelerated.

3. VolatileImage

Hardware-accelerated image, so it's fast, but you run the risk of the hardware-backed buffer being overwritten before you're done painting (although this rarely happens and according to Oracle, is only an issue for Windows-based machines). It's a little more difficult to use than BufferedImage for double-buffering custom paint(Graphics g) methods, but is well worth it if you find yourself doing a lot of pre-processing before rendering to screen.

4. Image This is basically just an interface that defines some bare-bones functionality that every Image should have. You should use this when you don't need to modify an image's contents and/or want to make methods that handle read-only-image data most flexible.

Roby answered 8/5, 2012 at 23:2 Comment(0)
A
2

Also, ImageIcon implements serializable so you can send it through java sockets. If you have Image objects, you have to convert them to ImageIcon and send. When the client side took the ImageIcons, it can convert them to Images again.

Antonia answered 8/5, 2012 at 23:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.