/*
* Banner.java
* A simple Java Banner set
* Usage:
*
* Ken Shirriff ken.shirriff@eng.sun.com
*
* Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
*
* SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
* THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
* TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
*
* Please refer to the file "license.txt"
* for further important copyright and licensing information.
*/
import java.awt.*;
import java.awt.image.*;
import java.applet.Applet;
import java.net.*;
import java.io.*;
import pcffont.*;
public class Banner extends Applet implements Runnable {
PCFFont font;
Thread thread;
int dispWidth, dispHeight;
Image scrollImg = null;
Image backImg = null;
Color bg = Color.white;
int topMargin;
int delay = 50;
int step = 5;
String back = null;
boolean okay = false;
public void init() {
// Read the parameters
String fontname = getParameter("font");
String text = getParameter("text");
String fgColor = getParameter("fg");
String bgColor = getParameter("bg");
String margin = getParameter("margin");
String delayString = getParameter("delay");
String stepString = getParameter("step");
String offset = getParameter("offset");
back = getParameter("backwards");
if (fontname == null || text == null) {
err("Must specify text and font");
return;
}
// Convert the text to unicode (i.e. handle \u1234)
text = PCFFont.toUnicode(text);
// Apply offset if any
if (offset != null) {
int off = Integer.parseInt(offset);
StringBuffer sb = new StringBuffer(text);
for (int i = 0; i < text.length(); i++) {
int ch = (int)sb.charAt(i);
if (ch+off < 256) {
sb.setCharAt(i, (char)(ch+off));
}
}
text = new String(sb);
}
if (back != null) {
StringBuffer sb = new StringBuffer(text);
StringBuffer sb2 = new StringBuffer(text);
for (int i = 0; i < sb.length(); i++) {
sb2.setCharAt(i,
sb.charAt(sb.length()-i-1));
}
text = new String(sb2);
}
// Load the font
showStatus("Loading font...");
try {
URL url = new URL(getDocumentBase(), fontname);
font = new PCFFont(url.openStream());
} catch (Exception e) {
err("Couldn't open the font "+fontname);
return;
}
showStatus("");
// Compute the size of the display and create the image buffer
dispWidth = size().width;
dispHeight = size().height;
if (margin != null) {
topMargin = Integer.parseInt(margin);
} else {
topMargin = (dispHeight-font.getHeight())/2;
if (topMargin<0) {
topMargin = 0;
}
}
backImg = createImage(dispWidth, dispHeight);
// Set the colors
font.setBgColor(null);
if (bgColor != null) {
try {
bg = parseColor(bgColor);
} catch (Exception e) {}
}
if (fgColor != null) {
try {
font.setFgColor(parseColor(fgColor));
} catch (Exception e) {
System.out.println("Exception "+e);
}
}
// Render the text into the image
scrollImg = font.stringImage(text);
// Set the scrolling parameters
if (delayString != null) {
delay = Integer.parseInt(delayString);
}
if (stepString != null) {
step = Integer.parseInt(stepString);
}
okay = true;
start();
}
String err_str = null;
// Display errors in the window to aid debugging
public void paint(Graphics g) {
if (err_str != null) {
g.setColor(Color.blue);
g.fillRect(0,0,500,500);
g.setColor(Color.black);
g.drawString(err_str, 10, 20);
}
}
void err(String s) {
err_str = s;
repaint();
showStatus(s);
System.out.println("Error: "+s);
}
String colornames[] = { "black", "blue", "cyan", "darkGray", "gray",
"green", "lightGray", "magenta", "orange", "pink", "red",
"white", "yellow", "clear"};
Color colors[] = {Color.black, Color.blue, Color.cyan, Color.darkGray,
Color.gray, Color.green, Color.lightGray, Color.magenta,
Color.orange, Color.pink, Color.red, Color.white,
Color.yellow, null};
// Convert a color name or #aabbcc color into a Java Color
Color parseColor(String s) throws Exception {
if (s.length()==7 && s.charAt(0)=='#') {
return new Color(
Integer.parseInt(s.substring(1,3),16), // r
Integer.parseInt(s.substring(3,5),16), // g
Integer.parseInt(s.substring(5,7),16)); // b
} else {
for (int i=0; i scrollImg.getWidth(null) + dispWidth) {
xpos = 0;
}
if (xpos < 0) {
xpos = scrollImg.getWidth(null) + dispWidth;
}
Graphics g = backImg.getGraphics();
g.setColor(bg);
g.fillRect(0, 0, dispWidth,
dispHeight);
g.drawImage(scrollImg, dispWidth-xpos, topMargin,
null);
try {
thread.sleep(50);
} catch (InterruptedException e) {}
getGraphics().drawImage(backImg, 0, 0, null);
}
}
public String getAppletInfo() {
return "Banner by Ken Shirriff shirriff@eng.sun.com";
}
}