Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which Image to Use

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?

like image 897
Anonymous181 Avatar asked May 08 '12 22:05

Anonymous181


People also ask

Which image format should I use?

The PNG file format is optimized for digital use, making it the most commonly used image format. It also supports more colors than the GIF format – PNG can handle up to 16 million colors, while GIF only supports 256 colors.

Which is better PNG or JPG?

Because of their different compression processes, JPEGs contain less data than PNGs — and therefore, are usually smaller in size. Unlike JPEGs, PNGs support transparent backgrounds, making them preferred for graphic design.

Should I use PNG or JPEG on my website?

The general rule is to use JPGs for photographs, images that don't have a transparent background, and other memory intensive files. And to choose PNGs for graphics, files with transparent backgrounds, and other images where clarity and color vibrancy are important.


2 Answers

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.

like image 139
CodeBlind Avatar answered Oct 27 '22 01:10

CodeBlind


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.

like image 26
Yücel Ceylan Avatar answered Oct 27 '22 00:10

Yücel Ceylan