//------------------------------------------------------------------------------ // Rainbow Banner Applet // // Copyright 1996, All rights reserved // You are free to use this java source code at your desire. // You also understand that by using this code, you are on your own. // I cannot offer any support if you have problems and no warrantees // are implied or offered for your use of this source code. // // Applet Parameters: // NAME="BANNER" VALUE="banner" - The banner string // NAME="FONTFACE" VALUE="Arial" - The banner font name // NAME="FONTSIZE" VALUE=24 - The point size of the banner font // NAME="DELAY" VALUE=125 - The delay (miliseconds) between each draw // NAME="NUMCOLORS" VALUE=30 - The number of colors to draw with // // Version: 0.9 // Author: Anupriyo Chakravarti (anupriyo@delphi.com) // Created: 8/17/96 // To do: // 1. The fontface parameter is not working. I must be doing something wrong. // 2. Make background color a parameter. // 3. Get rid of the annoying flickering. // // Histroy: // 08/25/96 Got rid of the flickering. It seems that when 'repaint' is called, // it first calls 'update' to clear the window with a gray background before // calling 'paint'. This caused the flickering. The trick is to do // the painting in the redefined 'update' method rather than in the // paint' method. //------------------------------------------------------------------------------ import java.awt.*; import java.applet.Applet; public class banner extends Applet implements Runnable { Font outFont; // The font on the banner Color[] arrColors; // The colors of the rainbow char[] arrChars; // The characters in the banner int nChars; // Number of characters in the banner int nColors = 32; // The number of colors wanted, set through a parameter int iNextColor = 0; // The start color index to while painting int iDelay = 125; // The delay (miliseconds) between each paint float fSat = (float)1.00; // The color Saturation float fBrt = (float)0.85; // The color Brightness boolean keepRunning = true; // Thread termination flag // Class initialization code public void init() { // Load the paramters String str = getParameter("BANNER"); if (str == null) str = "banner"; String strFontName = getParameter("FONTFACE"); if (strFontName == null) strFontName = "Arial"; int nPointSize = 24; int nR = 0xFF; int nG = 0xFF; int nB = 0xFF; try { nPointSize = Integer.parseInt(getParameter("FONTSIZE")); iDelay = Integer.parseInt(getParameter("DELAY")); nColors = Integer.parseInt(getParameter("NUMCOLORS")); iNextColor = Integer.parseInt(getParameter("STARTCOLOR")); if ((iNextColor < 0) || (iNextColor >= nColors)) { iNextColor = 0; } String strColor = getParameter("BGCOLOR"); nR = Integer.parseInt(strColor.substring(0, 2), 16); nG = Integer.parseInt(strColor.substring(2, 4), 16); nB = Integer.parseInt(strColor.substring(4, 6), 16); fSat = (float)Integer.parseInt(getParameter("SATURATION"))/(float)100; fBrt = (float)Integer.parseInt(getParameter("BRIGHTNESS"))/(float)100; } catch (NumberFormatException e) { } // Fill the color array arrColors = new Color[nColors]; for (int i = 0; i < nColors; i++) { float hue = (float)i / (float)nColors; arrColors[i] = Color.getHSBColor((float)hue, fSat, fBrt); } // create the output font outFont = new Font(strFontName, Font.BOLD, nPointSize); // fill the banner characters in the character array nChars = str.length(); arrChars = new char[nChars]; str.getChars(0, nChars, arrChars, 0); // set the applet background setBackground(new Color(nR, nG, nB)); // ready to paint repaint(); } public void update(Graphics g) { invalidate(); draw(g); } // This routine handles the painting public void draw(Graphics graphic) { graphic.setFont(outFont); // x and y hold the left baseline coordinates of the position where // the next character will be drawn int y = graphic.getFontMetrics(outFont).getMaxAscent(); int x = 0; // paint each character in different colors from the color array int iColor = iNextColor; for (int i = 0; i < nChars; i++) { graphic.setColor(arrColors[iColor]); graphic.drawChars(arrChars, i, 1, x, y); x += graphic.getFontMetrics(outFont).charWidth(arrChars[i]); iColor = iColor < (nColors-1) ? iColor+1 : 0; } // next time start the painting with this color iNextColor = iNextColor < (nColors-1) ? iNextColor+1 : 0; } // Implements Runnable Interface public void run() { // paint and sleep till we are done while (keepRunning) { repaint(); try { Thread.sleep(iDelay); } catch (InterruptedException e) { return; } } } public void start() { // start a new thread keepRunning = true; (new Thread(this)).start(); } public void stop() { // we are done keepRunning = false; } // For running standalone public static void main(String[] argv) { // Create the frame and launch banner Frame f = new Frame("bannerFrame"); f.show(); banner x = new banner(); f.add("Center", x); x.init(); x.start(); } // Constructor public banner() { } } // end class banner