问题描述
我目前正在学习 java swing builder 作为一项新技术技能.我需要研究如何在画布上导入图像.是的,它成功导入,图像清晰且不模糊.但是,当我运行项目时,我导入到项目中的图像变得模糊.我不知道为什么会这样.我研究了一些功能,例如缩放,但没有任何反应它会输出相同的输出变得模糊.
I currently studying the java swing builder as a new tech skills. I need to study how to import the image on my canvas. Yes it successfully imported and the image clearly define and not blurry.however when I run the project the image that I import to the project it goes blurry. I don't know why it happens. I research some functionality like get scaled but nothing happens it goes same output it get blurry.
图片尺寸:250x250
Image Dimension: 250x250
这是我在我的项目中实现的代码:
Here is my code that I implement on my project:
private Image doctor_ppe = new ImageIcon(LoginScreen.class.getResource("/assets/large.png")).getImage().getScaledInstance(250, 250, Image.SCALE_SMOOTH);
JLabel lblDds = new JLabel("");
lblDds.setVerticalAlignment(SwingConstants.BOTTOM);
lblDds.setHorizontalAlignment(SwingConstants.CENTER);
lblDds.setIcon(new ImageIcon(doctor_ppe));
lblDds.setBounds(59, 305, 236, 251);
panel.add(lblDds);
不可运行项目和可运行项目的区别:
Difference of not runnable project and runnable project:
图像不模糊:
运行项目后图像模糊:
希望有人经历过这个,希望对我的问题有所帮助谢谢.
Hope someone experience this, hope will help on my problemthank you.
推荐答案
可能是因为您的桌面使用了大于 1.0 的缩放因子,所以在绘制图像时会放大.
Probably because your desktop is using a scaling factor greater than 1.0, so the image is upscaled when it is painted.
如果您想更改整个应用程序的缩放比例,您可以尝试:
If you want to change the scaling for the entire application you can try:
- 使用命令行
-Dsun.java2d.uiScale=1.0
,或 - 使用
System.setProperty("sun.java2d.uiScale", "1.0") 以编程方式设置
另一种选择可能是仅防止 Icon
缩放:
Another option might be to prevent only the Icon
from scaling:
import java.awt.*;
import java.awt.geom.AffineTransform;
import javax.swing.*;
public class NoScalingIcon implements Icon
{
private Icon icon;
public NoScalingIcon(Icon icon)
{
this.icon = icon;
}
public int getIconWidth()
{
return icon.getIconWidth();
}
public int getIconHeight()
{
return icon.getIconHeight();
}
public void paintIcon(Component c, Graphics g, int x, int y)
{
Graphics2D g2d = (Graphics2D)g.create();
AffineTransform at = g2d.getTransform();
int scaleX = (int)(x * at.getScaleX());
int scaleY = (int)(y * at.getScaleY());
int offsetX = (int)(icon.getIconWidth() * (at.getScaleX() - 1) / 2);
int offsetY = (int)(icon.getIconHeight() * (at.getScaleY() - 1) / 2);
int locationX = scaleX + offsetX;
int locationY = scaleY + offsetY;
// Reset scaling to 1.0 by concatenating an inverse scale transfom
AffineTransform scaled = AffineTransform.getScaleInstance(1.0 / at.getScaleX(), 1.0 / at.getScaleY());
at.concatenate( scaled );
g2d.setTransform( at );
icon.paintIcon(c, g2d, locationX, locationY);
g2d.dispose();
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI()
{
JButton button = new JButton( "Button" );
NoScalingIcon icon = new NoScalingIcon( new ImageIcon("box.jpg") );
button.setIcon( icon );
JPanel panel = new JPanel( );
panel.add( button );
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(panel);
f.setSize(200, 200);
f.setLocationRelativeTo( null );
f.setVisible(true);
}
}
这篇关于运行java swing项目时如何修复模糊图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!