栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何正确刷新JFrame中的图像?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何正确刷新JFrame中的图像?

就个人而言,我可以在将其应用于标签之前调整其大小,或者使用aJPanel进行绘画。JLabel必须拖延很多功能。

举例来说,您遇到的问题是您实际上是在使用 该图像setIcon来设置图像,但是却 在其顶部paintComponent绘制了另一个(初始)图像

您的自定义标签将aImageIcon作为初始参数并将其绘制为初始参数

private static class MyJLabel extends JLabel {    private ImageIcon img = null;    public MyJLabel(ImageIcon img) {        super();        this.img = img;    }    @Override    public void paintComponent(Graphics g) {        super.paintComponent(g);        g.drawImage(img.getImage(), 0, 0, getWidth(), getHeight(), this);    }}

You initialise it as such…

label = new MyJLabel(new ImageIcon(img));

It should be noted that if you used the

Icon
support of
JLabel
, this…

label.setPreferredSize(dims);

会无关紧要,因为JLabel会使用图标的大小来确定其
首选的大小……但无论如何……

然后,使用此按钮更新图标。

img = image;if (label != null) {    label.setIcon(new ImageIcon(img));    label.repaint();}

应该指出的是,根据您的示例,这实际上是在EDT之外调用的,这很危险,并可能导致油漆变脏。

但setIcon永远不会改变的价值img范围内MyLabel,所以当你的

paintComponent
方法被调用时,你实际上是画了你的图标
已经在更新提供…

// Paint the new Iconsuper.paintComponent(g);// Paint the old/initial image...g.drawImage(img.getImage(), 0, 0, getWidth(), getHeight(), this);

Updated

就个人而言,我要做的是使用a之类的东西创建一个自定义组件,JPanel并根据
面板的当前大小缩放原始图像,例如……

现在,通常情况下,在执行图像缩放时,我更喜欢使用Java中演示的分治方法:维护JPanel
背景图像的宽高比,但是在此示例中,我只是AffineTransform为了简单起见使用

import java.awt.Dimension;import java.awt.EventQueue;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.HeadlessException;import java.awt.Image;import java.awt.geom.AffineTransform;import java.io.File;import java.io.IOException;import javax.imageio.ImageIO;import javax.swing.Jframe;import javax.swing.JPanel;import javax.swing.UIManager;import javax.swing.UnsupportedLookAndFeelException;public class ScalableImageExample {    public static void main(String[] args) {        new ScalableImageExample();    }    public ScalableImageExample() {        EventQueue.invokeLater(new Runnable() { @Override public void run() {     try {         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());     } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {         ex.printStackTrace();     }     try {         ResizableImagePane pane = new ResizableImagePane();         pane.setImage(...);         Jframe frame = new Jframe("Testing");         frame.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);         frame.add(pane);         frame.pack();         frame.setLocationRelativeTo(null);         frame.setVisible(true);     } catch (IOException exp) {         exp.printStackTrace();     } }        });    }    public class ResizableImagePane extends JPanel {        private Image img;        public ResizableImagePane() {        }        public void setImage(Image value) { if (img != value) {     Image old = img;     this.img = value;     firePropertyChange("image", old, img);     revalidate();     repaint(); }        }        public Image getImage() { return img;        }        @Override        public Dimension getPreferredSize() { return img == null ? new Dimension(200, 200) : new Dimension(img.getWidth(this), img.getHeight(this));        }        @Override        protected void paintComponent(Graphics g) { super.paintComponent(g); if (img != null) {     Graphics2D g2d = (Graphics2D) g.create();     int width = getWidth();     int height = getHeight();     double scaleFactor = getScaleFactorToFit(new Dimension(img.getWidth(this), img.getHeight(this)), getSize());     int x = (int)((width - (img.getWidth(this) * scaleFactor)) / 2);     int y = (int)((height - (img.getHeight(this) * scaleFactor)) / 2);     AffineTransform at = new AffineTransform();     at.translate(x, y);     at.scale(scaleFactor, scaleFactor);     g2d.setTransform(at);     g2d.drawImage(img, 0, 0, this);     g2d.dispose(); }        }        public double getScaleFactor(int iMasterSize, int iTargetSize) { return (double) iTargetSize / (double) iMasterSize;        }        public double getScaleFactorToFit(Dimension original, Dimension toFit) { double dScale = 1d; if (original != null && toFit != null) {     double dScaleWidth = getScaleFactor(original.width, toFit.width);     double dScaleHeight = getScaleFactor(original.height, toFit.height);     dScale = Math.min(dScaleHeight, dScaleWidth); } return dScale;        }    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/428888.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号