// DspQuote.class
//-----------------
// Written by Eric Pence, 1997
// This applet displays a random quote from the file DspQuote.txt.
// Each quote in this file must exist in a single record.
// The calling html document has the following parms (defaults are shown):
//
// <PARAM name = File value = "DspQuote.txt"> File containing quotes
// <PARAM name = FontName value = "Helvetica"> Font to use
// <PARAM name = FontSize value = "12"> Font size to use
// <PARAM name = BGColor value = "ffffff"> Background color
// <PARAM name = TxtColor value = "000000"> Text color
import java.applet.*;
import java.awt.*;
import java.net.*;
import java.util.Vector;
import java.io.*;
public class DspQuote extends Applet {
private String QuoteFile;
private String Font_Name;
private String QuoteTxt;
private String str;
private String str_work;
private Color bgColor;
private Color txtColor;
private Font dspFont;
private FontMetrics fm;
private int Font_Size;
private int widthOneChar;
private int charsPerLine;
private int charsInString;
private BufferedReader inFile;
private Vector QuoteVec;
public void init() {
try {
QuoteFile = getParameter("File");
if (QuoteFile.equals(null))
QuoteFile ="DspQuote.txt";
Font_Name = getParameter("FontName");
if (Font_Name.equals(null))
Font_Name = "Helvetica";
str = getParameter("FontSize");
if (str != null)
Font_Size = Integer.parseInt(str);
else
Font_Size = 12;
str = getParameter("BGColor");
if (str != null)
bgColor = new Color(Integer.valueOf(str,16).intValue());
else
bgColor = Color.white;
setBackground(bgColor);
str = getParameter("TxtColor");
if (str != null)
txtColor = new Color(Integer.valueOf(str,16).intValue());
else
txtColor = Color.black;
} catch (Exception e) {
QuoteFile =" DspQuote.txt";
Font_Name = "Helvetica";
Font_Size = 12;
bgColor = Color.white;
setBackground(bgColor);
txtColor = Color.black;
}
dspFont = new Font(Font_Name,Font.PLAIN,Font_Size);
fm = getFontMetrics(dspFont);
// Load the quotes and randomly select and display one.
QuoteVec = new Vector();
QuoteTxt = this.loadQuotes();
if (QuoteTxt.equals(" "))
QuoteTxt = this.getQuote();
}
// Load quotes into a string vector.
String loadQuotes() {
try {
inFile = new BufferedReader(new InputStreamReader(new URL(getCodeBase(),QuoteFile).openStream()));
} catch (Exception ex) {
return "An IO error occured while opening the " + QuoteFile + " file.";
}
try {
while ((str = inFile.readLine()) != null) {
QuoteVec.addElement(str);
}
} catch (IOException ex) {
return "An IO error occured while reading the " + QuoteFile + " file.";
}
return " ";
}
// Get a random quote.
String getQuote() {
str = (String) (QuoteVec.elementAt((int) (Math.random() * QuoteVec.size())));
return str;
}
// Get another quote when applet is clicked on.
public boolean mouseDown(Event e,int x,int y) {
showStatus("");
QuoteTxt = this.getQuote();
repaint();
showStatus("Click on quote to display another quote.");
return true;
}
// Display status message and change pointer when mouse is over applet area.
public boolean mouseEnter(Event e,int x,int y) {
this.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
showStatus("Click on quote to display another quote.");
return true;
}
// Clear status message and reset pointer when mouse leaves applet area.
public boolean mouseExit(Event e,int x,int y) {
this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
showStatus("");
return true;
}
// Display the quote.
public void paint(Graphics g) {
int x, y,
strWidth,
dspWidth,
strIdx,
endIdx,
lastSpace,
lastIdx = 0,
idx1,
idx2;
g.setFont(dspFont);
g.setColor(txtColor);
Dimension dsp = this.getSize();
dspWidth = dsp.width;
strWidth = fm.stringWidth(QuoteTxt);
// Wrap text if it doesn't fit in display area.
if (strWidth > dspWidth) {
// Calculate the width of a single character.
charsInString = QuoteTxt.length();
widthOneChar = strWidth / charsInString;
// Calculate the number of characters on a line.
charsPerLine = (dspWidth / widthOneChar) - 12; // allow a little leeway
// Start the multi-line text left/top.
x = widthOneChar;
y = fm.getHeight();
strIdx = 0;
endIdx = charsPerLine;
boolean moreLines = true;
boolean lastTime = false;
while (moreLines) {
str_work = QuoteTxt.substring(strIdx, endIdx);
if (str_work.length() < charsPerLine)
idx2 = str_work.length();
else {
// Locate last space in current string so words wont truncate
lastSpace = str_work.lastIndexOf(' ');
if (lastSpace != 0)
idx2 = lastSpace;
else
idx2 = charsPerLine;
}
// Left-justify string (ignore leading space).
if (str_work.substring(0, 1).equals(" "))
str = str_work.substring(1, idx2);
else
str = str_work.substring(0, idx2);
g.drawString(str,x,y); // Write the line of text
// Determine if there is more to display
lastIdx += idx2;
if ((lastIdx < charsInString) && (!lastTime)) {
y += fm.getHeight(); // Drop down to next line
strIdx = lastIdx + 1;
endIdx = strIdx + charsPerLine;
if (endIdx >= charsInString) {
endIdx = charsInString;
lastTime = true;
}
}
else
moreLines = false;
}
}
else {
// Center the single-line text
x = (dspWidth - strWidth) >> 1;
y = ((dsp.height - fm.getHeight()) >> 1) + fm.getHeight() - fm.getDescent();
g.drawString(QuoteTxt,x,y); // Write the line of text
}
}
}