1

I took a snip of a table in my application, and i want to display that snip in a tutorial section of the app. I can not get it to display with out being blurry. I don't know what is causing this. The picture is fine when i open it up in Windows photo viewer, but in the app it is blurry.

All my pictures are stored in a package called icons in my project, and they are not links to the pictures but actual copies.

Here is a minimal working example of my code. If anything that resizes the image, makes it lose quality how i can i prevent that?

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Color;
import java.awt.Component;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import java.awt.Insets;
import java.awt.Font;
import java.awt.Dimension;
import javax.swing.JTextArea;

@SuppressWarnings("serial")
public class example extends JFrame {

     private JPanel contentPane;

    public static void main(String[] args) {
         EventQueue.invokeLater(new Runnable() {
             public void run() {
                try {
                    example frame = new example();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public example() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 558, 373);
        contentPane = new JPanel();
        contentPane.setBackground(Color.WHITE);
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        GridBagLayout gbl_contentPane = new GridBagLayout();
        gbl_contentPane.columnWidths = new int[]{0, 0};
        gbl_contentPane.rowHeights = new int[]{0, 0};
        gbl_contentPane.columnWeights = new double[]{1.0, Double.MIN_VALUE};
        gbl_contentPane.rowWeights = new double[]{1.0, Double.MIN_VALUE};
        contentPane.setLayout(gbl_contentPane);

        JPanel panel = new JPanel();
        panel.setBackground(Color.WHITE);
        GridBagConstraints gbc_panel = new GridBagConstraints();
        gbc_panel.fill = GridBagConstraints.BOTH;
        gbc_panel.gridx = 0;
        gbc_panel.gridy = 0;
        contentPane.add(panel, gbc_panel);
        GridBagLayout gbl_panel = new GridBagLayout();
        gbl_panel.columnWidths = new int[]{0, 0, 0};
        gbl_panel.rowHeights = new int[]{0, 0, 0, 100, 0};
        gbl_panel.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
        gbl_panel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 
Double.MIN_VALUE};
        panel.setLayout(gbl_panel);

        JLabel lblWordsGoHere = new JLabel("Words go here");
        lblWordsGoHere.setFont(new Font("Tahoma", Font.PLAIN, 18));
        GridBagConstraints gbc_lblWordsGoHere = new GridBagConstraints();
        gbc_lblWordsGoHere.insets = new Insets(0, 0, 5, 0);
        gbc_lblWordsGoHere.gridx = 1;
        gbc_lblWordsGoHere.gridy = 0;
        panel.add(lblWordsGoHere, gbc_lblWordsGoHere);

        Component horizontalStrut = Box.createHorizontalStrut(20);
        horizontalStrut.setPreferredSize(new Dimension(20, 20));
        GridBagConstraints gbc_horizontalStrut = new GridBagConstraints();
        gbc_horizontalStrut.insets = new Insets(0, 0, 5, 5);
        gbc_horizontalStrut.gridx = 0;
        gbc_horizontalStrut.gridy = 1;
        panel.add(horizontalStrut, gbc_horizontalStrut);

        JLabel lblNewLabel = new JLabel("More words here...");
        lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 14));
        GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
        gbc_lblNewLabel.insets = new Insets(0, 0, 5, 0);
        gbc_lblNewLabel.anchor = GridBagConstraints.WEST;
        gbc_lblNewLabel.gridx = 1;
        gbc_lblNewLabel.gridy = 1;
        panel.add(lblNewLabel, gbc_lblNewLabel);

        JLabel lblNewLabel_1 = new JLabel(new     ImageIcon(example.class.getResource("/icons/Step3.png")));
        GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints();
        gbc_lblNewLabel_1.insets = new Insets(0, 0, 5, 0);
        gbc_lblNewLabel_1.gridx = 1;
        gbc_lblNewLabel_1.gridy = 2;
        panel.add(lblNewLabel_1, gbc_lblNewLabel_1);

        JTextArea txtrLotsOfDescriptive = new JTextArea();
        txtrLotsOfDescriptive.setFont(new Font("Monospaced", Font.PLAIN, 14));
        txtrLotsOfDescriptive.setText("lots of descriptive words....");
        GridBagConstraints gbc_txtrLotsOfDescriptive = new GridBagConstraints();
        gbc_txtrLotsOfDescriptive.fill = GridBagConstraints.BOTH;
        gbc_txtrLotsOfDescriptive.gridx = 1;
        gbc_txtrLotsOfDescriptive.gridy = 3;
        panel.add(txtrLotsOfDescriptive, gbc_txtrLotsOfDescriptive);
    }

}

here is the image, the original snip from application, as you can tell it looks alright and this is how i would want it to look and display in the app.

enter image description here

now here is the snip from the image being displayed via the code in the tutorial screen. it's not as concise and it actually looks a bit worse than what is shown because the snip enlarged it. The snip makes it a little easier to read here, but you can still see that it doesn't look like the other picture. it has lost some quality and i don't know why or what to do about it. Any help and suggestions would go a long way! thank you!

enter image description here

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Kyle
  • 89
  • 9
  • Best to create and post a valid [mre] so we can see ***exactly*** how you're displaying the image. For instance, if you are displaying the image as an ImageIcon in a JLabel and you artificially change the size of the label, say by calling `setSize(...)` or `setBounds(...)` on the label, this will change the image size and degrade its quality. – Hovercraft Full Of Eels May 01 '20 at 16:06
  • @HovercraftFullOfEels I was unaware that changing the label would affect the picture. I added code to show the layout manager. There is nothing else going on to the label except what is shown. I hope this is sufficient. – Kyle May 01 '20 at 16:25
  • I'm afraid that you've still not posted a valid [mre] as no one can compile/run/reproduce your problem. Until you've posted a valid one, then the question is a duplicate of the others. – Hovercraft Full Of Eels May 01 '20 at 16:30
  • But again, if anything in your program resizes the image in any way, then this sort of thing can easily happen. – Hovercraft Full Of Eels May 01 '20 at 16:35
  • @HovercraftFullOfEels I added a minimal example that still shows the problem, will you remove the duplicate? – Kyle May 01 '20 at 16:54
  • Thank you for the program and I have removed the duplicate close, but when I run your code, I see no such image degradation. – Hovercraft Full Of Eels May 01 '20 at 17:06
  • @HovercraftFullOfEels How can this be? i clearly see it as by my picture examples. what could be wrong on my end? – Kyle May 01 '20 at 17:22
  • 1
    @HovercraftFullOfEels, *and you artificially change the size of the label, say by calling setSize(...) or setBounds(...) on the label, this will change the image size and degrade its quality.* - a JLabel always paints the Icon at its actual size. – camickr May 01 '20 at 17:29
  • 1
    @Kyle, Maybe you are using scaling on Windows 10 for example. This would probably cause problems. Maybe this question is related: https://stackoverflow.com/questions/60838951/java-gui-scaling-problems – camickr May 01 '20 at 17:42
  • @camickr you were right. Thank you so much. my scaling was on 125% i changed it to 100% and the picture is clear as day. i read over the links, and saw that windows is not scaling properly. how can i ensure that when a user installs my app, that windows will scale it correctly? – Kyle May 01 '20 at 17:59
  • Really have no idea as I have not played with this before. However I do know that when you start a Java app you can use something like: `java -Dsun.java2d.uiScale=1.5 Example` to tell java to scale the app by 50%. Maybe if you use "1.0" it will use regular scaling no matter what Windows default is set to? – camickr May 01 '20 at 19:38

3 Answers3

0

A comment from @camickr was the answer to my problem. My scaling was on 125% in my windows settings. I changed it to 100% and my picture quality improved greatly. Looks just it does in the photo viewer.

Kyle
  • 89
  • 9
0

I have the same problem, images are pixelated when I was using java jdk-16.0.2 The real problem is in JDK, when I change my JDK to OpenJDK all the images are displaying correctly. You don't have to change windows scaling as it make everything small.

-1

Put this code where you import the image:

    Image image = null;
    try {
        image = ImageIO.read(new File("Images/image.png")).getScaledInstance(90, 90, Image.SCALE_SMOOTH);
    } catch (IOException e) {
        e.printStackTrace();
    }
    JLabel lblNewLabel_1 = new JLabel(new ImageIcon(image));
matan
  • 97
  • 13