如何在Java中使图片不模糊
我在应用程序中截取了一张表,我想在应用程序的教程部分显示该截取。我无法让它在显示时不模糊。我不知道是什么原因造成的。当我在Windows照片查看器中打开图片时,它很好,但在应用程序中它很模糊 我的所有图片都存储在一个名为“我的项目中的图标”的包中,它们不是图片的链接,而是实际的副本 下面是我的代码的一个最简单的工作示例。如果任何调整图像大小,使其失去质量的事情,我该如何防止如何在Java中使图片不模糊,java,image,swing,imageicon,Java,Image,Swing,Imageicon,我在应用程序中截取了一张表,我想在应用程序的教程部分显示该截取。我无法让它在显示时不模糊。我不知道是什么原因造成的。当我在Windows照片查看器中打开图片时,它很好,但在应用程序中它很模糊 我的所有图片都存储在一个名为“我的项目中的图标”的包中,它们不是图片的链接,而是实际的副本 下面是我的代码的一个最简单的工作示例。如果任何调整图像大小,使其失去质量的事情,我该如何防止 import java.awt.EventQueue; import javax.swing.JFrame; impor
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);
}
}
这是图像,应用程序中的原始剪报,您可以看出它看起来不错,这就是我希望它在应用程序中的外观和显示方式
现在,这里是通过教程屏幕中的代码显示的图像的剪报。它不是那么简洁,实际上它看起来有点比所显示的更糟糕,因为剪子放大了它。剪报使这里更容易阅读,但你仍然可以看到它看起来不像其他图片。它已经失去了一些质量,我不知道为什么或者该怎么办。任何帮助和建议都会大有帮助!谢谢大家!
将此代码放在导入图像的位置:
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));
将此代码放在导入图像的位置:
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));
@camickr的评论就是我问题的答案。在windows设置中,我的缩放比例为125%。我把它改为100%,我的图片质量大大提高了。看起来就像它在照片查看器中一样 来自@camickr的评论就是我问题的答案。在windows设置中,我的缩放比例为125%。我把它改为100%,我的图片质量大大提高了。看起来就像它在照片查看器中一样 最好创建并发布一个有效的文件,这样我们就可以准确地看到您是如何显示图像的。例如,如果您将图像显示为JLabel中的图像图标,并通过调用标签上的
setSize(…)
或setBounds(…)
来人为更改标签的大小,这将更改图像大小并降低其质量。@HovercraftFullOfEels我不知道更改标签会影响图片。我添加了代码来显示布局管理器。除了显示的内容外,标签上没有其他内容。我希望这就足够了。恐怕你还没有发布有效的日志,因为没有人可以编译/运行/重现你的问题。除非您发布了一个有效的问题,否则该问题是其他问题的重复。@HoverCraftFullOfels,并且您人为地更改标签的大小,例如通过在标签上调用setSize(…)或setBounds(…),这将更改图像大小并降低其质量。-JLabel总是按图标的实际大小绘制图标。@Kyle,可能您正在使用Windows 10上的缩放。这可能会引起问题。也许这个问题是相关的:最好创建并发布一个有效的文件,这样我们就可以准确地看到您是如何显示图像的。例如,如果您将图像显示为JLabel中的图像图标,并通过调用标签上的setSize(…)
或setBounds(…)
来人为更改标签的大小,这将更改图像大小并降低其质量。@HovercraftFullOfEels我不知道更改标签会影响图片。我添加了代码来显示布局管理器。除了显示的内容外,标签上没有其他内容。我希望这就足够了。恐怕你还没有发布有效的日志,因为没有人可以编译/运行/重现你的问题。除非您发布了一个有效的问题,否则该问题是其他问题的重复。@HoverCraftFullOfels,并且您人为地更改标签的大小,例如通过在标签上调用setSize(…)或setBounds(…),这将更改图像大小并降低其质量。-JLabel总是按图标的实际大小绘制图标。@Kyle,可能您正在使用Windows 10上的缩放。这可能会引起问题。也许这个问题是相关的: