/** * Author : Bijoy Naick * Organization : Student, University of Waterlo * Major : Computer Science and Information Systems, 4th Year * Date : December, 1997 * URL : http://www.undergrad.math.uwaterloo.ca/~bnnaick/ * E.Mail : bnnaick@uwaterloo.ca * * ----------------------------------------------------------------------------- * Permission to use, copy, modify and distribute this software and its * documentation without fee for NON-COMMERCIAL purposes is hereby granted * provided that this notice with a reference to the original source and * the author appears in all copies or derivatives of this software. * * All other rights are reserved. * * THE AUTHOR MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF * THIS SOFTWARE, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. THE AUTHOR SHALL NOT BE LIABLE FOR * ANY DAMAGES SUFFERED BY ANYBODY AS A RESULT OF USING, MODIFYING OR * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. * ----------------------------------------------------------------------------- * * PARAMETERS : * ---------- * * 1. COUNT : The number of text, URL pairs that will be entered. * 2. TEXT : Each text string has the form TEXT1, TEXT2, ... * 3. URL : Each URL has the form URL1, URL2, ... * 4. BGCOLOR : The background color in RGB format * 5. TEXTCOLOR : The text color in RGB format * 6. FONT : The font to be used * 7. SIZE : The text size * 8. BOLD : "TRUE" if user wants boldface * 9. ITALIC : "TRUE" if user want italics * 10. PAUSE : The time to pause in the middle */ import java.applet.*; import java.awt.*; import java.net.*; import java.util.*; public class Ticker extends Applet implements Runnable { // // VARIABLE DECLARION // ================== // THREAD support. This is the main thread Thread mainThread = null; // the width and height of the applet private final int width = 450; private final int height = 40; // the co-ordinates of the string private int x, y; // arrays to store the text and associated URL private String text[]; private URL URL_List[]; // number of text,URL pairs private int numItems; // the current text,URL index in the arrays private int index = 0; // the font in which the text is displayed. // Default is Times New Roman private String fontName; private final String defaultFont = "Times New Roman"; // the font size of the displayed text. // Default size is 12 private int fontSize; private final int defaultSize = 12; // the background color of the applet // Default is Black private Color bg; private final Color defaultBg = Color.black; // the color of the text displayed. // Default is Green private Color fg; private final Color defaultFg = Color.green; // Indicate if text should be bold faced and/or italicised private int yesBold; private int yesItalic; // the time to pause in the middle. // Default is 2000; private int pause; private final int defPause = 2000; // speed control private final int speed = 90; // // END OF DECLARATION // ================== // The constructor of the Ticker class. Nothing is done here. //------------------------------------------------------------- public Ticker() { // There is nothing to do. } // Applet Information //------------------------------------------------------------- public String getAppletInfo() { return "Name: Ticker\r\n" + "Author: Bijoy Naick\r\n"; } // The init() method. // The parameters are read in and the values are stored in // variables. The background color and font are also set. //------------------------------------------------------------- public void init() { // temporary variables String currText, currURL; String param; // set applet size resize(width, height); // get the number of text, URL pairs specified numItems = getParam(getParameter("COUNT"), 0); // create two arrays to store the text and associated // URL's. text = new String[numItems]; URL_List = new URL[numItems]; // Loop "Count" times, get the text, URL pairs and // save them in the arrays. Each text, URL pairs is read // in using parameter names TEXT(1..COUNT), URL(1..COUNT) // The URL's are read in as strings, and converted to URL for (int i=0; i < numItems; i++) { currText = "TEXT" + String.valueOf(i+1); text[i] = getParameter(currText); currURL = "URL" + String.valueOf(i+1); try { URL_List[i] = new URL(getParameter(currURL)); } catch (MalformedURLException e) { // Nothing for now!!!! } } // get the time to pause in the middle pause = getParam(getParameter("PAUSE"), defPause); // Read in the font to be used. If not specified, set to default param = getParameter("FONT"); fontName = (param == null) ? defaultFont : param; // Read in the font size, and check if user wants Bold and Italics fontSize = getParam(getParameter("SIZE"), defaultSize); yesBold = getParam(getParameter("BOLD"), Font.BOLD, Font.PLAIN); yesItalic = getParam(getParameter("ITALIC"), Font.ITALIC, Font.PLAIN); // Get the background and text colors fg = getColor(getParameter("TEXTCOLOR"), defaultFg); bg = getColor(getParameter("BGCOLOR"), defaultBg); // set the background color and font setBackground(bg); setFont(new Font(fontName, yesBold + yesItalic, fontSize)); } // The destroy() method. // Invoked when the applet is destroyed. //------------------------------------------------------------- public void destroy() { // Nothing to do here. } // The paint() method. // Sets the color and writes the string //------------------------------------------------------------- public void paint(Graphics g) { // set text color g.setColor(fg); // write string to text g.drawString(text[index], x, y); } // The start() method. // Invoked when the applet is activated. // Create thread of control and start it. //------------------------------------------------------------- public void start() { mainThread = new Thread(this); mainThread.start(); } // The stop() method. // Invoked when the applet is deactivated. // Stop the main thread if it is not already null. //------------------------------------------------------------- public void stop() { if (mainThread != null) { mainThread.stop(); } } // The run() method. // This routine calculates the x and y co-ordinates, calls // paint and puts the thread to sleep for a while. The value of // index is updated. //------------------------------------------------------------- public void run() { // get font metrics FontMetrics fm = getFontMetrics(getFont()); // get the ascent for the current font int ascent = fm.getAscent(); // loop forever while (true) { // calculate x value so that the text is centered x = (width - fm.stringWidth(text[index]))/2; // scroll the text up for (y = height + ascent; y > 0; y--) { repaint(); // if text is in the middle, then pause if (y == ((height + fm.getAscent())/2) - 1 ) { try { Thread.sleep(pause); } catch (InterruptedException e) { // do nothing } } // make sure thread doesn't fly across screen try { Thread.sleep(speed); } catch (InterruptedException e) { // do nothing } } // update index to next string index = ((index + 1) % numItems); } } // event mouseUp() // Invoked when the user clicks the mouse on the applet. // A new window is opened with the URL corresponding to the // text which was displayed when the mouse was clicked. //------------------------------------------------------------- public boolean mouseUp(Event evt, int x, int y) { if (URL_List[index] != null) { getAppletContext().showDocument(URL_List[index]); return true; } else { showStatus("Check the URL! I think it's wrong"); retrun false; } } // HELPER ROUTINES TO GET PARAMETERS // --------------------------------- // Converts the parameter in "R,G,B" form into type Color. // If the input is invalid, returns the default. //------------------------------------------------------------- private Color getColor(String param, Color defColor) { int r, g, b; // if paramter is null, return default colour if (param == null) { return defColor; } // use StringTokenizer to parse the paramter which is // in "R,G,B" form. Then return the new Color. StringTokenizer st = new StringTokenizer(param, ","); try { r = Integer.parseInt(st.nextToken()); g = Integer.parseInt(st.nextToken()); b = Integer.parseInt(st.nextToken()); return new Color(r, g, b); } catch (Exception e) { return defColor; } } // OVERLOADED getParam routine. // Converts param from String to Integer. If param cannot // be converted, returns the default. //------------------------------------------------------------- private int getParam(String param, int defValue) { int retValue; // check if param is null if (param != null) { try { // convert to integer and return retValue = Integer.parseInt(param); return retValue; } catch (Exception e) { return defValue; } } // param is null, return default else { return defValue; } } // OVERLOADED getParam routine. // Used ONLY for font attributes such as bold and italics. // Converts param from String to Integer, depending on value // parameter. //------------------------------------------------------------- private int getParam(String param, int trueValue, int falseValue) { // Check that parameter is not null and that it is set to // TRUE. If so return trueValue. if ((param != null) && (param.equalsIgnoreCase("TRUE"))) { return trueValue; } // if not return falseValue else { return falseValue; } } }