The interface Image
is the one that models the behavior of objects that represent images in Java.
The class BufferedImage
is an implementation of Image
which corresponds to images represented by a sequence of pixels stored entirely in memory.
The Graphics
(as well as his subclass Graphics2D
) nay represents an image, and yes is an object that makes the drawing in images.
A certain analogy that would give to do (although very imperfect) is that "the BufferedImage
is a role, while the Graphics
is a pen".
As you showed yourself, the method to draw an image in a Graphics
is as follows:
BufferedImage bi = ...;
g.drawImage(bi, px, py, this);
There is also the method to obtain a Graphics
from a BufferedImage
, which is the method createGraphics()
:
Graphics2D g = bi.createGraphics();
Using a BufferedImage
, you can even access the pixels individually with the methods getRGB(int, int)
and setRGB(int, int, int)
, but this is usually something very low level to make things like drawings of complex geometric shapes, textures, rendering texts with different colors and fonts in the image, etc. To do this from there manipulating the pixels directly is a hard and difficult work, and that’s where the classes Graphics
and Graphics2D
enter. They are abstractions that allow you to perform these operations.
The AWT, whenever it calls the method paint(Graphics)
, it will pass an instance of Graphics2D
. Then you can cast it safely. The class Graphics2D
offers a much richer set of methods than that offered directly by Graphics
.
The interface Icon
is intended to be a figure used to decorate Swing components. For example, MetalCheckBoxIcon
is the implementation that represents the checkbox square. Already the class ImageIcon
is the implementation of Icon
which is intended to represent an icon that matches some arbitrary image. Use ImageIcon
when only the BufferedImage
would already be necessary is conceptually wrong, because the purpose of the class ImageIcon
is more to be an adapter between the interfaces Image
and Icon
.
Ah, and don’t overdo the method paint(Graphics)
. You should write the paintComponent(Graphics)
. See more about this in that other answer of mine.
I didn’t understand your doubt, but if the comparison is between the Paint method and loading an image via Imageio the comparison doesn’t have much to do, because the purposes are different. The first draws on the screen, the second just loads the file as a resource to use in the code.
– user28595
I didn’t understand
– Juao
Comparing Image and Paint methods of this code doesn’t do much.
– user28595
I searched the java api, which I understood is a subclass of image, which has some features.
– Juao
bufferedImage is yes, but there is no relation to the Paint method. This method, as I said, serves to draw something on the screen, not necessarily an image, anything. The bufferedImage serves to load an image as a resource to be used in the code in a way that java understands, it does not draw anything.
– user28595