Controlling the image quality on the Java application side

给你一囗甜甜゛ 提交于 2021-02-11 07:01:52

问题


I posted a question here asking about what to do about a blurry picture in a java application. What i discovered is that my windows scaling was causing the blurriness. Now i have a greater issue. I want the quality of the image and application to be controlled via the application, without having to change any windows settings if at all possible.

Instead of changing the scaling back to 100% there was another windows suggestion from here that said to change the compatibility settings of the java.exe and or javaw.exe to override the high DPI settings to have scaling performed by the system. It works, as in the picture is clearer, but the text loses a crispness to it.

The best quality i have found for the application is to have it not override the high DPI but simply have the scaling set to 100%. The text is crisp and clear and the picture is as well.

I am trying to figure out if there is a way to keep that quality without having to have the users change any of their windows settings to have the application look good. I'm wondering if there is a way in the code to achieve this.

here is an example program with my scaling at 125% The picture is blurry but the text good.

Here is an example of the DPI overridden by the system with scaling still at 125% notice that the text loses a crispness to it but the picture is better.

finally the scaling is 100% and the DPI is not being overridden anymore. this is the best text and picture quality

here is the example code, if anyone needs it.

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("Cambria Math", 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("Cambria Math", 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.setLineWrap(true);
    txtrLotsOfDescriptive.setWrapStyleWord(true);
    txtrLotsOfDescriptive.setEditable(false);
    txtrLotsOfDescriptive.setFont(new Font("Cambria Math", Font.PLAIN, 14));
    txtrLotsOfDescriptive.setText("Lorem ipsum dolor sit amet, eu tempor maluisset eum. Ipsum detracto mediocrem quo eu. Eos ad ocurreret argumentum. Cum ei vero ipsum alienum, has sonet impetus repudiandae at, cu viris assentior eum.");
    GridBagConstraints gbc_txtrLotsOfDescriptive = new GridBagConstraints();
    gbc_txtrLotsOfDescriptive.fill = GridBagConstraints.BOTH;
    gbc_txtrLotsOfDescriptive.gridx = 1;
    gbc_txtrLotsOfDescriptive.gridy = 3;
    panel.add(txtrLotsOfDescriptive, gbc_txtrLotsOfDescriptive);
}

}

来源:https://stackoverflow.com/questions/61620903/controlling-the-image-quality-on-the-java-application-side

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!