home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-10-04 | 7.0 KB | 226 lines |
- import java.awt.*;
- import java.applet.*;
- import java.util.*;
-
- /***************************************************************************
- Applet: HorizontalScroller
- Author: Imagination's End, mods by Jeff Rhyason
- Desc: Doesn't the name basically state it all?
-
- Date Created: 19960422
- Date Modified: 19961005
-
- This is a finicky version.
- ***************************************************************************/
-
- /*
- HorizontalScroller
-
- Overview
-
-
- The second in a series of text-scrolling applets that will
- be included in the Mortar package. It takes uses an off-
- screen image buffer to update an area of drawn text in
- increments small enough that the text appears to move
- right to left somewhat smoothly.
-
- Like most applets, characteristics and data can be
- customized through PARAM tags in the HTML file from
- which the applet is invoked.
-
- Each line of text can be customized individually, to have
- a particular foreground or background colour. Each line of
- text can be assigned a URL which the browser is referred to
- if a mouseclick is detected over the line.
-
- General Parameters
-
- General Parameters are mostly defaults which are used if
- a line does not specify its own characteristics.
-
- TIMEOUT: delay between each movement (in milliseconds)
- BACKCOLOR: the default background color for a line of text (black)
- TEXTCOLOR: the default foreground color (white) See note on COLORS below
- FADESTEPS: number of color steps to fade the text to at the ends (default 5)
- TEXT: the string which will get written
- PTSIZE: the font size (calculated automatically)
- FONT: The font to use (defaults to Courier)
-
- Note: It is recommended that, for best performance, unless you have
- a specific need, you do not specify PTSIZE, as it can be calculated
- automatically based on the height of the applet. Also, applets should
- generally not be too large (40 pixels high is about the limit).
-
- Also, at this time it is required that you choose monospaced fonts for
- the applet to look proper. (such as Courier, the default).
-
- It is furthermore required that the TEXT you display have at least
- 2 times the FADESTEPS + 2 characters, where FADESTEPS defaults to 5.
-
- Types of Values
-
- Strings are usually stored with double quotes (" ") around them.
- They are required if the string is to have a space in it, otherwise
- the space would get confused with the HTML standard of delimiting
- parameters with spaces.
-
- Colors are represented in a numerical format only. Each pair of
- digits in a series of three is the hexadecimal representation for
- the Red, Green, or Blue component (in that order).
-
-
-
- Examples
-
- <APPLET CODE=HorizontalScroller.class WIDTH=400 HEIGHT=30>
- <PARAM NAME=TEXT VALUE="Hello there, fellow Mortar immortal! What do you think of Mortar so far?">
- <PARAM NAME=TEXTCOLOR VALUE=EEEEEE>
- <PARAM NAME=BACKCOLOR VALUE=000055>
- </APPLET>
-
- */
-
- public class HorizontalScroller extends Applet implements Runnable
- {
- Thread scroll;
- int timeout,fadeSteps,tWidth,cWidth;
- Color clrBack, clrText, clrFade[];
- String strText;
- Font strFont;
- FontMetrics fm;
- Image os;
-
- public void init()
- {
- fm = null;
- timeout = IEExtend.strInt(getParameter("TIMEOUT"),200);
- clrBack = IEExtend.strColor(getParameter("BACKCOLOR"),new Color(0,0,0));
- clrText = IEExtend.strColor(getParameter("TEXTCOLOR"),new Color(255,255,255));
- strText = new String(getParameter("TEXT"));
- fadeSteps = IEExtend.strInt(getParameter("FADESTEPS"),5);
- clrFade = new Color[fadeSteps];
-
- int ptSize = IEExtend.strInt(getParameter("PTSIZE"),bounds().height);
- if(getParameter("FONT") == null)
- strFont = new Font("Courier",Font.PLAIN,ptSize);
- else
- strFont = new Font(getParameter("FONT"),Font.PLAIN,ptSize);
-
- CalcFade();
-
- os = createImage(bounds().width, bounds().height);
- }
-
- private void CalcFade()
- {
- int lr = clrBack.getRed();
- int lg = clrBack.getGreen();
- int lb = clrBack.getBlue();
- int hr = clrText.getRed();
- int hg = clrText.getGreen();
- int hb = clrText.getBlue();
- int rStep = (hr - lr) / (fadeSteps+2);
- int gStep = (hg - lg) / (fadeSteps+2);
- int bStep = (hb - lb) / (fadeSteps+2);
-
- for(int i=0;i<fadeSteps;i++)
- {
- clrFade[i] = new Color(lr + (i+1)*rStep, lg + (i+1)*gStep, lb + (i+1)*bStep);
- }
- }
-
- public String getAppletInfo()
- {
- return "HorizontalScroller for the Mortar Package, by Big Picture Multimedia";
- }
-
- public void start()
- {
- scroll = new Thread(this);
- scroll.start();
- }
-
- public void stop()
- {
- scroll.stop();
- }
-
- public void run()
- {
- while(true)
- {
- try
- {
- Thread.currentThread().sleep(timeout);
- }
- catch(Exception e)
- {
- return;
- }
- Graphics g = this.getGraphics();
- moveText(g);
- g.dispose();
- g.finalize();
- }
- }
-
- private void moveText(Graphics h)
- {
- Graphics g = os.getGraphics();
-
- String newStr = strText.substring(1) + strText.charAt(0);
- g.setFont(strFont);
- if(fm == null)
- {
- fm = g.getFontMetrics();
- calcData();
- }
- int height = bounds().height - fm.getDescent();
-
- g.setColor(clrBack);
- g.fillRect(0,0,this.bounds().width,bounds().height);
- drawNewString(g,height,newStr);
-
- strText = newStr;
-
- g.dispose();
- h.drawImage(os, 0, 0, this );
- }
-
- private void drawNewString(Graphics g, int height, String newStr)
- {
- //now, some more complex drawing
- int xAt = 0;
- for(int i=0;i<fadeSteps;i++)
- {
- g.setColor(clrFade[i]);
- g.drawString(newStr.substring(i,i+1), i*cWidth, height);
- }
- g.setColor(clrText);
- g.drawString(newStr.substring(fadeSteps,tWidth-fadeSteps),
- fadeSteps*cWidth,height);
- for(int i=0;i<fadeSteps;i++)
- {
- g.setColor(clrFade[fadeSteps-i-1]);
- g.drawString(newStr.substring(tWidth-fadeSteps+i,
- tWidth-fadeSteps+1+i), (tWidth-fadeSteps+i)*cWidth,height);
- }
- }
-
- public void paint(Graphics g)
- {
- g.drawImage(os, 0, 0, this );
- }
-
- private void calcData()
- {
- Rectangle r = bounds();
- tWidth = r.width / fm.charWidth('A'); //all widths the same
- cWidth = fm.charWidth('A');
- }
- }
-
-
-
-