簡體   English   中英

如何在JFrame中刷新圖像?

[英]How refresh image in JFrame?

有一些服務器,我需要從中獲取圖像。 並且此圖像有時會更新。 程序需要獲取此圖像並將其始終以全屏顯示在屏幕上。 我寫了一些代碼,如果運行一次就可以正常工作。 但是我無法處理圖像更新。 我需要每隔XX分鍾或每秒鍾從服務器獲取一次圖像,並將其顯示在屏幕上。 可能我需要一些刷新圖像功能,例如-repaint(),但我不知道如何在此代碼中正確使用它。 我嘗試了cycle-while和Thread.sleep(),但是由於創建了許多多余的對象而無法正常工作...請幫助我。

public class MyParser {
public static void main(String[] args) throws IOException, InterruptedException {
            String urlStr = "http://192.168.11.111/images/SGBWebServerImage.bmp";
            JFrame frame = new JFrame();
            URL url = new URL(urlStr);
            BufferedImage image = resize(ImageIO.read(url), 320, 1920);
            ImageIcon icon = new ImageIcon(image);
            frame.add(new JLabel(icon));
            frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().setBackground(Color.BLACK);
            frame.pack();
            frame.setVisible(true);
    }

private static BufferedImage resize(BufferedImage img, int height, int width) {
    Image tmp = img.getScaledInstance(width, height, Image.SCALE_SMOOTH);
    BufferedImage resized = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
    Graphics2D g2d = resized.createGraphics();
    g2d.drawImage(tmp, 0, 0, null);
    g2d.dispose();
    return resized;
}

我需要每隔XX分鍾或每秒鍾從服務器獲取一次圖像,並將其顯示在屏幕上。

使用Swing Timer安排一些活動。 閱讀Swing教程中有關如何使用Swing計時器的部分, 獲取更多信息。

計時器啟動時,您需要:

  1. 從服務器獲取圖像
  2. 更新JLabel的圖標

這意味着您將需要重組代碼,以便對標簽進行引用。 因此,您需要擺脫所有靜態方法。

您可以簽出:例如, 即使在單獨的線程中實現了睡眠之后,GUI執行也不會延遲 您只需要替換actionPerformed(...)方法中的邏輯即可獲取圖像並更新標簽的圖標。

檢查是否有幫助。

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class MyImage extends JPanel implements ActionListener {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    JLabel imageLabel;

    public MyImage() {

        ImageIcon icon = new ImageIcon("https://picsum.photos/200/300/?random");
        setLayout(new BorderLayout());
        imageLabel = new JLabel(icon);
        add(imageLabel, BorderLayout.CENTER);
        javax.swing.Timer timer = new javax.swing.Timer(1000, this);
        timer.start();
    }

    public void actionPerformed(ActionEvent e) {

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                try {
                    String imageName = "https://picsum.photos/200/300/?random";
                    URL url = new URL(imageName);
                    ImageIcon icon = new ImageIcon(url);
                    icon.getImage().flush();
                    imageLabel.setIcon(icon);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private static void createAndShowUI() {
        JFrame frame = new JFrame("testimage reload");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new MyImage());
        frame.setLocationByPlatform(true);
        frame.setSize(500, 500);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                createAndShowUI();
            }
        });
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM