home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-09-02 | 12.7 KB | 513 lines |
- /************************************************************************************************
-
- Displays a vertically scrolling a list of web links, clicking on a link will send the browser
- to that site.
-
- <applet code="LinkScroll.class" width=w height=h>
- <param name="delay" value="25">
- <param name="pause" value="2000">
- <param name="fgcolor" value="color">
- <param name="bgcolor" value="color">
- <param name="vlcolor" value="#663399">
- <param name="shadow" value="x,y,color">
- <param name="border" value="size,color">
- <param name="font" value="name,style,size">
- <param name="link-1" value="string">
- <param name="url-1" value="url" or "url,target">
- <param name="status-1" value="string">
- ...
- <param name="link-n" value="string">
- <param name="url-n" value="url" or "url,target">
- <param name="status-n" value="string">
- </applet>
-
- ************************************************************************************************/
-
- import java.awt.*;
- import java.net.*;
- import java.util.*;
- import java.applet.Applet;
-
- public class LinkScroll extends Applet implements Runnable {
-
- // Thread control variables.
-
- Thread scrollThread;
- boolean paused = false;
-
- // Parameters and defaults.
-
- int delay = 25;
- int pause = 2000;
- Color fgColor = Color.blue;
- Color bgColor = Color.white;
- Color vlColor = new Color(102,51,153);
- int shxOffset = 0;
- int shyOffset = 0;
- Color shColor = Color.lightGray;
- int bdSize = 0;
- Color bdColor;
- String fontName = "Dialog";
- int fontStyle = Font.PLAIN;
- int fontSize = 12;
-
- // Link data vectors.
-
- Vector linkList = new Vector();
- Vector urlList = new Vector();
- Vector targetList = new Vector();
- Vector statusList = new Vector();
- Vector hitList = new Vector();
-
- // Current link data.
-
- String link;
- URL url;
- String target;
- String status;
- Boolean hit;
- int currentLink;
-
- // Values for the offscreen image.
-
- int xOffset;
- int yOffset;
- int yOffsetBase;
- int yOffsetMax;
- int yOffsetMin;
- Font font;
- Dimension offDimension;
- Image offImage;
- Graphics offGraphics;
-
- // Status window flag.
-
- boolean mouseIn = false;
-
- public void init() {
-
- String s, t, u;
- StringTokenizer st;
- int i, n;
- Graphics g;
- Dimension d;
- FontMetrics fm;
-
- // Get delay time.
-
- try {
- s = getParameter("delay");
- if (s != null)
- if ((n = Integer.parseInt(s)) > 0)
- delay = n;
- }
- catch (Exception e) {}
-
- // Get pause time.
-
- try {
- s = getParameter("pause");
- if (s != null)
- if ((n = Integer.parseInt(s)) > 0)
- pause = n;
- }
- catch (Exception e) {}
-
- // Get foreground, background and visited-link colors.
-
- try {
- s = getParameter("fgcolor");
- if (s != null)
- fgColor = getColorParm(s);
- }
- catch (Exception e) {}
- bdColor = fgColor;
-
- try {
- s = getParameter("bgcolor");
- if (s != null)
- bgColor = getColorParm(s);
- }
- catch (Exception e) {}
-
- try {
- s = getParameter("vlcolor");
- if (s != null)
- vlColor = getColorParm(s);
- }
- catch (Exception e) {}
-
- // Get shadow offsets and color.
-
- try {
- s = getParameter("shadow");
- if (s != null) {
- st = new StringTokenizer(s, ",");
- if ((n = Integer.parseInt(st.nextToken())) > 0)
- shxOffset = n;
- if ((n = Integer.parseInt(st.nextToken())) > 0)
- shyOffset = n;
- shColor = getColorParm(st.nextToken());
- }
- }
- catch (Exception e) {}
-
- // Get border size and color.
-
- try {
- s = getParameter("border");
- if (s != null) {
- st = new StringTokenizer(s, ",");
- if ((n = Integer.parseInt(st.nextToken())) > 0)
- bdSize = n;
- bdColor = getColorParm(st.nextToken());
- }
- }
- catch (Exception e) {}
-
- // Get the font.
-
- try {
- s = getParameter("font");
-
- // Font name.
-
- st = new StringTokenizer(s, ",");
- t = st.nextToken();
- if (t.equalsIgnoreCase("Courier"))
- fontName = "Courier";
- else if (t.equalsIgnoreCase("Dialog"))
- fontName = "Dialog";
- else if (t.equalsIgnoreCase("Helvetica"))
- fontName = "Helvetica";
- else if (t.equalsIgnoreCase("Symbol"))
- fontName = "Symbol";
- else if (t.equalsIgnoreCase("TimesRoman"))
- fontName = "TimesRoman";
-
- // Font style.
-
- t = st.nextToken();
- if (t.equalsIgnoreCase("plain"))
- fontStyle = Font.PLAIN;
- else if (t.equalsIgnoreCase("bold"))
- fontStyle = Font.BOLD;
- else if (t.equalsIgnoreCase("italic"))
- fontStyle = Font.ITALIC;
- else if (t.equalsIgnoreCase("boldItalic"))
- fontStyle = Font.BOLD + Font.ITALIC;
-
- // Font size.
-
- t = st.nextToken();
- if ((n = Integer.parseInt(t)) > 0)
- fontSize = n;
- }
- catch (Exception e) {}
-
- // Get the link data.
-
- s = null;
- i = 1;
- hit = Boolean.FALSE;
- do {
- try {
- s = getParameter("link-" + i);
- if (s != null) {
- link = s;
-
- // Get the URL, target and status window message.
-
- url = null;
- target = null;
- try {
- t = getParameter("url-" + i);
- st = new StringTokenizer(t, ",");
- u = st.nextToken();
- try {
- url = new URL(getDocumentBase(), u);
- if (st.hasMoreTokens())
- target = st.nextToken();
- }
- catch (MalformedURLException e) {}
- }
- catch (Exception e) {}
-
- status = null;
- try {
- t = getParameter("status-" + i);
- if (t != null)
- status = t;
- }
- catch (Exception e) {}
-
- // Add the link data to the lists.
-
- linkList.addElement(link);
- urlList.addElement(url);
- targetList.addElement(target);
- statusList.addElement(status);
- hitList.addElement(hit);
-
- i++;
- }
- }
- catch (Exception e) {}
- } while (s != null);
-
- // Set the font values.
-
- g = getGraphics();
- d = size();
- font = g.getFont();
- g.setFont(font = new Font(fontName, fontStyle, fontSize));
- fm = g.getFontMetrics();
- xOffset = fm.getMaxAdvance();
- yOffsetBase = size().height - (size().height - fm.getHeight()) / 2 - fm.getDescent();
- yOffsetMin = -fm.getHeight() - fm.getDescent();
- yOffsetMax = d.height + fm.getHeight();
- yOffset = yOffsetMax;
-
- // Initialize the current link.
-
- link = (String) linkList.firstElement();
- url = (URL) urlList.firstElement();
- status = (String) statusList.firstElement();
- hit = (Boolean) hitList.firstElement();
- currentLink = 0;
- }
-
- private Color getColorParm(String s) {
-
- int r, g, b;
-
- // Check if a pre-defined color is specified.
-
- if (s.equalsIgnoreCase("black"))
- return(Color.black);
- if (s.equalsIgnoreCase("blue"))
- return(Color.blue);
- if (s.equalsIgnoreCase("cyan"))
- return(Color.cyan);
- if (s.equalsIgnoreCase("darkGray"))
- return(Color.darkGray);
- if (s.equalsIgnoreCase("gray"))
- return(Color.gray);
- if (s.equalsIgnoreCase("green"))
- return(Color.green);
- if (s.equalsIgnoreCase("lightGray"))
- return(Color.lightGray);
- if (s.equalsIgnoreCase("magenta"))
- return(Color.magenta);
- if (s.equalsIgnoreCase("orange"))
- return(Color.orange);
- if (s.equalsIgnoreCase("pink"))
- return(Color.pink);
- if (s.equalsIgnoreCase("red"))
- return(Color.red);
- if (s.equalsIgnoreCase("white"))
- return(Color.white);
- if (s.equalsIgnoreCase("yellow"))
- return(Color.yellow);
-
- // If the color is specified in HTML format, build it from
- // the red, green, blue values.
-
- if (s.length() == 7 && s.charAt(0) == '#') {
- r = Integer.parseInt(s.substring(1,3),16);
- g = Integer.parseInt(s.substring(3,5),16);
- b = Integer.parseInt(s.substring(5,7),16);
- return(new Color(r, g, b));
- }
-
- // Default to black.
-
- return(Color.black);
- }
-
- public void start() {
-
- if (scrollThread == null) {
- scrollThread = new Thread(this);
- scrollThread.start();
- }
- }
-
- public void stop() {
-
- if (scrollThread != null) {
- scrollThread.stop();
- scrollThread = null;
- }
- }
-
- public boolean mouseEnter(Event e, int x, int y) {
-
- // Show address of current URL in status window.
-
- mouseIn = true;
- if (status != null)
- getAppletContext().showStatus(status);
- else if (url != null)
- getAppletContext().showStatus(url.toString());
- else
- getAppletContext().showStatus("");
- return true;
- }
-
- public boolean mouseExit(Event e, int x, int y) {
-
- // Clear status window.
-
- mouseIn = false;
- getAppletContext().showStatus("");
- return true;
- }
-
- public boolean mouseDown(Event e, int x, int y) {
-
- int i;
-
- // Mark current link and any equivalent links as visited.
-
- hit = Boolean.TRUE;
- hitList.setElementAt(hit, currentLink);
- for (i = 0; i < hitList.size(); i++)
- if (url.sameFile((URL) urlList.elementAt(i)))
- hitList.setElementAt(hit, i);
-
- // Link to the current URL.
-
- if (url != null)
- if (target != null)
- getAppletContext().showDocument(url, target);
- else
- getAppletContext().showDocument(url);
- return true;
- }
-
- public void run() {
-
- // Lower this thread's priority.
-
- Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
-
- long startTime;
-
- // Get the start time.
-
- startTime = System.currentTimeMillis();
-
- // This is the animation loop.
-
- while (Thread.currentThread() == scrollThread) {
-
- // Advance the animation if not paused.
-
- if (!paused) {
-
- // Go to the next link.
-
- if (--yOffset < yOffsetMin ) {
- yOffset = yOffsetMax;
- if (++currentLink >= linkList.size())
- currentLink = 0;
-
- // Set the current link data.
-
- link = (String) linkList.elementAt(currentLink);
- url = (URL) urlList.elementAt(currentLink);
- target = (String) targetList.elementAt(currentLink);
- status = (String) statusList.elementAt(currentLink);
- hit = (Boolean) hitList.elementAt(currentLink);
-
- // Update the status window.
-
- if (mouseIn)
- if (status != null)
- getAppletContext().showStatus(status);
- else if (url != null)
- getAppletContext().showStatus(url.toString());
- else
- getAppletContext().showStatus("");
- }
-
- if (yOffset == yOffsetBase)
- paused = true;
- repaint();
- try {
- startTime += delay;
- Thread.sleep(Math.max(0, startTime - System.currentTimeMillis()));
- }
- catch (InterruptedException e) {
- break;
- }
- }
-
- else {
- try {
- startTime += pause;
- Thread.sleep(Math.max(0, startTime - System.currentTimeMillis()));
- }
- catch (InterruptedException e) {
- break;
- }
- paused = false;
- }
- }
- }
-
- public void paint(Graphics g) {
-
- update(g);
- }
-
- public void update(Graphics g) {
-
- Dimension d = size();
- int i;
-
- // Create the offscreen graphics context, if no good one exists.
-
- if (offGraphics == null || d.width != offDimension.width || d.height != offDimension.height) {
- offDimension = d;
- offImage = createImage(d.width, d.height);
- offGraphics = offImage.getGraphics();
- }
-
- // Fill in background.
-
- offGraphics.setColor(bgColor);
- offGraphics.fillRect(0, 0, d.width, d.height);
-
- // Set the font.
-
- offGraphics.setFont(font);
-
- // Draw shadow.
-
- if (shxOffset != 0 || shyOffset != 0) {
- offGraphics.setColor(shColor);
- offGraphics.drawString(link, xOffset + shxOffset, yOffset + shyOffset);
- }
-
- // Draw text.
-
- if (hit == Boolean.TRUE)
- offGraphics.setColor(vlColor);
- else
- offGraphics.setColor(fgColor);
- offGraphics.drawString(link, xOffset, yOffset);
-
- // Draw border.
-
- offGraphics.setColor(bdColor);
- for (i = 0; i < bdSize; i++)
- offGraphics.drawRect(i, i, d.width - 2 * i - 1, d.height - 2 * i - 1);
-
- // Paint the image onto the screen.
-
- g.drawImage(offImage, 0, 0, this);
- }
- }
-