0

I have a program that reads data from a URL makes that a BufferedImage and paints it on a JPanel. But very strange bugs suchs as the images switching around, etc. Here is my code:

package paraguay.cameras;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.net.URL;
import java.util.Timer;
import java.util.TimerTask;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;

public class Paraguay_Cameras {

    public static URL url1 = null;
    public static URL url2 = null;
    public static URL url3 = null;
    public static URL url4 = null;
    public static BufferedImage img1 = null;
    public static BufferedImage img2 = null;
    public static BufferedImage img3 = null;
    public static BufferedImage img4 = null;
    public static JPanel cam1 = null;
    public static JPanel cam2 = null;
    public static JPanel cam3 = null;
    public static JPanel cam4 = null;

    public static void main(String[] args) {

        // UI

        setLookAndFeelofNimbus();

        // DIMESIONS

        final Dimension screen_size = Toolkit.getDefaultToolkit()
                .getScreenSize();
        final int width = (int) screen_size.getWidth();
        final int height = (int) screen_size.getHeight();
        final Dimension sqr = new Dimension(width / 4, height / 4);

        // TIMERS

        Timer t = new Timer();
        TimerTask reload = new TimerTask() {
            @Override
            public void run() {
                try {

                    url1 = new URL(
                            "http://camaras.ultimahora.com/Camara/Centro.jpg?rand=12645");
                    url2 = new URL(
                            "http://camaras.ultimahora.com/Camara/tembetary.jpg?rand=6314");
                    url3 = new URL(
                            "http://camaras.ultimahora.com/Camara/recoleta.jpg?rand=15474");
                    url4 = new URL(
                            "http://camaras.ultimahora.com/Camara/elportal.jpg?rand=2599");

                } catch (Exception exc) {

                    JOptionPane
                            .showMessageDialog(
                                    null,
                                    "Oh no! Looks like we can't connect to the internet!\nPlease try again...",
                                    "", JOptionPane.ERROR_MESSAGE);
                    System.exit(0);

                }

                try {

                    img1 = ImageIO.read(url1);

                } catch (Exception exc) {

                    JOptionPane
                            .showMessageDialog(
                                    null,
                                    "Uh, oh! Looks like we  can't get the image in panel 1!\nSolution: Check your internet connection",
                                    "", JOptionPane.ERROR_MESSAGE);

                }

                try {

                    img2 = ImageIO.read(url2);

                } catch (Exception exc) {

                    JOptionPane
                            .showMessageDialog(
                                    null,
                                    "Uh, oh! Looks like we  can't get the image in panel 2!\nSolution: Check your internet connection",
                                    "", JOptionPane.ERROR_MESSAGE);

                }

                try {

                    img3 = ImageIO.read(url3);

                } catch (Exception exc) {

                    JOptionPane
                            .showMessageDialog(
                                    null,
                                    "Uh, oh! Looks like we  can't get the image in panel 3!\nSolution: Check your internet connection",
                                    "", JOptionPane.ERROR_MESSAGE);

                }

                try {

                    img4 = ImageIO.read(url4);

                } catch (Exception exc) {

                    JOptionPane
                            .showMessageDialog(
                                    null,
                                    "Uh, oh! Looks like we  can't get the image in panel 4!\nSolution: Check your internet connection",
                                    "", JOptionPane.ERROR_MESSAGE);

                }

            }
        };

        // JPANELS

        cam1 = new JPanel() {
            public void paintComponent(Graphics g) {

                Graphics2D g2 = (Graphics2D) g;

                g2.drawImage(img1, 0, 0, null);

            }
        };
        cam1.setPreferredSize(sqr);
        cam2 = new JPanel() {
            public void paintComponent(Graphics g) {

                Graphics2D g2 = (Graphics2D) g;

                g2.drawImage(img2, 0, 0, null);

            }
        };
        cam2.setPreferredSize(sqr);
        cam3 = new JPanel() {
            public void paintComponent(Graphics g) {

                Graphics2D g2 = (Graphics2D) g;

                g2.drawImage(img3, 0, 0, null);

            }
        };
        cam3.setPreferredSize(sqr);
        cam4 = new JPanel() {
            public void paintComponent(Graphics g) {

                Graphics2D g2 = (Graphics2D) g;

                g2.drawImage(img4, 0, 0, null);

            }
        };
        cam4.setPreferredSize(sqr);

        JPanel main = new JPanel(new GridLayout(2, 2));
        main.add(cam1);
        main.add(cam2);
        main.add(cam3);
        main.add(cam4);
        main.setBackground(Color.WHITE);

        // JFRAME

        JFrame window = new JFrame("Paraguay Cameras");
        window.setSize(width, height);
        window.add(main);
        window.setVisible(true);
        window.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        window.setLocationRelativeTo(null);
        window.setResizable(true);
        window.setAlwaysOnTop(true);
        window.setEnabled(true);

        window.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent we) {

                System.exit(0);

            }
        });

        t.schedule(reload, 0, 4000);
        cam1.repaint();
        cam2.repaint();
        cam3.repaint();
        cam4.repaint();

    }

    public static void setLookAndFeelofNimbus() {

        try {

            UIManager
                    .setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");

        } catch (Exception exc) {

            JOptionPane
                        .showMessageDialog(
                                null,
                                "Oops! Looks like we can't use the UI (User Interface) of Nimbus...",
                            "", JOptionPane.ERROR_MESSAGE);

            }

            }
    }

Can anyone help me fix my bugs? Thanks!

tenorsax
  • 21,123
  • 9
  • 60
  • 107
  • Your code is too long and very hard to read. Can you narrow it down to the area in which you think the problem might be and then delete all the extra stuff ? – Radu Murzea Mar 23 '13 at 18:21
  • Welcome to Stackoverflow! When choosing a title for your question, there's no need to include tags in them. Please read http://meta.stackexchange.com/questions/19190 for the discussion and why they're not needed. – Patrick Mar 23 '13 at 18:47

1 Answers1

1

You are never repainting the panels after the images are loaded. Frankly, in current code it is hard to achieve that. Refactor the code so you can call main.repaint(); to refresh the panels after the images are re-loaded. Also, don't forget to call super.paintComponents(g); inside your paintComponent() implementation.

Another point, for Swing it is better to use javax.swing.Timer. See How to Use Swing Timers for more details.

Also note that calling setPreferredSize() in this case will have no effect as you're using GridLayout. In general, setPreferredSize() should mostly be avoided, see Should I avoid the use of set[Preferred|Maximum|Minimum]Size methods in Java Swing?

Community
  • 1
  • 1
tenorsax
  • 21,123
  • 9
  • 60
  • 107
  • If you're not touching UI from the timer or performing lengthy operations then you can use general-purpose timer. In your code you're mostly loading images in the timer, however, `JOptionPane` is also there. – tenorsax Mar 23 '13 at 19:02