IM269 Coursework 3: HitApplet.java


// IM269 - Coursework 3: RMI Client/Server
// Semester A, 7th December 1998
// Eamonn Martin (BSc Computing)
// Student ID: 96/D59682
// efm001@unl.ac.uk

// RMI Hit Counter Client Applet (hit-counter)

package hitter;

import java.awt.*;
import java.rmi.*;

import hitter.HitCounter.Info;

public class HitApplet extends java.applet.Applet {
	private static final int digits = 6;
	private int hits;

	// Get the hit-count for this document from the HitCounter RMI server
	public void init() {
		String name = "//" + getCodeBase().getHost() + "/HitCounter";
		try {
			HitCounter hitter = (HitCounter)Naming.lookup(name);
			hits = hitter.getHits(""+getDocumentBase(), getParameter("log"));
		} catch (Exception e) {
			hits = -1;
		}
		setBackground(Color.black);
		setForeground(Color.white);
	}

	// Display the counter value
	public void paint(Graphics g) {
		String s = "", c = (hits==-1) ? "?" : "0";
		if (hits!=-1) s += hits; 
		while (s.length() < digits) s = c + s;
		g.setFont(new Font("Monospaced", Font.PLAIN, 12));
		g.drawString(s, 2, 1 + g.getFontMetrics().getMaxAscent());
	}
}

Go To: IM269: Programming The Internet