What is the advantage of using Bufferedimage for images?

Asked

Viewed 3,069 times

3

I want to know the difference in using BufferedImage and the method graphics to draw the images to the 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();
        }
    }
  • 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.

  • I didn’t understand

  • 1

    Comparing Image and Paint methods of this code doesn’t do much.

  • I searched the java api, which I understood is a subclass of image, which has some features.

  • 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.

1 answer

3


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.

  • In short, they are different things that can be used together, but never replace each other, correct?

  • 1

    @diegofm Yes, correct.

  • I can’t use the paintComponent() method, it’s not working.

  • @Juao É paintComponent(Graphics). However, I would like to know, what class is this? And what is the superclass?

  • http://pastebin.com/LuSGDDe7 I was trying to implement this tutorial http://www.pontov.com.br/site/index.php/java/48-java2d/123-a-primeira-animacao.

  • @Juao Sorry, the name is paintComponents, in the plural, with the s in the end. My fault. I’ve already edited my answer to fix this.

  • That’s the one I’m wearing.

  • @Juao I edited everything and put it in http://pastebin.com/nkKvfF5d - The name of the method was paintComponent without the s same. Also, I put it inside the JPanel and not in the JFrame. Another problem is calling the repaint() from within their Thread auxiliary, since this is an interaction with the AWT/Swing made from outside the Event Dispatch Thread. I solved it with the SwingUtilities.invokeLater. Only these simple changes have made enough difference - Your ball flashed, now it’s not.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.