1

I want to re-size my ImageIcon to fit my jLabel. Using the answer from this post Scale the ImageIcon automatically to label size I am using

public jfrmHome() {
    initComponents();
    this.setLocationRelativeTo(null);
    ImageIcon iconimage;
    iconimage = new ImageIcon(getClass().getResource("/org/me/musiconweb/resources/Music-icon.png"));
    BufferedImage bi = new BufferedImage(iconimage.getIconWidth(), iconimage.getIconHeight(), BufferedImage.TYPE_INT_RGB);
    Graphics g = bi.createGraphics();
    iconimage.paintIcon(null, g, 0,0);
    g.dispose();
    BufferedImage resizedimage=resize(bi,jlblPicture.getWidth(), jlblPicture.getHeight());
    ImageIcon resizedicon=new ImageIcon(resizedimage);
    jlblPicture.setIcon(resizedicon);
}

This re-sizes the Image but i have a little problem. The background of the image becomes black instead of white that it was

This

original image

turns to

error image

Please what am i doing wrong?

Community
  • 1
  • 1
  • First, change `BufferedImage.TYPE_INT_RGB` to `BufferedImage.TYPE_INT_ARGB`. For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Oct 05 '13 at 16:08
  • @AndrewThompson How am i to post and SSCCE. I added only the problems that i had. –  Oct 05 '13 at 16:09
  • @AndrewThompson Wow thanks it worked. Could you please add it as answer so that i can mark it? –  Oct 05 '13 at 16:11
  • *"How am i to post and SSCCE."* That question suggests to me that you never followed the link and read what it had to say. Please do that now. – Andrew Thompson Oct 05 '13 at 16:14

3 Answers3

3

That image has transparency. So change BufferedImage.TYPE_INT_RGB to BufferedImage.TYPE_INT_ARGB

It is not obvious at SO on a white BG, but try this SSCCE & it becomes more clear..

import java.net.URL;
import javax.swing.*;

class ShowImage {

    public static void main(String[] args) throws Exception {
        final URL url = new URL("https://i.stack.imgur.com/1yeUy.png");
        Runnable r = new Runnable() {
            @Override
            public void run() {
                JLabel l = new JLabel(new ImageIcon(url));

                JOptionPane.showMessageDialog(null, l);
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency
        SwingUtilities.invokeLater(r);
    }
}

enter image description here

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
0

Your image has a transparent background. Therefore it shows as black when drawn on a newly created opaque image. Use

g.setColor(Color.WHITE);
g.fillRect(0, 0, jlblPicture.getWidth(), jlblPicture.getHeight());

if you want a white background. For transparent background, draw on TYPE_INT_ARGB image instead.

kiheru
  • 6,588
  • 25
  • 31
  • 1
    @PreciousTijesunimi Curiously enough, it does work for me. Anyway, the important thing is that a working solution was found. – kiheru Oct 05 '13 at 16:30
  • Thanks maybe it was ide problem. I use netbeans –  Oct 05 '13 at 16:38
0

Try out these codes

ImageIcon icon1 = new 
ImageIcon(getClass().getResource("\\image\\"+f1.getName()));  
BufferedImage bi = new 
BufferedImage(icon1.getIconWidth(),icon1.getIconHeight()
,   BufferedImage.TYPE_INT_RGB);
Graphics g = bi.createGraphics();
icon1.paintIcon(null, g, 0,0);
g.dispose();`
        //image resizing code...............>
ImageIcon resizedicon=new ImageIcon(bi.getScaledInstance(imglbl.getWidth(), 
imglbl.getHeight(),1 ));
imglbl.setBackground(new java.awt.Color(255, 255, 255));
imglbl.setIcon(resizedicon);