有一些服务器,我需要从中获取图像。这张图片有时会更新。程序需要获取此图像并始终全屏显示在屏幕上。我写了一些代码,如果运行一次它就可以正常工作。但我无法处理更新图像。我需要每隔 XX 分钟或秒从服务器获取图像并将其显示在屏幕上。可能是我需要一些刷新图像功能,如 - repaint(),但我不知道如何在这段代码中正确使用它。我试过循环 - 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;
}
慕盖茨4494581
相关分类