刷新JPanel时的错误

时间:2013-03-23 18:19:21

标签: java swing url jpanel bufferedimage

我有一个从URL读取数据的程序使其成为BufferedImage并在JPanel上绘制它。但非常奇怪的错误,如图像切换等。这是我的代码:

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

            }

            }
    }

任何人都可以帮我修复我的错误吗?谢谢!

1 个答案:

答案 0 :(得分:1)

加载图像后,您永远不会重新绘制面板。坦率地说,在目前的代码中很难实现这一点。重构代码,以便在重新加载图像后调用main.repaint();来刷新面板。另外,请不要忘记在super.paintComponents(g);实施中致电paintComponent()

另一点,对于Swing,最好使用javax.swing.Timer。有关详细信息,请参阅How to Use Swing Timers

另请注意,在这种情况下调用setPreferredSize()将无效,因为您正在使用GridLayout。一般来说,应该主要避免使用setPreferredSize(),请参阅Should I avoid the use of set[Preferred|Maximum|Minimum]Size methods in Java Swing?