0

There is some server and I need to get image from it. And this image updates sometimes. And program need to get this image and show it on a screen always in fullscreen. I wrote some code and it works fine if run it once. But I can't handle with updating image. I need to get image every XX minutes or seconds from server and show it on the screen. May be I need some refresh image function like - repaint(), but I don't know how use it right in this code. I tried cycle - while and Thread.sleep() but it didn't work correctly because of creating many excessed objects... Help me please.

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;
}
bulgakov
  • 61
  • 3

2 Answers2

0

I need to get image every XX minutes or seconds from server and show it on the screen.

Use a Swing Timer to schedule some activity. Read the section from the Swing tutorial on How to Use Swing Timers for more information.

When the timer fires you will need to:

  1. get the image from the server
  2. update the Icon of the JLabel

Which means you will need to restructure your code so you have a reference to the label. So you need to get rid of all the static methods.

You can check out: No delay in GUI execution even after implementing sleep in a separate thread for an example. You will just need to replace the logic in the actionPerformed(...) method to get your image and update the Icon of the label.

camickr
  • 321,443
  • 19
  • 166
  • 288
0

Check if this helps.

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