【发布时间】:2015-11-25 05:00:45
【问题描述】:
我正在使用此代码进行幻灯片放映,它可以 100% 工作,但问题是图像分辨率,如果分辨率高没问题,但如果分辨率低,则图像显示的形状非常糟糕。
代码是:
public class EasyView_V1 {
public static Timer tm = null;
public static File[] f = new File("C:/Ma7moud/my projects/Rami/NetBeans/Web applications/TheChatServerApp_3/web/images/brugges2006/big").listFiles();
public static void main(String[] args) {
JPanel panel5 = new JPanel();
JLabel pic = new JLabel();
pic.setSize(new Dimension(610, 695));
//Call The Function SetImageSize
try {
SetImageSize(f.length - 1, f, pic);
} catch (Exception e) {
System.out.println(e.toString());
JOptionPane.showMessageDialog(null, "Images directory not found", "Error", JOptionPane.ERROR_MESSAGE);
JOptionPane.showMessageDialog(null, "Plz check the images directory and try again", "Attention", JOptionPane.INFORMATION_MESSAGE);
System.exit(1);
}
try {
//set a timer
tm = new Timer(1000, (ActionEvent e) -> {
SetImageSize(x, f, pic);
x += 1;
try {
if (x >= f.length) {
x = 0;
}
} catch (Exception ex) {
System.out.println(ex.toString());
System.exit(1);
}
});
} catch (Exception e) {
System.out.println(e.toString());
}
panel5.add(pic);
tm.start();
JFrame f = new JFrame ()
f.add(panel5);
f.pack();
f.setVisible(true);
}
图片绘制代码如下:
public static void SetImageSize(int i, File[] f, JLabel pic) {
try {
ImageIcon icon = new ImageIcon(f[i].toString());
Image img = icon.getImage();
@SuppressWarnings("UnusedAssignment")
Image newImg = null;
newImg = img.getScaledInstance(pic.getWidth(), pic.getHeight(), Image.SCALE_SMOOTH);
ImageIcon newImc = new ImageIcon(newImg);
pic.setIcon(newImc);
} catch (Exception ex) {
System.out.println(ex.toString());
}
}
}
【问题讨论】:
-
1) 为了尽快获得更好的帮助,请发帖 minimal reproducible example 或 Short, Self Contained, Correct Example。 2) 例如,获取图像的一种方法是热链接到在this Q&A 中看到的图像。 3) 当图像放大时,图像质量总是会受到影响。
-
不要使用
new ImageIcon加载图像,而是使用ImageIO.read。将返回的 BufferedImage 的宽度和高度与您的代码将图像缩放到的尺寸进行比较,如果会放大图像,则跳过缩放。 -
好的,谢谢你 Andrew 和 VGR 的帮助,我会考虑你的建议,我会尽快给你解决方案
标签: java image swing user-interface slideshow