1

I am trying to scale an image in order for it to fit in with the JLabel size and I come across this error

at java.awt.image.ReplicateScaleFilter.<init>(Unknown Source)
at java.awt.image.AreaAveragingScaleFilter.<init>(Unknown Source)
at java.awt.Image.getScaledInstance(Unknown Source)
at ma.ensao.Practice.main(Practice.java:49)

here is the whole code I am using

import java.util.*;
import java.io.IOException;
import java.io.*;
import javax.imageio.ImageIO;
import java.awt.image.*;
import java.awt.*;
import javax.swing.*;
public class Practice {
public static void main(String[] args) throws IOException{

BufferedImage img = null;
        JLabel label=new JLabel();
        try {
            img = ImageIO.read(new File("med/mido.jpg"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        Image imgS = img.getScaledInstance(label.getWidth(), label.getHeight(),
                Image.SCALE_SMOOTH);
        ImageIcon imageIcon = new ImageIcon(imgS);
        label.setIcon(imageIcon);

        JPanel p=new JPanel();
        p.add(label);

        JFrame f = new JFrame();
        f.setSize(500,500);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLocationRelativeTo(null);
        f.add(p);
        f.setVisible(true);

}

}

I wonder why I have gotten this error? thanks.

Mohammed NAIMI
  • 177
  • 1
  • 11
  • `JLabel` is a complex component, which makes it hard to customise, a different approach might be to use a custom component and scale the image based on you needs and the size of the component itself - [for example](https://stackoverflow.com/questions/14548808/scale-the-imageicon-automatically-to-label-size/14553003#14553003) – MadProgrammer Nov 18 '20 at 22:20

1 Answers1

2
JLabel label=new JLabel();
System.out.println( label.getSize() );

Try the above code.

I would guess the issue is that the size of the label is (0, 0), so it would be hard to scale an image to that size.

Instead you should pick the size that you want the image to be, then get the scaled instance and then create the label using the scaled image.

Edit:

I want to make the size of the image equal to the size of label whenever the user change the size of the window,

So you want the size of the image to dynamically scale. So you can't just create an initial scaled image.

Also you can't add the JLabel to a JPanel and then add the panel to the frame because the default layout manager of the JPanel is the FlowLayout, which will always respect the preferred size of the label.

So you have a couple of options:

  1. Do custom painting on a JPanel. In the paintCompononent(...) method you would get the current size of the panel using the getWidth() and getHeight() methods of the JPanel. Then you would use the drawImage(...) method of the Graphics class to draw the image. This custom panel would then be added directly to BorderLayout.CENTER of the frame to allow the panel to dynamically resize. Read the Swing tutorial on Custom Painting for more information and working examples to get your started.

  2. Use the Stretch Icon. This is a custom class that will allow the Icon to dynamically resize. So you would create the JLabel using the StretchIcon and then add the label to the BorderLayout.CENTER of the frame.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • you are right label is (0,0), and when I tried given random width and height to getScaledInstance it works, but I want to make the size of the image equal to the size of label whenever the user change the size of the window, so how can I prevent label from having (0,0) to use it as a parameter – Mohammed NAIMI Nov 18 '20 at 22:09
  • thank you @camickr, the first solution works for me but the resulting image is of low quality, is there a method to circumvent this using drawImage? – Mohammed NAIMI Nov 19 '20 at 19:59
  • You can't scale an image larger and keep the same quality. You will always start to get pixelation. – camickr Nov 20 '20 at 00:21
  • ah, I understand @camickr thanks for your interaction. – Mohammed NAIMI Nov 20 '20 at 11:03