//****************************************************************************** // verticalScroll_andyhosk.java: Applet // Written by R.A. Hoskinson....All Rights Reserved //****************************************************************************** import java.applet.*; import java.awt.*; import java.net.*; import destination; import java.util.*; //============================================================================== // Main Class for applet verticalScroll_andyhosk // //============================================================================== public class verticalScroll_andyhosk extends Applet implements Runnable { // THREAD SUPPORT: // m_verticalScroll is the Thread object for the applet //-------------------------------------------------------------------------- private Thread m_verticalScroll = null; // PARAMETER SUPPORT: // Parameters allow an HTML author to pass information to the applet; // the HTML author specifies them using the tag within the // tag. The following variables are used to store the values of the // parameters. //-------------------------------------------------------------------------- // Members for applet parameters // = //-------------------------------------------------------------------------- private String m_fontname = "Verdana"; private String m_text = "Hoskinson\'s Vertical Scroll Applet^"; private String m_fontstyle = "bold"; private int m_fontsize = 10; private String m_bgcolor = "white"; private String m_fgcolor = "black"; private int m_timeinterval = 10; private String m_URL; private String m_target = "_top"; private int m_linespacing = 5; private Image textCanvas; private Image inTextCanvas; private Image outTextCanvas; private Font theFont; private String[] linesOfText; private destination myBookmark; private String m_xcoord = "center"; private int m_ycoord; private boolean firstTime = true; private String m_buildNumber = "970824"; private String m_linkcolor; // Parameter names. To change a name of a parameter, you need only make // a single change. Simply modify the value of the parameter string below. //-------------------------------------------------------------------------- private final String PARAM_fontname = "fontname"; private final String PARAM_text = "text"; private final String PARAM_fontstyle = "fontstyle"; private final String PARAM_fontsize = "fontsize"; private final String PARAM_bgcolor = "bgcolor"; private final String PARAM_fgcolor = "fgcolor"; private final String PARAM_timeinterval = "timeinterval"; private final String PARAM_URL = "URL"; private final String PARAM_target = "target"; private final String PARAM_linespacing = "linespacing"; private final String PARAM_xcoord = "xcoord"; private final String PARAM_linkcolor = "linkcolor"; // verticalScroll_andyhosk Class Constructor //-------------------------------------------------------------------------- public verticalScroll_andyhosk() { // TODO: Add constructor code here } // APPLET INFO SUPPORT: // The getAppletInfo() method returns a string describing the applet's // author, copyright date, or miscellaneous information. //-------------------------------------------------------------------------- public String getAppletInfo() { //return "Vertical Scroll Applet Build " + m_buildNumber + ".....Written by R.A. Hoskinson\r\n\r\nThis is an UNREGISTERED SHAREWARE version of this applet.\r\nUsers may evaluate this applet for a 30 day period,\r\nafter which they must send a 25 dollar registration fee to R.A. Hoskinson.\r\n\r\nFor more information, contact andyhosk@aol.com.\r\n"; return "Vertical Scroll Applet Build " + m_buildNumber + ".....Written by R.A. Hoskinson\r\n\r\nThis is a REGISTERED version of this applet.\r\nPlease report bugs to andyhosk@aol.com.\r\n"; } // PARAMETER SUPPORT // The getParameterInfo() method returns an array of strings describing // the parameters understood by this applet. // // verticalScroll_andyhosk Parameter Information: // { "Name", "Type", "Description" }, //-------------------------------------------------------------------------- public String[][] getParameterInfo() { String[][] info = { { PARAM_fontname, "String", "font name" }, { PARAM_text, "String", "The displayed text (^ equals CR/LF)" }, { PARAM_fontstyle, "String", "bold, plain, or italic" }, { PARAM_fontsize, "int", "point size of the font" }, { PARAM_bgcolor, "String", "background color" }, { PARAM_fgcolor, "String", "foreground color" }, { PARAM_linkcolor, "String", "link color" }, { PARAM_timeinterval, "int", "scrolling speed" }, { PARAM_URL, "String", "hyperlink" }, { PARAM_target, "String", "hyperlink's target frame" }, { PARAM_linespacing, "int", "number of pixels between lines" }, { PARAM_xcoord, "String", "x coordinates of menu items" }, }; return info; } // The init() method is called by the AWT when an applet is first loaded or // reloaded. Override this method to perform whatever initialization your // applet needs, such as initializing data structures, loading images or // fonts, creating frame windows, setting the layout manager, or adding UI // components. //-------------------------------------------------------------------------- public void init() { // PARAMETER SUPPORT // The following code retrieves the value of each parameter // specified with the tag and stores it in a member // variable. //---------------------------------------------------------------------- String param; // fontname: font name //---------------------------------------------------------------------- param = getParameter(PARAM_fontname); if (param != null) m_fontname = param; // text: The displayed text (^ equals CR/LF) //---------------------------------------------------------------------- param = getParameter(PARAM_text); if (param != null) m_text = param; // fontstyle: bold, plain, or italic //---------------------------------------------------------------------- param = getParameter(PARAM_fontstyle); if (param != null) m_fontstyle = param; // fontsize: point size of the font //---------------------------------------------------------------------- param = getParameter(PARAM_fontsize); if (param != null) m_fontsize = Integer.parseInt(param); // bgcolor: background color //---------------------------------------------------------------------- param = getParameter(PARAM_bgcolor); if (param != null) m_bgcolor = param; // fgcolor: foreground color //---------------------------------------------------------------------- param = getParameter(PARAM_fgcolor); if (param != null) m_fgcolor = param; m_linkcolor = getParameter(PARAM_linkcolor); if (m_linkcolor == null) m_linkcolor = m_fgcolor; // timeinterval: scrolling speed //---------------------------------------------------------------------- param = getParameter(PARAM_timeinterval); if (param != null) m_timeinterval = Integer.parseInt(param); // URL: hyperlink //---------------------------------------------------------------------- param = getParameter(PARAM_URL); if (param != null) m_URL = param; // target: hyperlink's target frame //---------------------------------------------------------------------- param = getParameter(PARAM_target); if (param != null) m_target = param; // linespacing: number of pixels between lines //---------------------------------------------------------------------- param = getParameter(PARAM_linespacing); if (param != null) m_linespacing = Integer.parseInt(param); param = getParameter(PARAM_xcoord); if (param != null) m_xcoord = param; System.out.println(getAppletInfo()); // Parse text parameter into an array of strings linesOfText = readTokens(m_text); setBackground(setCustomColor(m_bgcolor)); setForeground(setCustomColor(m_fgcolor)); //construct the font if (m_fontstyle.equalsIgnoreCase("bold")) theFont = new Font(m_fontname, Font.BOLD, m_fontsize); if (m_fontstyle.equalsIgnoreCase("italic")) theFont = new Font(m_fontname, Font.ITALIC, m_fontsize); if (m_fontstyle.equalsIgnoreCase("plain")) theFont = new Font(m_fontname, Font.PLAIN, m_fontsize); myBookmark = new destination("verticalScroll applet hyperlink", getDocumentBase(), m_URL, m_target); outTextCanvas = createTextCanvas(linesOfText, theFont, m_linespacing); setForeground(setCustomColor(m_linkcolor)); inTextCanvas = createTextCanvas(linesOfText, theFont, m_linespacing); textCanvas = outTextCanvas; } // Place additional applet clean up code here. destroy() is called when // when you applet is terminating and being unloaded. //------------------------------------------------------------------------- public void destroy() { } // verticalScroll_andyhosk Paint Handler //-------------------------------------------------------------------------- public void paint(Graphics g) { if (firstTime) { Rectangle r = g.getClipRect(); g.clearRect(r.x, r.y, r.width, r.height); firstTime = false; } else { g.drawImage(textCanvas, 0, m_ycoord, this); } } public void update(Graphics g) { //Override the update method paint(g); } // The start() method is called when the page containing the applet // first appears on the screen. The AppletWizard's initial implementation // of this method starts execution of the applet's thread. //-------------------------------------------------------------------------- public void start() { if (m_verticalScroll == null) { m_verticalScroll = new Thread(this); m_verticalScroll.start(); } // TODO: Place additional applet start code here } // The stop() method is called when the page containing the applet is // no longer on the screen. The AppletWizard's initial implementation of // this method stops execution of the applet's thread. //-------------------------------------------------------------------------- public void stop() { if (m_verticalScroll != null) { m_verticalScroll.stop(); m_verticalScroll = null; } // TODO: Place additional applet stop code here } // THREAD SUPPORT // The run() method is called when the applet's thread is started. If // your applet performs any ongoing activities without waiting for user // input, the code for implementing that behavior typically goes here. For // example, for an applet that performs animation, the run() method controls // the display of images. //-------------------------------------------------------------------------- public void run() { firstTime = true; while (true) { for (int i=0; i < textCanvas.getHeight(this); i++) { m_ycoord = size().height-i; repaint(); try { Thread.sleep(m_timeinterval); } catch (InterruptedException e) { //firstTime = true; //repaint(); } } } } // MOUSE SUPPORT: // The mouseDown() method is called if the mouse button is pressed // while the mouse cursor is over the applet's portion of the screen. //-------------------------------------------------------------------------- public boolean mouseDown(Event evt, int x, int y) { try { getAppletContext().showDocument(myBookmark.url, myBookmark.target); }catch (NullPointerException npe) {} return true; } // MOUSE SUPPORT: // The mouseEnter() method is called if the mouse cursor enters the // applet's portion of the screen. //-------------------------------------------------------------------------- public boolean mouseEnter(Event evt, int x, int y) { textCanvas = inTextCanvas; repaint(); try { showStatus(myBookmark.url.toString()); }catch (NullPointerException npe) { //showStatus("Hoskinson's Vertical Scroll Applet Build " + m_buildNumber + "...UNREGISTERED SHAREWARE version"); showStatus(" "); } return true; } // MOUSE SUPPORT: // The mouseExit() method is called if the mouse cursor leaves the // applet's portion of the screen. //-------------------------------------------------------------------------- public boolean mouseExit(Event evt, int x, int y) { //showStatus("Hoskinson's Vertical Scroll Applet Build " + m_buildNumber + "...UNREGISTERED SHAREWARE version"); textCanvas = outTextCanvas; repaint(); showStatus(" "); return true; } public String[] readTokens(String newText) { StringTokenizer st = new StringTokenizer(newText,"^"); int numTokens=st.countTokens(); String[] myBookmarks = new String[numTokens]; for (int i=0; i < numTokens; i++) { myBookmarks[i] = st.nextToken(); } return myBookmarks; } public Color setCustomColor(String mycolor) { if (mycolor.equalsIgnoreCase("white")) return Color.white; else if (mycolor.equalsIgnoreCase("black")) return Color.black; else if (mycolor.equalsIgnoreCase("red")) return Color.red; else if (mycolor.equalsIgnoreCase("green")) return Color.green; else if (mycolor.equalsIgnoreCase("blue")) return Color.blue; else if (mycolor.equalsIgnoreCase("yellow")) return Color.yellow; else if (mycolor.equalsIgnoreCase("cyan")) return Color.cyan; else if (mycolor.equalsIgnoreCase("darkGray")) return Color.darkGray; else if (mycolor.equalsIgnoreCase("gray")) return Color.gray; else if (mycolor.equalsIgnoreCase("lightGray")) return Color.lightGray; else if (mycolor.equalsIgnoreCase("magenta")) return Color.magenta; else if (mycolor.equalsIgnoreCase("orange")) return Color.orange; else if (mycolor.equalsIgnoreCase("pink")) return Color.pink; else { Color customfgcolor = new Color(Integer.parseInt(mycolor)); return customfgcolor; } } public int computeImageHeight(Font myFont, int vspace, int numlines) { FontMetrics bufferfm = getFontMetrics(theFont); int tempnum = (getMaxFontHeight(bufferfm) + vspace) * (numlines + 1); tempnum = tempnum + size().height; return tempnum; } public int computeXPosition(Font myFont, String pos, String textString) { FontMetrics fm = getFontMetrics(myFont); int xposition; if (pos.equalsIgnoreCase("center")) xposition = (size().width - fm.stringWidth(textString)) / 2; else xposition = Integer.parseInt(pos); return xposition; } public Image createTextCanvas(String[] textarray, Font myFont, int vspace) { Image offscreenImage; Graphics offscreenGraphics; offscreenImage = createImage(size().width, computeImageHeight(myFont, vspace, textarray.length)); offscreenGraphics = offscreenImage.getGraphics(); offscreenGraphics.setFont(myFont); offscreenGraphics.setColor(getBackground()); offscreenGraphics.fillRect(0,0,size().width,computeImageHeight(myFont, vspace, textarray.length)); // Now make the applet foreground color the current offscreen graphic color (in prep for painting the String) offscreenGraphics.setColor(getForeground()); // draw the string on the offscreen graphic int currentyposition = getMaxFontHeight(getFontMetrics(myFont)); for (int i=0; i < textarray.length; i++) { offscreenGraphics.drawString(textarray[i],computeXPosition(myFont, m_xcoord, textarray[i]),currentyposition); currentyposition = currentyposition + vspace + getMaxFontHeight(getFontMetrics(myFont)); } return offscreenImage; } public int getMaxFontHeight(FontMetrics fm) { return fm.getLeading() + fm.getMaxAscent() + fm.getMaxDescent(); } }