home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-10-04 | 8.3 KB | 261 lines |
- import java.awt.*;
- import java.applet.*;
- import java.util.*;
- import java.net.*;
-
-
- /***************************************************************************
- Applet: VerticalScroller
- Author: Imagination's End, mods by Jeff Rhyason
- Desc: Doesn't the name basically state it all?
-
- Date Created: 19960422
- Date Modified: 19961005
- ***************************************************************************/
-
- /*
- VerticalScroller
-
- Overview
-
-
- The first 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
- upward 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
- PTSIZE: the default font size (20)
- STEPS: number of pixels to jump upward in each movement (3)
- NUMLINES: the number of lines of text you want scrolled
- FONT: the default font to use (Helv)
-
- Per-Line Parameters
-
- Per-Line parameters involve a prefix, and have a line-number
- (0-based) appended.
-
- STRING0: text to display on line 0 (1st line) (nothing)
- COLOR0: color that text will be written in (RRGGBB in hex)
- BACKCOLOR0: background text will be written over (same)
- LOCALURL0: a local URL to visit when the line is clicked
- REMOTEURL0: a remote URL to visit
-
- There is no difference between the LOCALURL and the REMOTEURL;
- if the LOCALURL is entered, it's called first. If no LOCALURL
- exists, but a REMOTEURL does, it will be called instead.
-
- 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=VerticalScroller.class WIDTH=200 HEIGHT=100>
- <PARAM NAME=FONT VALUE=COURIER>
- <PARAM NAME=PTSIZE VALUE=20>
- <PARAM NAME=NUMLINES VALUE=2>
-
- <PARAM NAME=STRING0 VALUE="Hello there!">
- <PARAM NAME=COLOR0 VALUE=0077FF>
- <PARAM NAME=BACKCOLOR0 VALUE=AA0000>
- <PARAM NAME=REMOTEURL0 VALUE=http://disemia.com/>
-
- <PARAM NAME=STRING1 VALUE="How are you?">
- <PARAM NAME=COLOR1 VALUE=00DD00>
- </APPLET>
- */
-
-
- public class VerticalScroller extends Applet implements Runnable
- {
- Thread scroll;
- int timeout, stepSize, topLine, numLines, ptSize, linesPerScreen, offset;
- Color clrBack, clrText;
- String strs[];
- URL localURLS[];
- URL remoteURLS[];
- Color strClrs[],backClrs[];
- int strWidths[];
- FontMetrics fm;
- Font strFont;
- Image os;
- Rectangle r;
- Graphics osg;
-
- public void init()
- {
- fm = null;
- timeout = IEExtend.strInt(getParameter("TIMEOUT"),100);
- clrBack = IEExtend.strColor(getParameter("BACKCOLOR"),new Color(0,0,0));
- clrText = IEExtend.strColor(getParameter("TEXTCOLOR"),new Color(255,255,255));
- ptSize = IEExtend.strInt(getParameter("PTSIZE"), 20);
- stepSize = IEExtend.strInt(getParameter("STEPSIZE"), ptSize/7);
- numLines = IEExtend.strInt(getParameter("NUMLINES"),0);
- strs = new String[numLines];
- strClrs = new Color[numLines];
- backClrs = new Color[numLines];
- localURLS = new URL[numLines];
- remoteURLS = new URL[numLines];
- if(getParameter("FONT") == null)
- strFont = new Font("Helv",Font.BOLD,ptSize);
- else
- strFont = new Font(getParameter("FONT"),Font.PLAIN,ptSize);
-
- r = this.bounds();
- linesPerScreen = (r.height / ptSize) + 2;
- String temp;
- for(int i=0;i<numLines;i++)
- {
- try
- {
- strs[i] = getParameter("STRING" + i);
- strClrs[i] = IEExtend.strColor(getParameter("COLOR"+i),clrText);
- backClrs[i] = IEExtend.strColor(getParameter("BACKCOLOR"+i),null);
- if(getParameter("LOCALURL"+i)!= null && getParameter("LOCALURL"+i).length() > 0)
- localURLS[i] = new URL(getDocumentBase(),getParameter("LOCALURL"+i));
- if(getParameter("REMOTEURL"+i)!=null && getParameter("REMOTEURL"+i).length() > 0)
- remoteURLS[i] = new URL(getParameter("REMOTEURL"+i));
- }
- catch(Exception e)
- {
- System.out.println("Malformed URL probably!");
- System.err.println("Check your line-specific parameters for line " + i );
- }
- }
-
- os = createImage(r.width, r.height);
- osg = os.getGraphics();
-
- topLine = -linesPerScreen;
- offset = 0;
- }
-
- public String getAppletInfo()
- {
- return "VerticalScroller 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()
- {
- Graphics g = this.getGraphics();
- while(true)
- {
- try
- {
- Thread.currentThread().sleep(timeout);
- }
- catch(Exception e)
- {
- return;
- }
- moveText(g);
- }
- }
-
- public void paint(Graphics g)
- {
- g.drawImage(os,0,0,this);
- }
-
- private void moveText(Graphics g)
- {
- osg.setColor(clrBack);
- osg.fillRect(0,0,r.width,r.height);
- osg.setFont(strFont);
- if(fm == null)
- {
- fm = osg.getFontMetrics();
- strWidths = new int[numLines];
- for(int i=0;i<numLines;i++)
- strWidths[i] = r.width/2 - fm.stringWidth(strs[i])/2;
- }
-
- for(int i=0;i<linesPerScreen;i++)
- {
- if(!(topLine + i >= numLines) && (topLine+i >= 0))
- {
- if(backClrs[topLine+i] != null)
- {
- osg.setColor(backClrs[topLine+i]);
- osg.fillRect(0,(i-1)*ptSize-offset+fm.getMaxDescent(),r.width,ptSize);
- }
- osg.setColor(strClrs[topLine+i]);
- osg.drawString(strs[topLine+i], strWidths[topLine+i], i*ptSize - offset);
- }
- }
- offset += stepSize;
- if(offset >= ptSize)
- {
- offset -= ptSize;
- topLine++;
- if(topLine > numLines)
- {
- topLine = -linesPerScreen;
- }
- }
- g.drawImage(os,0,0,this);
- }
-
- public boolean mouseDown(Event evt, int x, int y)
- {
- // the y is the only significant thing
- if(y < 0 || y > r.height)
- return true;
-
- int line = (y+offset+ptSize-fm.getMaxDescent()) / ptSize + topLine;
- if(line < 0 || line >= numLines)
- return true;
-
- if(localURLS[line] != null)
- {
- System.out.println(localURLS[line].toString());
- getAppletContext().showDocument(localURLS[line]);
- }
- else if(remoteURLS[line] != null)
- {
- System.out.println(remoteURLS[line].toString());
- getAppletContext().showDocument(remoteURLS[line]);
- }
- return true;
- }
- }
-
-
-
-