// // RemoteCounter.java // version 961114 // copyright 1996 by bo larsson // all rights reserved // // bugs or feedback to bliss@seagull.net // // include all the java packages that are needed. import java.awt.*; import java.net.*; import java.io.*; import java.applet.*; // // The RemoteCounter applet connects to a cgi script on // the host. It expects data back in the form of a string // representing the number of times the script has been // executed. It can take two parameters: // // Dummy - some text that you want to show instead of the count. // PageId - the numeric id of the page to be counted. // public class RemoteCounter extends Applet { String count; int numberOfBytes; public void init() { String ret; byte bcount[] = new byte[20]; // handle the dummy parameter. if ((ret = getParameter("Dummy")) != null) { count = ret; // do the connection and retrieve data } else { // in a try block to catch exceptions during connection. try { int tmp; // build the string. String connectString = new String("http://www.seagull.net/bliss/cgi/jcounter/gscntr.pl?A="); String idParam; URL counterScript; // deal with the pageId argument if any. if ((idParam = getParameter("PageId")) != null) { counterScript = new URL(connectString.concat(idParam)); } else { counterScript = new URL(connectString); } // open the connection specified by the given URL counterScript.openConnection(); // open an input stream off the same URL. InputStream counterStream = counterScript.openStream(); // read input. while ((tmp = counterStream.read()) != -1 && numberOfBytes < 20) { bcount[numberOfBytes] = (byte)tmp; numberOfBytes++; } count = new String(bcount, 0, 0, numberOfBytes); } catch (Exception e) { System.exit(-1); } } } // paint the bytes retrieved in white on red. public void paint(Graphics g) { int cx, cy; // center horizontally. cx = (size().width - g.getFontMetrics().stringWidth(count)) /2 ; // center vertically. The x y values for drawString refer to // the BOTTOM of the font, so compensate by adding the height // instead of subtracting. cy = (size().height + g.getFontMetrics().getHeight()) /2 ; g.setColor(Color.red); g.fillRect(0, 0, size().width, size().height); g.setColor(Color.white); g.drawString(count, cx, cy); } }