0

I know they are quite a few forums that kinda talks about this, but my problem is that I have a JLabel with an icon which I do NOT want to scale when resolution is <> 100% ! I.e. (sorry, in french):

enter image description here

Initially, I set my JLabel icon with something like:

lblImage.setIcon( getNoteIcon() );

//...

ImageIcon getNoteIcon(){
    return new ImageIcon( getClass().getResource('path/to/file.png') );
}

This results are that my image displays blurry when HiDPI resolution is set to higher than 100%. Examples:

At 100%:

enter image description here

And at 150%:

enter image description here

You can see that the music note image is fuzzier in the 150% resolution.

What I want: I DO NOT want it to up or downscale the image! I want to keep the original size, unless I can provide different image sizes for various resolutions.

That being said, I looked and tried to use the BaseMultiResolutionImage class to deal with resolutions, passing along an array of the "same size image", but I get the same results when I change the resolution. This is the code I tried :

ImageIcon getNoteIcon(){
    List<Image> imgList = new ArrayList<Image>();

    /* all images are width = 108 height = 83 */
    imgList.add(ImageIO.read(new File('path/to/file.png'))); // 100%
    imgList.add(ImageIO.read(new File('path/to/file.png'))); // 125%
    imgList.add(ImageIO.read(new File('path/to/file.png'))); // 150%
    imgList.add(ImageIO.read(new File('path/to/file.png'))); // 175%
    imgList.add(ImageIO.read(new File('path/to/file.png'))); // 200%

    BaseMultiResolutionImage multiResolutionImage = new BaseMultiResolutionImage(imgList.toArray(new Image[0]));

    return new ImageIcon(
        multiResolutionImage
    );
}

I don't understand what I'm doing wrong, or if I'm even using BaseMultiResolutionImage correctly. Finally, if I do manage say to give 3 different images for 3 resolutions, (i.e. 100, 125, 150%), is there a way to tell it the "default" one to use when I do not have an image for a given resolution (i.e. 200%) ?

Can anyone shed a light on all of this? Seems like a lot of confusion on my end just to get an image to NOT scale in my window...

Much thanks! Pat


UPDATE AFTER "PROPER" USE OF BaseMultiResolutionImage:

try {
    List<Image> imgList = new ArrayList<Image>();

    imgList.add(ImageIO.read(getClass().getResource("images/notes/hidpi_100/file.png"))); // 100% - 108x83
    imgList.add(ImageIO.read(getClass().getResource("images/notes/hidpi_125/file.png"))); // 125% - 135x104
    imgList.add(ImageIO.read(getClass().getResource("images/notes/hidpi_150/file.png"))); // 150% - 162x124
    imgList.add(ImageIO.read(getClass().getResource("images/notes/hidpi_175/file.png"))); // 175% - 189x145
    imgList.add(ImageIO.read(getClass().getResource("images/notes/hidpi_200/file.png"))); // 200% - 216x166

    BaseMultiResolutionImage multiResolutionImage = new BaseMultiResolutionImage(imgList.toArray(new Image[0]));
    ImageIcon icon = new ImageIcon(multiResolutionImage);

    return icon;
}
catch(IOException ex){
    ex.printStackTrace();
    return null;
}

On screen font DPI set to 175% in Windows, this results in:

Before using BaseMultiResolutionImage

enter image description here

After using BaseMultiResolutionImage

enter image description here

Here is a comparison of what I get (left), with the actual image file it's supposed to show on the right hand side (mind you Paint doesn't take into account background transparency):

enter image description here

Any ideas as to why this "choppy" image is showing? What am I missing ?

Thanks again. Pat

Pat
  • 449
  • 1
  • 4
  • 14
  • Try using a jlabel constructor with inageicon and setsize() and setminimumsize() and setmaximumsize() on the jlabel. – Samuel Marchant Feb 19 '23 at 14:52
  • 1
    *I have a JLabel with an icon which I do NOT want to scale* - check out: https://stackoverflow.com/a/65742492/131872 for an Icon that won't scale. – camickr Feb 19 '23 at 15:15
  • [mcve] please .. – kleopatra Feb 19 '23 at 16:37
  • My bad! When I initially tried with the BaseMultiResolutionImage, I was literally using _the same file_ ! I think the image files themselves need to have increase size for corresponding resolution! i.e. my 100% resolution image file is at 108x83. The image file size to use on a 175% resolution should be of 189x145. So now, the images are no longer fuzzy ;) HOWEVER, I am running into another issue where the 175% is not blurry, BUT it is not smooth.. it is kinda choppy. An antialiasing problem or something ? I've updated question to show this... – Pat Feb 20 '23 at 11:21
  • Kinda difficult to explain this but the upscaling of the image information on simple geometric shape such as that would seem ok and be a little scratchy above 150 percent. If it had been a true colour 24 bit photograph image it would show severe quality degradation. Why not make the image (redraw new file) at the largest percent resolution and downscale in your method from the large master image. – Samuel Marchant Feb 24 '23 at 15:51
  • If you are using core image package, choose resize algorithm constant field value SMOOTH – Samuel Marchant Feb 24 '23 at 15:54
  • Sorry Samuel. I don't understand... "redraw new file.. and downscale from that" ?? Nor do I understand the using of core image package. Either way, I've submitted a new question on Stack that demos this problem with full working code and images: https://stackoverflow.com/questions/75564701/misunderstanding-or-usage-of-basemultiresolutionimage-upscaled-images-are-scra Hope you, or someone else can shed a light on this... it's annoying me and find this problem stupid to deal with :( Thank you. – Pat Feb 25 '23 at 10:21

0 Answers0