//------------------------------------------------------------------------------ // thought.java - Give a thought of the day // Copyright 1997, All rights reserved // Version: 1.0 // Author: Jon McCain // Created: 10/18/97 // //------------------------------------------------------------------------------ // Parameters: datafile - Name of the file with the thoughts. The file // should contain one thought per line // maxcount - Number of thoughts in the file // import java.io.*; import java.awt.*; import java.net.*; import java.util.*; public class thought extends java.applet.Applet implements Runnable { Thread doit = null; String datafile; String message; int count,maxcount; Random r = new Random(); public String getAppletInfo() { return "Thought of the Day 1.0 by Jon McCain"; } //randomly pick a thought from the data file void getthought() { DataInputStream dis; // randomly pick a number from 1 to maxcount int n = 1 + Math.abs(r.nextInt()) % maxcount; //now find that line in the list and set message to it try { count = 1; URL u = new URL(getCodeBase() + datafile ); dis = new DataInputStream( u.openConnection().getInputStream() ); while ( (message=dis.readLine()) != null && count != n) { count++; } } catch (Exception e) { System.out.println( e ); } } // Read the parameters public void init() { datafile = getParameter("datafile"); String s = getParameter("maxcount"); maxcount = (s != null) ? Integer.valueOf(s).intValue() : 1; getthought(); } // draw a box and show the message // Note: can only handle 2 lines of text. public void paint(Graphics g) { Dimension d = size(); int pos; //draw the box g.setColor(Color.black); g.fillRect(0,0,d.width,d.height); g.setColor(Color.gray); g.drawRect(0,0,d.width,d.height); g.drawRect(1,1,d.width - 2,d.height - 2); g.setColor(Color.red); //see if can fit whole message on one line pos = message.length(); while (g.getFontMetrics().stringWidth(message.substring(0,pos)) > d.width - 14) { pos--; } g.drawString(message.substring(0,pos),5,14); //wrap around to second line if need too. if (message.length() > pos) g.drawString(message.substring(pos,message.length()),5,27); } // on a mouse click get a new thought public boolean mouseUp(java.awt.Event evt, int x, int y) { getthought(); repaint(); return true; } public boolean mouseEnter(java.awt.Event evt, int x, int y) { getAppletContext().showStatus("Click for another thought"); return true; } public boolean mouseExit(java.awt.Event evt, int x, int y) { getAppletContext().showStatus(""); return true; } public void run() { Thread.currentThread().setPriority(Thread.MIN_PRIORITY); } public void start() { if (doit == null) { doit = new Thread(this); doit.start(); } getthought(); } public void stop() { doit = null; } }