import java.awt.*; import java.applet.*; import java.net.*; import java.io.*; import java.net.MalformedURLException; import java.awt.Component; import javax.imageio.ImageIO; import java.lang.Thread; public class web_imagesUnleashed extends Applet implements Runnable { Image img; Thread t; public void init() { System.out.println("init"); this.setSize(1024, 768); } public void start() { t = new Thread(this); t.start(); } public void run () { String statusStr = getText("http://community-info.org/FileStatus.txt"); while (true) { if (statusStr.indexOf("Wrote") != -1) { repaint(0,0, 1024, 768); //the perl script wrote a new image url, so display it writeText(); //Write "Read" to http://community-info.org/FileStatus.txt, too let the perl script write again } try { t.sleep(1); } catch (Exception e) { System.err.println(e); System.out.println("Failed to run"); } statusStr = getText("http://community-info.org/FileStatus.txt"); } } // end of init() method public void paint(Graphics g) { System.out.println("paint"); URL url; try { String urlStr = getText("http://community-info.org/unleashed.txt"); url = new URL(urlStr); img = ImageIO.read(url); System.out.println("Write the image to the web page"); g.drawImage(img, 0, 0,this); System.out.println("Wrote"); } catch (Exception e) { System.out.println("Failed to paint"); System.err.println(e); } } public void writeText() { System.out.println("writeText"); String tempStr = ""; try { OutputStream to_file = System.out; // so we can see this as it takes place //Now use the URL class to parse the user-specified URL into //it's various parts URL url = new URL("http://community-info.org/FileStatus.asp"); String host = url.getHost(); int port = 80; String filename = url.getFile(); //create the socket Socket socket = new Socket(host, port); //get input and output streams for the socket InputStream from_server = socket.getInputStream(); PrintWriter to_server = new PrintWriter(socket.getOutputStream()); //send a GET to the web server to_server.print("GET " + filename + "\n\n"); to_server.flush(); //read the server's response int bytes_read; byte [] localBuffy = new byte[4096]; while ((bytes_read = from_server.read(localBuffy)) != -1) { to_file.write(localBuffy, 0, bytes_read); String localStr = new String(localBuffy); tempStr += localStr; } //finished reading from the server; close up socket.close(); to_file = null; } catch (Exception e) { System.out.print("Failed to writeText"); System.err.println(e); } } public String getText (String FileName) { System.out.println("getText"); String text = ""; try { URL url; URLConnection urlConn; DataInputStream dis; url = new URL(FileName); urlConn = url.openConnection(); urlConn.setDoInput(true); urlConn.setUseCaches(false); dis = new DataInputStream(urlConn.getInputStream()); String s; while ((s = dis.readLine()) != null) { text += s; } dis.close(); urlConn = null; url = null; } catch (MalformedURLException mue) { System.out.println("MalformedURLException during getText"); System.err.println(mue); } catch (IOException ioe) { System.out.println("IOException during getText"); System.err.println(ioe); } return text; } }