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

时间:2014-01-12 02:47:57

标签: java multithreading swing jframe jlabel

这个问题让我感到困扰了几个小时,我自己也无法找到解决方案......

我在网上找到了类似的主题,但我找不到完全相同的问题解释并尽可能简单的解决方案。我还查看了 EDT SwingWorker API文档,但这对我来说太复杂了:(

所以,让我们谈谈这一点。我有一个简单的JFrame里面有JLabel,它由我的图像组成:

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);
    }
}

private static class MyJFrame extends JFrame implements Runnable {
    private BufferedImage img = null;
    private MyJLabel label = null;

    public MyJFrame(BufferedImage image, String title) {
        super(title);
        img = image;
    }

    @Override
    public void run() {
        Dimension dims = new Dimension(img.getWidth(), img.getHeight());
        dims = new Dimension(dims.width / 2, dims.height / 2);

        label = new MyJLabel(new ImageIcon(img));
        label.setPreferredSize(dims);

        addComponentListener(new ComponentAdapter() {
            @Override
            public void componentResized(ComponentEvent e) {
                label.repaint();
            }
        });
        setLayout(new BorderLayout());
        getContentPane().add(BorderLayout.CENTER, label);
        setLocation(200, 200);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        pack();
        setVisible(true);
    }

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

这段代码引用了它:

buffer = receiveImage(in); // download image

MyJFrame f = null;
javax.swing.SwingUtilities.invokeLater(f = new MyJFrame(buffer, "RDP"));

int x = 0;
while (x <= 15) {
    txt.println("next"); // notify server that we are ready

    while (true) { // wait for server
        if (reader.readLine().equals("ready")) break;
    }

    buffer = receiveImage(in); // download image

    // do some magic here and refresh image somehow :(
    f.changeImage(buffer); // does not work!

    x++;
}

不幸的是,我使用 changeImage 方法的方法不起作用 - 没有任何反应(GUI启动但永远不会更新)。

我很感激这一点。 简单,最合适的解释工作示例;)

问候!

2 个答案:

答案 0 :(得分:2)

就个人而言,我要先将其调整大小,然后再将其应用于标签,或者使用JPanel来执行绘画。 JLabel必须有很多功能。

例如,您遇到的问题是您实际上是使用setIcon设置图片,而是使用paintComponent在其顶部绘制另一个(初始)图片< / p>

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

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);
    }
}

你初始化它......

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

应该注意的是,如果您使用了Icon的{​​{1}}支持,那么......

JLabel

无关紧要,因为label.setPreferredSize(dims); 会使用图标大小来确定它的首选尺寸......但无论如何......

然后使用此更新图标..

JLabel

应该指出的是,基于你的例子,这实际上是在EDT之外调用的,这很危险并且可能导致油漆

但是img = image; if (label != null) { label.setIcon(new ImageIcon(img)); label.repaint(); } 永远不会更改setIconimg的值,因此当您调用MyLabel方法时,实际上是在绘制您提供的图标更新...

paintComponent

<强>更新

就我个人而言,我要做的是创建一个自定义组件,使用像// Paint the new Icon super.paintComponent(g); // Paint the old/initial image... g.drawImage(img.getImage(), 0, 0, getWidth(), getHeight(), this); 这样的东西,并根据面板的当前大小缩放原始图像,例如......

现在,通常情况下,在执行图像缩放时,我更喜欢使用Java: maintaining aspect ratio of JPanel background image中所示的除法和conqure方法,但对于此示例,为简单起见,我只使用了JPanel

Small Large

AffineTransform

答案 1 :(得分:0)

这就是我提出的:

private static class MyJPanel extends JPanel {
    private Image img = null;

    public MyJPanel() {}

    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;
    }
}

private static class MyJFrame extends JFrame implements Runnable {
    private BufferedImage img = null;
    private MyJPanel panel = null;

    public MyJFrame(BufferedImage image, String title) {
        super(title);
        img = image;
    }

    @Override
    public void run() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {}

        panel = new MyJPanel();
        panel.setImage(img);

        setLayout(new BorderLayout());
        add(BorderLayout.CENTER, panel);
        setLocation(200, 200);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        pack();
        setVisible(true);
    }

    public void changeImage(BufferedImage image) {
        if ((panel != null) && (panel.getImage() != image)) panel.setImage(image);
    }
}

这是@MadProgrammer提供的示例中非常直接的复制粘贴。

唯一剩下的就是EDT用法,对我来说更像 magic 。 我仍在使用脏方式

调用此代码
MyJFrame mjf = null;
javax.swing.SwingUtilities.invokeLater(mjf = new MyJFrame(buffer, "RDP"));
...
mjf.changeImage(buffer);

我的问题是:如何将{strong> changeImage 方法与EDT一起使用?