View Javadoc
1 package krico.camserv; 2 import krico.util.match.*; 3 4 import java.applet.Applet; 5 6 import java.awt.*; 7 8 import java.awt.image.*; 9 10 import java.net.*; 11 12 import java.io.InputStream; 13 import java.io.IOException; 14 15 16 public class WebPanel extends Panel implements Runnable{ 17 Applet parent; 18 URL imgURL; 19 Image img; 20 long sleep; 21 Object lock = new Object(); 22 volatile boolean go = false; 23 MemoryImageSource source; 24 InputStream in; 25 Matcher find; 26 int width,height; 27 byte []pattern; 28 byte store[] = new byte[0]; 29 ColorModel cmdl = ColorModel.getRGBdefault(); 30 static String errStr = "Error getting image"; 31 32 public WebPanel(Applet applet, URL url,int width, int height,long sleepTime){ 33 super(); 34 parent = applet; 35 setBackground(parent.getBackground()); 36 sleep = sleepTime; 37 this.imgURL = url; 38 this.width = width; 39 this.height = height; 40 setSize(new Dimension(width,height)); 41 try{ 42 source = createCamSource(width,height); 43 }catch(Exception e){ 44 System.err.println("Could not create ImageSource: " + e); 45 }finally{ 46 img = createImage(source); 47 } 48 } 49 public Dimension getPreferredSize(){ 50 return getMinimumSize(); 51 } 52 public Dimension getMinimumSize(){ 53 return new Dimension(width,height); 54 } 55 public MemoryImageSource createCamSource(int width,int height) throws IOException{ 56 int size = width * height; 57 int []pixels = new int[size]; 58 int value = getBackground().getRGB(); 59 for (int i = 0; i < size; i++) { 60 pixels[i] = value; 61 } 62 MemoryImageSource ret = new MemoryImageSource(width, height, pixels, 0, width); 63 ret.setAnimated(true); 64 ret.setFullBufferUpdates(true); 65 return ret; 66 } 67 public synchronized boolean getGo(){ 68 if(lock != null) 69 return go; 70 else 71 return false; 72 } 73 public synchronized void setGo(boolean b){ 74 if(lock != null) 75 go = b; 76 try{ 77 if(go){ 78 System.out.println("Opening"); 79 in = imgURL.openConnection().getInputStream(); 80 Thread.currentThread().yield(); 81 }else{ 82 in = null; 83 } 84 }catch(Exception e){System.err.println("Ex stopping");} 85 86 } 87 public void run(){ 88 Thread me = Thread.currentThread( ); 89 me.setPriority(Thread.MIN_PRIORITY); 90 me.yield(); 91 while(getGo()){ 92 try{ 93 Thread.currentThread().sleep(sleep); 94 if(source != null){ 95 int [] pixels = getPix(); 96 if(pixels != null && pixels.length > 0){ 97 source.newPixels(pixels,cmdl,0,width); 98 } 99 } 100 101 }catch(Exception e){ 102 System.err.println("Exp in run: " + e); 103 e.printStackTrace(); 104 setGo(false); 105 } 106 } 107 System.err.println("Finished!"); 108 } 109 110 public void paint(Graphics g){ 111 if(img != null) 112 g.drawImage(img,0,0,this); 113 else 114 g.drawString(errStr,0,10); 115 } 116 117 public byte[] getImageData() throws IOException{ 118 while(pattern == null) getPattern(); 119 int found[] = find.findMatches(store); 120 int lgt = -1; 121 while((lgt = found.length) < 2){ 122 byte []read = new byte[in.available()]; 123 in.read(read); 124 byte aux[] = new byte[read.length + store.length]; 125 System.arraycopy(store,0,aux,0,store.length); 126 System.arraycopy(read,0,aux,store.length,read.length); 127 store = aux; 128 found = find.findMatches(store); 129 } 130 int st = found[0] + pattern.length; 131 int end = found[1]; 132 byte ret[] = new byte[end - st]; 133 System.arraycopy(store,st,ret,0,ret.length); 134 byte aux[] = new byte[store.length -end]; 135 System.arraycopy(store,end,aux,0,aux.length); 136 store = aux; 137 return ret; 138 } 139 public void getPattern() throws IOException{ 140 byte b[] = new byte[in.available()]; 141 in.read(b); 142 int i=0; 143 int nm = 0; 144 while(i<b.length && nm < 3){ 145 if(b[i] == '\n') 146 nm++; 147 i++; 148 } 149 if(nm < 3) return; 150 pattern = new byte[i]; 151 for(int j=0; j<i; j++) 152 pattern[j] = b[j]; 153 find = new KMPmatcher(pattern); 154 store = b; 155 } 156 public synchronized int[] getPix() throws IOException{ 157 if(lock == null) return null; 158 byte data[] = getImageData(); 159 Image img = Toolkit.getDefaultToolkit().createImage(data); 160 int[] pixels = new int[width * height]; 161 PixelGrabber pg = new PixelGrabber(img, 0, 0, width, height, pixels, 0, width); 162 try { 163 pg.grabPixels(); 164 } catch (InterruptedException e) { 165 System.err.println("interrupted waiting for pixels!"); 166 return new int[0]; 167 } 168 if ((pg.getStatus() & ImageObserver.ABORT) != 0) { 169 System.err.println("image fetch aborted or errored"); 170 return new int[0]; 171 } 172 return pixels; 173 } 174 175 /*** 176 * This method was generously added by 177 * <a href="timandmary@sympatico.ca">Tim Gibson</a> Thax! 178 */ 179 public void update(Graphics g) 180 { 181 182 paint(g); 183 } 184 185 186 } 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205

This page automatically generated by Maven