FileDocCategorySizeDatePackage
StockApplet.javaAPI DocExample11295Wed Apr 19 11:20:40 BST 2000examples.stock

StockApplet

public class StockApplet extends Applet implements StockNotify, Serializable
The StockApplet exports a remote object, and contacts the StockWatch server to register interest in receiving stock updates. The applet displays the updates in a graph format.

Fields Summary
static final int
MAX_UPDATES
maximum number of updates displayed
private static final int
width
maximum width of panel
private static final int
height
maximum height of panel
private Vector
time
vector containing time of each update
private Vector
checkboxes
vector to hold references to checkboxes
private Hashtable
stockTable
table mapping stock names to stock data
private StockWatch
stockWatch
reference to StockWatch server
private static String[]
name
private Color[]
color
Constructors Summary
Methods Summary
public voiddestroy()
Called when applet is destroyed; the applet cancels all requests for stock updates.

	// cancel request for stock updates
	if (stockWatch != null) {
	    try {
		stockWatch.cancelAll(this);
	    } catch (Exception e) {
		// eat exception
	    }
	}
    
public voidinit()
Initialize applet: export the applet as a remote object that gets notified of stock updates.

	// record export exception
	Exception ee = null;

	try {
	    // initialize applet fields here to properly handle a restart
	    synchronized (this) {
		time = new Vector(MAX_UPDATES);
		stockTable = new Hashtable();
		stockWatch = null;
	    }

	    // export the applet as a remote object
	    System.out.println("StockApplet.init: exporting remote object");
	    try {
		UnicastRemoteObject.exportObject(this);
		ee = null;
	    } catch (java.rmi.server.ExportException e) {
		// use already exported object; remember exception
		ee = e;
	    }
	    
	    // lookup StockWatch server
	    URL base = getDocumentBase();
	    String serverName = "//" + base.getHost() + ":" +
		getParameter("registryPort") + "/example.stock.StockServer";
	    
	    System.out.println("StockApplet.init: looking up server");
	    stockWatch = (StockWatch)Naming.lookup(serverName);

	    // register interest in receiving stock updates
	    for (int i=0; i<name.length; i++) {
		System.out.println("StockApplet.init: watch stock " + name[i]);
		stockWatch.watch(name[i], this);
		stockTable.put(name[i], new StockData(name[i], color[i]));
	    }
	    System.out.println("StockApplet.init: done registering stocks");
	    
	} catch (Exception e) {
	    add(new Label("exception occurred during initialization; " +
			  "check the log"));
	    add(new Label(e.getClass().getName() + ": " + e.getMessage()));
	    
	    // fatal error
	    System.out.println("got exception: " + e.getMessage());
	    e.printStackTrace();
	    return;
	}

        setLayout(null);

	// clean up after previous run, remove old checkboxes
	if ((ee != null) && (checkboxes != null)) {
	    Enumeration oldCheckboxes = checkboxes.elements();
	    while (oldCheckboxes.hasMoreElements()) {
		SensitiveCheckbox cb = (SensitiveCheckbox)
		    oldCheckboxes.nextElement();
		remove(cb);
	    }
	}

	// draw checkboxes
	checkboxes = new Vector();
	Enumeration enum = stockTable.elements();
	int i=0; 
	while (enum.hasMoreElements()) {
	    StockData data = (StockData)enum.nextElement();
	    SensitiveCheckbox cb = new SensitiveCheckbox (data, this);
	    data.cb = cb;
	    checkboxes.add(cb);
	    add(cb);
	    cb.setState(data.displayed);
	    cb.reshape(10,i++*25+35,110,18);
	}
    
public voidpaint(java.awt.Graphics g)
Called to repaint the panel.


        // draw black boarder
        g.setColor(Color.black);
        g.drawRect(0,0,width-1,height-1);

        float miny = 0.0f;
        float maxy = 75.0f;

	// draw all stock data
	Enumeration enum = stockTable.elements();
	while (enum.hasMoreElements()) {
	    StockData data = (StockData)enum.nextElement();

            int size;
            Stock[] updates;
            synchronized (data.updates) {
                size = data.updates.size();
                updates = new Stock[size];
                data.updates.copyInto(updates);
            }
	    
            g.setColor(data.color);

            if (data.displayed) {
		// draw box around checkbox if mouse is over it

                if (data.cb != null && data.cb.haveMouse()) {
                    Point p = data.cb.location();
                    Dimension d = data.cb.size();

                    g.drawRect(p.x-1, p.y-1, d.width+4, d.height+4);
                    g.drawRect(p.x-2, p.y-2, d.width+4, d.height+4);
		    // point to graph for stock
		    if (size > 0)
                        g.drawLine(p.x+d.width+2,p.y+10,150,
			scale(updates[0].current));
                }
		// draw graph of updates for this stock
                int x = 150, inc = 10;
                for (int i = 0; i < size; i++) {
                    if (updates[i] != null) {
                        g.drawRect(x-1,scale(updates[i].current)-1,3,3);
                        if ((i < size - 1) && updates[i + 1] != null) {
                            int x2 = x + inc;
                            g.drawLine(x, scale(updates[i].current), x2,
                                       scale(updates[i + 1].current));
                        }
                    }
		    x += inc;
		}
	    }
        }
    
intscale(float y)
Used to scale y-values.

        return height - (int) (y*5+.5);
    
voidsetMouseHere(boolean display)
Make sure that mouseHere is set properly (fix for windows display problems).

	Enumeration enum = stockTable.elements();
	while (enum.hasMoreElements()) {
	    StockData data = (StockData)enum.nextElement();
	    data.cb.mouseHere = display;
	}
    
public synchronized voidupdate(java.util.Date date, Stock[] stock)
Notification of stock updates for a particular time.

param
date the time of the stock update
param
stocks an array containing the stocks for which the object has registered interest.

    
                                       
            
    
	System.out.println("StockApplet.update: " + date);
	// record date
	if (time.size() == MAX_UPDATES) { 
	    time.removeElementAt(0);
	}
	time.addElement(date);

	// record individual stock updates
	int numUpdates = time.size();
	for (int i=0; i<stock.length; i++) {
	    StockData data = (StockData)stockTable.get(stock[i].symbol);
	    if (data != null) {
		data.update(stock[i], numUpdates);
	    }
	}
	repaint();