2013-03-23 65 views
0

我有一个程序从URL读取数据,使得这个BufferedImage并在JPanel上绘制它。但suchs的图像非常奇怪的错误开关周围等。这里是我的代码:刷新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); 

      } 

      } 
    } 

谁能帮我解决我的错误?谢谢!

+0

您的代码太长且很难阅读。你能否缩小到你认为问题可能出现的地方,然后删除所有额外的东西? – 2013-03-23 18:21:32

+0

欢迎来到Stackoverflow!为您的问题选择标题时,不需要在其中添加标签。请阅读http://meta.stackexchange.com/questions/19190进行讨论,以及为什么他们不需要。 – Patrick 2013-03-23 18:47:49

回答

1

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

另一点,对于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?

+0

使用util timer还可以吗? – user1978786 2013-03-23 18:56:25

+0

如果您没有从计时器中触摸UI或进行冗长的操作,那么您可以使用通用计时器。在你的代码中,你大部分都是在定时器中加载图像,但是'JOptionPane'也在那里。 – tenorsax 2013-03-23 19:02:41

+1

谢谢!谢谢!谢谢!!!!!!!!!!!!!!!!!!!!!臭虫被压扁了! – user1978786 2013-03-23 19:04:57

相关问题