StockAppletpublic class StockApplet extends Applet implements StockNotify, SerializableThe 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_UPDATESmaximum number of updates displayed | private static final int | widthmaximum width of panel | private static final int | heightmaximum height of panel | private Vector | timevector containing time of each update | private Vector | checkboxesvector to hold references to checkboxes | private Hashtable | stockTabletable mapping stock names to stock data | private StockWatch | stockWatchreference to StockWatch server | private static String[] | name | private Color[] | color |
Methods Summary |
---|
public void | destroy()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 void | init()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 void | paint(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;
}
}
}
| int | scale(float y)Used to scale y-values.
return height - (int) (y*5+.5);
| void | setMouseHere(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 void | update(java.util.Date date, Stock[] stock)Notification of stock updates for a particular time.
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();
|
|