What is the advantage of using BufferedImage for images?

3

I want to know the difference in using BufferedImage and the graphics method to draw the images to ImageIcon .

Here is an example:

@Override
    public void paint(Graphics g) {

        g.drawImage(bfImage, 0, 0, this);
        g.drawImage(bfImage2, 10, 350, this);
    }
    public void desenharImagem() {

        try{

            bfImage = ImageIO.read(getClass().getResource("Imagens/cenario.jpg"));
            bfImage2 = ImageIO.read(getClass().getResource("Imagens/0.png"));

        }catch(IOException e) {
            e.printStackTrace();
        }
    }
    
asked by anonymous 14.01.2017 / 21:54

1 answer

3

The interface Image is the one that models the behavior of objects representing 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.

Graphics (as well as its subclass Graphics2D ) does not represents an image, and yes it is an object that does the drawing in images.

A certain analogy that would make (although imperfectly) is that "% w / o% is a role, while% w / o% is a pen ".

As you yourself have shown, the method for drawing an image in a BufferedImage is as follows:

BufferedImage bi = ...;
g.drawImage(bi, px, py, this);

There is also the method to get a Graphics from a Graphics , which is the Graphics ":

Graphics2D g = bi.createGraphics();

Using BufferedImage , you can even access the pixels individually with the

14.01.2017 / 23:42