Pudgy's World
posts - 13,  comments - 16,  trackbacks - 0
Recently, I am working on graphic project. This project I faced is a big challenge to me. Here is a sloution to show the progress bar when loading the image from the net.
PS:
If you install the JAI, it will break the progress interrupt.
See this in Sun's Bug Database
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4842452


  1 import java.awt.BorderLayout;
  2 import java.awt.event.ActionEvent;
  3 import java.awt.event.ActionListener;
  4 import java.awt.image.BufferedImage;
  5 import java.io.IOException;
  6 import java.net.MalformedURLException;
  7 import java.net.URL;
  8 import java.util.Iterator;
  9 
 10 import javax.imageio.ImageIO;
 11 import javax.imageio.ImageReader;
 12 import javax.imageio.event.IIOReadProgressListener;
 13 import javax.imageio.stream.ImageInputStream;
 14 import javax.swing.*;
 15 
 16 public class IIOProgressTest
 17 {
 18     public static void main(String[] args)
 19     {
 20         try {
 21             UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
 22         } catch (Exception e) {
 23             e.printStackTrace();
 24         }
 25 
 26         final JLabel imageLabel = new JLabel();
 27         JScrollPane scrollPane = new JScrollPane(imageLabel);
 28         final JButton startBtn = new JButton("Load Image");
 29         final JProgressBar progressBar = new JProgressBar();
 30         progressBar.setVisible(false);
 31         JPanel p = new JPanel(new BorderLayout(55));
 32         p.add(startBtn, BorderLayout.WEST);
 33         p.add(progressBar, BorderLayout.CENTER);
 34         
 35         final IIOReadProgressListener progressListener = new IIOReadProgressListener() {
 36             public void imageStarted(ImageReader source, int imageIndex)
 37             {
 38                 SwingUtilities.invokeLater(new Runnable()
 39                 {
 40                     public void run()
 41                     {
 42                         startBtn.setEnabled(false);
 43                         progressBar.setValue(0);
 44                         progressBar.setVisible(true);
 45                         invalidate();
 46 
 47                     }
 48                 });
 49             }
 50             public void imageProgress(ImageReader source, final float percentageDone)
 51             {
 52                 SwingUtilities.invokeLater(new Runnable()
 53                 {
 54                     public void run()
 55                     {
 56                         System.out.println("percentageDone" + percentageDone);
 57                         progressBar.setValue((int) (percentageDone));
 58                     }
 59                 });
 60             }
 61             public void imageComplete(ImageReader source) 
 62             {
 63                 SwingUtilities.invokeLater(new Runnable()
 64                 {
 65                     public void run()
 66                     {
 67                         startBtn.setEnabled(true);
 68                         progressBar.setValue(100);
 69                         progressBar.setVisible(false);
 70                     }
 71                 });
 72             }
 73 
 74             public void sequenceStarted(ImageReader source, int minIndex) {}
 75             public void sequenceComplete(ImageReader source) {}
 76             public void thumbnailStarted(ImageReader source, int imageIndex, int thumbnailIndex) {}
 77             public void thumbnailProgress(ImageReader source, float percentageDone) {}
 78             public void thumbnailComplete(ImageReader source) {}
 79             public void readAborted(ImageReader source) {}
 80         };
 81 
 82         startBtn.addActionListener(new ActionListener()
 83         {
 84             public void actionPerformed(ActionEvent e)
 85             {
 86                 final String urlStr = JOptionPane.showInputDialog(
 87                         SwingUtilities.getWindowAncestor(startBtn), "Input image URL");
 88                 if (urlStr == null) {
 89                     return;
 90                 }
 91 
 92                 new Thread() {
 93                     public void run()
 94                     {
 95                         try {
 96                             URL url = new URL(urlStr);
 97                             ImageInputStream iis = ImageIO.createImageInputStream(url.openStream());
 98                             Iterator iter = ImageIO.getImageReaders(iis);
 99                             if (iter.hasNext()) {
100                                 ImageReader reader = (ImageReader) iter.next();
101                                 reader.setInput(iis);
102                                 reader.addIIOReadProgressListener(progressListener);
103                                 BufferedImage image = reader.read(reader.getMinIndex());
104                                 final ImageIcon icon = new ImageIcon(image);
105 
106                                 SwingUtilities.invokeLater(new Runnable()
107                                 {
108                                     public void run()
109                                     {
110                                         imageLabel.setIcon(icon);
111                                         imageLabel.revalidate();
112                                     }
113                                 });
114                             }
115                         } catch (MalformedURLException e) {
116                             e.printStackTrace();
117                         } catch (IOException e) {
118                             e.printStackTrace();
119                         }
120                     }
121                 }.start();
122             }
123         });
124 
125         JFrame f = new JFrame("IIOProgressTest");
126         f.getContentPane().setLayout(new BorderLayout(55));
127         f.getContentPane().add(scrollPane, BorderLayout.CENTER);
128         f.getContentPane().add(p, BorderLayout.SOUTH);
129         f.setSize(800600);
130         f.setLocationRelativeTo(null);
131         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
132         f.setVisible(true);
133     }
134 }
135 

posted on 2005-08-16 12:19 Pudgy's World 阅读(899) 评论(0)  编辑  收藏 所属分类: ImageIO

只有注册用户登录后才能发表评论。


网站导航:
 

<2005年8月>
31123456
78910111213
14151617181920
21222324252627
28293031123
45678910

常用链接

留言簿(1)

随笔分类(13)

随笔档案(13)

文章分类(4)

文章档案(5)

相册

Developer

Favorite blogs

搜索

  •  

积分与排名

  • 积分 - 22115
  • 排名 - 1633

最新评论

阅读排行榜

评论排行榜