-1

I am loading an image in Java Swing and want to display it in JPanel..The problem I am facing is that despite of image being loaded, it is not showing in JPanel..I can say that image is successfully loaded because it is showing me the correct path of the loaded image in my label..

Here is my code..

private static final int IMG_WIDTH = 120;
private static final int IMG_HEIGHT = 120;
JLabel label;
ImageIcon photo;
WritableRaster raster;
DataBufferByte data;
File image;

JFileChooser chooser;
FileNameExtensionFilter filter;
chooser = new JFileChooser();
chooser.setCurrentDirectory(image);
filter = new FileNameExtensionFilter("jpeg, gif and png files", "jpg", "gif", "png");
chooser.addChoosableFileFilter(filter);

int i = chooser.showOpenDialog(this);
if (i == JFileChooser.APPROVE_OPTION) {
    image = chooser.getSelectedFile();
    jLabel8.setText(image.getAbsolutePath());
    try {
        BufferedImage originalImage = ImageIO.read(image);
        int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
        BufferedImage resizeImageJpg = resizeImage(originalImage, type);
        photo = new ImageIcon(toImage(resizeImageJpg));
        jPanel2.removeAll();
        label = new JLabel("", photo, JLabel.CENTER);
        label.setIcon(photo);
        jPanel2.add(label);
        setVisible(true);
            //converting buffered image to byte array
        raster = resizeImageJpg.getRaster();
        data = (DataBufferByte) raster.getDataBuffer();

     } catch (IOException e) {
            System.out.println(e.getMessage());
        }

        repaint();
        chooser.setCurrentDirectory(image);
    }

  public Image toImage(BufferedImage bufferedImage) {
    return Toolkit.getDefaultToolkit().createImage(bufferedImage.getSource());
}


private static BufferedImage resizeImage(BufferedImage originalImage, int type) {
    BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type);
    Graphics2D g = resizedImage.createGraphics();
    g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null);
    g.dispose();

    return resizedImage;
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Java Enthusiast
  • 654
  • 7
  • 19
  • To test write something in the label in which you are adding the image, and see if it is visible. – Ninad Pingale Jun 11 '14 at 11:11
  • It is not visible and jPanel2 is still blank.. – Java Enthusiast Jun 11 '14 at 11:15
  • 1
    try label.setVisible(true) if not you might have to add Layout to panel or set bounds to label. – Ninad Pingale Jun 11 '14 at 11:19
  • 1
    1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete and Verifiable Example). 2) One way to get image(s) for an example is to hot-link to the images seen in [this answer](http://stackoverflow.com/a/19209651/418556). – Andrew Thompson Jun 11 '14 at 11:22
  • @JavaLearner adding Layout to my jPanel2 worked..thnk u for your help :) – Java Enthusiast Jun 11 '14 at 11:22
  • 1
    `jPanel2.removeAll();` Either use a `CardLayout` or add a single label to it at start up and simply call `setIcon` using the new (resized) image. – Andrew Thompson Jun 11 '14 at 11:25
  • 1
    Take a look at [maintaining aspect ratio of JPanel background image](http://stackoverflow.com/questions/11959758/java-maintaining-aspect-ratio-of-jpanel-background-image/11959928#11959928) for a discussion on techniques for resizing images in Swing – MadProgrammer Jun 11 '14 at 11:39
  • 2
    Unless you really have to, try avoid creating a new `JLabel` and instead use it's `setIcon` property... – MadProgrammer Jun 11 '14 at 11:39
  • More examples [here](http://stackoverflow.com/q/15961412/230513). – trashgod Jun 11 '14 at 12:43

1 Answers1

1

First, forget the toImage() method, BufferedImage already extends the Image class.

Then try to use

    try {
        BufferedImage originalImage = ImageIO.read( image );
        int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
        BufferedImage resizeImageJpg = resizeImage( originalImage, type );

        //forget the toImage() method, BufferedImage already extends the Image class.
        photo = new ImageIcon( resizeImageJpg );
        //And by the way, if you remove components and add new ones, use validate() instead.

        //And don't remove the JLabel, just change the icon, it will repaint automatically.
        label.setIcon(photo);
    }
    catch ( java.io.IOException iOException ) { 
        System.out.println(e.getMessage());
    }

And for the resized BufferedImage, use AffineTransform to scale the image to the new size.

private static BufferedImage resizeImage(BufferedImage originalImage, int type) {
    BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type);

    //Calculate de scale ratio;
    double scaledDx = ((IMG_WIDTH*1.0d)/originalImage.getWidth());
    double scaledDy = ((IMG_HEIGHT*1.0d)/originalImage.getHeight());

    AffineTransform resizeAffine = AffineTransform.getScaleInstance( scaledDx, scaledDy );
    BufferedImageOp buffResized = new AffineTransformOp( resizeAffine, null );

    Graphics2D g2 = resizedImage.createGraphics();
    g2.drawImage( resizedImage, buffResized, 0, 0 );
    g2.dispose(); //Don't forget to dispose to release resources.
}

I hope I could help.

Harald K
  • 26,314
  • 7
  • 65
  • 111
saclyr
  • 161
  • 5