/Users/lyon/j4p/src/video/BroadcastViewerThread.java

1    package video; 
2     
3    import futils.Futil; 
4    import gui.ClosableJFrame; 
5    import net.multicast.McastUtil; 
6     
7    import javax.imageio.ImageIO; 
8    import java.awt.Graphics; 
9    import java.awt.Graphics2D; 
10   import java.awt.image.BufferedImage; 
11   import java.io.ByteArrayInputStream; 
12   import java.io.File; 
13   import java.io.FileInputStream; 
14   import java.io.IOException; 
15   import java.io.InputStream; 
16   import java.net.InetAddress; 
17    
18   /** 
19    * Copyright DocJava, inc. 
20    * User: lyon 
21    * Date: Oct 27, 2004 
22    * Time: 5:08:08 AM 
23    */ 
24   public class BroadcastViewerThread extends Thread { 
25       McastUtil mcu; 
26    
27       public BroadcastViewerThread(McastUtil mcu) { 
28           this.mcu = mcu; 
29           setDaemon(false); 
30           start();                // start calls run 
31       } 
32    
33       public void run() { 
34           String tmp; 
35           ImageFrame imgFrame = new ImageFrame(); 
36           imgFrame.setVisible(true); 
37           try { 
38               while (true) { 
39                   byte[] data = mcu.getBytes(); 
40                   InetAddress address = mcu.getInetAddress(); 
41                   ByteArrayInputStream bais = 
42                           new ByteArrayInputStream(data, 0, data.length); 
43                   imgFrame.getImage(bais); 
44                   System.out.println("\n\nRecived: \"" + 
45                           "ip=" + address + 
46                           "\"\nMessage Length is: " + 
47                           data.length); 
48               } 
49           } catch (IOException e) { 
50               System.out.println("Exit..."); 
51               e.printStackTrace(); 
52           } 
53       } 
54    
55       public static class ImageFrame extends ClosableJFrame { 
56           private BufferedImage img = null; 
57    
58           public void getImage() { 
59               try { 
60                   File f = Futil.getReadFile("select an image file"); 
61                   getImage(f); 
62               } catch (IOException e) { 
63                   e.printStackTrace(); 
64               } 
65           } 
66    
67           public void getImage(File f) throws IOException { 
68               FileInputStream fis = new FileInputStream(f); 
69               getImage(fis); 
70           } 
71    
72           public void getImage(InputStream is) throws IOException { 
73               BufferedImage bi = ImageIO.read(is); 
74               is.close(); 
75               setImage(bi); 
76           } 
77    
78           public void setImage(BufferedImage bi) { 
79               img = bi; 
80               setSize(bi.getWidth(), bi.getHeight()); 
81               repaint(); 
82           } 
83    
84           public void paint(Graphics g) { 
85               if (img == null) { 
86                   g.drawString("no data rcvd", 0, this.getHeight() / 2); 
87                   return; 
88               } 
89               ; 
90               Graphics2D g2d = (Graphics2D) g; 
91               g2d.drawImage(img, null, 0, 0); 
92           } 
93       } 
94   } 
95