0

我有一个从 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);

            }

            }
    }

谁能帮我修复我的错误?谢谢!

4

1 回答 1

1

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

还有一点,对于 Swing,最好使用javax.swing.Timer。有关更多详细信息,请参阅如何使用摆动计时器

另请注意,setPreferredSize()在这种情况下调用将没有任何效果,因为您正在使用GridLayout. 一般来说,setPreferredSize()应该尽量避免,请参阅是否应该避免在 Java Swing 中使用 set[Preferred|Maximum|Minimum]Size 方法?

于 2013-03-23T18:29:40.883 回答