A
A
Alexander Bulgakov2018-08-29 14:18:58
Java
Alexander Bulgakov, 2018-08-29 14:18:58

How to properly update image in JFrame?

Hello. Question such:
There is a certain server. An image is generated on it, this image must be enlarged and shown on the monitor. The picture is periodically updated and it is necessary to redraw it all. Here I have problems with redrawing, stupidly stuffing the problem into a cycle did not solve it, just new objects are generated. If you run it once, everything works. But it needs to be updated periodically.

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

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question