0

I've already read the post Java / Swing how to deal with different screen DPI and density settings?, regarding the jLabel images pixelization on new JDKs (above 9) and high DPI screens, but it didn't do the trick.

Here is my case and code: My old projects using png images worked fine (no pixelization) on old DPI screens, but now, all images are pixelized. So, I'm burning out my brain here to solve this issue and created a new Java project using a high-resolution PNG image.

The Problem: Doesn't matter how hard I try, the image is not getting resized to the jLabel size. Here is the code:

initComponents(); //Initialize the JFrame components... 
ImageIcon iconWorkHours = new ImageIcon(getClass().getResource("/horkhours/WorkHours.png"));
this.lblLogo.setPreferredSize(new Dimension(135, 133));
Image imageWorkHours = iconWorkHours.getImage();
imageWorkHours.getScaledInstance(lblLogo.getWidth(), lblLogo.getHeight(), Image.SCALE_SMOOTH);
BufferedImage bufImgWH = new BufferedImage(lblLogo.getWidth(), lblLogo.getHeight(), BufferedImage.TYPE_INT_ARGB);
bufImgWH.getGraphics().drawImage(imageWorkHours, 0, 0, null);
this.lblLogo.setIcon(new ImageIcon(bufImgWH));

The result keeps looking like this: Image doesn't resize

Methark
  • 95
  • 8
  • 2
    You are scaling a raster image to a larger size. It is not possible to do that in a way that prevents pixelation for any and all cases. AFAIK, the only two solutions are 1. use a raster image of the correct large size so you need not scale and 2. use vector graphics instead of raster images. – Thomas Behr Jan 31 '23 at 11:40
  • `imageWorkHours.getScaledInstance` is ignore the result of the call. You should also avoid using `setPreferredSize` and let the label contents determine the "best fit" – MadProgrammer Jan 31 '23 at 11:51
  • Hi! Thanks for the feedback. But the image is bigger (not smaller) than the jLabel size. I've also tried to simply use the image with the same jLabel width and height, it stays pixelated too. When you say to use vector images, what do you mean? SVG? – Methark Jan 31 '23 at 17:51

0 Answers0