2

Why does the following code show a black image instead of the picture? How to properly extend BufferedImage?

class SizeOfImage {

    public static void main(String[] args) throws Exception {
        URL url = new URL("http://cloudbite.co.uk/wp-content/uploads/2011/03/google-chrome-logo-v1.jpg");
        final BufferedImage bi = ImageIO.read(url);
        final String size = bi.getWidth() + "x" + bi.getHeight();

        final  CustomImg cstImg = new CustomImg(bi.getWidth(), bi.getHeight(), bi.getType());

        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                JLabel l = new JLabel(size, new ImageIcon(cstImg), SwingConstants.RIGHT);
                JOptionPane.showMessageDialog(null, l);
            }
        });
    }

    public static class CustomImg extends BufferedImage {

        public CustomImg(int width, int height, int type){
            super(width, height, type);
        }

    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Radek
  • 1,403
  • 3
  • 25
  • 54
  • 8
    *"How to properly extend BufferedImage?"* Why do you need to extend it? Don't get me wrong, there are arcane situations in which you might extend it, but humor me. Edit: I just noticed that code neither extends BI, nor ever paints to the 2nd BI. – Andrew Thompson Nov 21 '11 at 16:14
  • Because I need a custom class extending buffered image the above code is only an example – Radek Nov 21 '11 at 16:16
  • 1
    @AndrewThompson 'Why do you need to extend it?' - OP: 'Because I need a custom class extending it' ... that's not a reason, just a repetition :-) Please answer the "why", it's crucial – kleopatra Nov 21 '11 at 16:53

2 Answers2

7

Probably because the downloaded image bi is never drawn onto the cstImg.

This line:

CustomImg cstImg = new CustomImg(bi.getWidth(), bi.getHeight(), bi.getType());

creates a new image based on the width, height and type of bi... not the content of bi. For that you probably want to do something like

cstImg.getGraphics().drawImage(bi, 0, 0, null);
aioobe
  • 413,195
  • 112
  • 811
  • 826
7

Size of Image

import java.awt.image.BufferedImage;
import java.awt.Graphics;
import javax.swing.*;
import javax.imageio.ImageIO;

import java.net.URL;

class SizeOfImage {

    public static void main(String[] args) throws Exception {
        URL url = new URL(
            "http://cloudbite.co.uk/wp-content/" +
            "uploads/2011/03/google-chrome-logo-v1.jpg");
        BufferedImage bi = ImageIO.read(url);
        final String size = bi.getWidth() + "x" + bi.getHeight();
        final  CustomImg cstImg = new CustomImg(
            bi.getWidth(),
            bi.getHeight(), bi.
            getType());

        // paint something to the new image!
        Graphics g = cstImg.createGraphics();
        g.drawImage(bi,0,0,null);
        g.dispose();

        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                JLabel l = new JLabel(
                    size,
                    new ImageIcon(cstImg),
                    SwingConstants.RIGHT );
                JOptionPane.showMessageDialog(null, l);
            }
        });
    }

    public static class CustomImg extends BufferedImage {
        public CustomImg(int width, int height, int type){
            super(width, height, type);
        }
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • +1 for succinct. See also this [thread](http://groups.google.com/group/comp.lang.java.help/browse_thread/thread/e74886dace754256). – trashgod Nov 21 '11 at 22:08