/* * Copyright (c) 1998, 1999 Kevan Stannard. All Rights Reserved. * * Permission to use, copy, modify, and distribute this software * and its documentation for NON-COMMERCIAL or COMMERCIAL purposes and * without fee is hereby granted. * * Please note that this software comes with * NO WARRANTY * * BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED * BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES * PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS * TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME * THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. * * IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER * PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, * INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE * THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED * BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER * OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. */ import java.applet.*; import java.awt.*; import java.net.*; import java.io.*; public class JZOOFadingText extends Applet implements Runnable { // thread data Thread thread = null; // double buffer Image db; Graphics dbGraphics; // applet int appletWidth = 0; int appletHeight = 0; // other String text; String textPart; Font font; FontMetrics fm; int textWidth = 0; int textAscent = 0; int textDescent = 0; String fontName = null; int fontSize = -1; int xPos = 0; int yPos = 0; int speed = 0; String messageFile; Color bgColor; Color textColor; Image bgImage; String bgImageName; int bgImageX; int bgImageY; // glow related variables float increment = 20; float incrementCount = 0; final int IN = 1; final int OUT = 2; int fade = IN; float textR, textG, textB; float bgR, bgG, bgB; float deltaR, deltaG, deltaB; float red, green, blue; boolean nextWord = true; int textIndex = 0; // status final int OK = 0; final int LOADING_IMAGES = 1; int status = 0; // seperator char public static final char NEW_LINE = 0; public String getAppletInfo() { StringBuffer info = new StringBuffer(); info.append("--------------------\r\n"); info.append("JZOOFadingText\r\n"); info.append("Version 1.3\r\n"); info.append("www.jzoo.com\r\n"); info.append("--------------------\r\n"); return info.toString(); } public void getParameters() { String param = null; // get applet width and height appletWidth = this.size().width; appletHeight = this.size().height; // get message parameter messageFile = getParameter("messagefile"); System.out.println(" messagefile=" + messageFile); // get speed parameter param = getParameter("speed"); try { increment = Integer.parseInt(param); } catch (Exception e) { increment = 20; } System.out.println(" speed=" + increment); // get background colour parameter param = getParameter("bgcolor"); int bgColorValue = 0; try { bgColorValue = Integer.parseInt(param, 16); bgColor = new Color(bgColorValue); } catch (Exception e) { int c = (int) (Math.random() * 255); bgColor = new Color(c,c,c); } System.out.println(" bgcolor=" + bgColor); // get text colour param = getParameter("textcolor"); int textColorValue = 0; try { textColorValue = Integer.parseInt(param, 16); textColor = new Color(textColorValue); } catch (Exception e) { int c = (int) (Math.random() * 255); textColor = new Color(c,c,c); } System.out.println(" textcolor=" + textColor); // get background image bgImageName = getParameter("bgimage"); System.out.println(" bgimage=" + bgImageName); // get font name fontName = getParameter("fontname"); System.out.println(" fontname=" + fontName); // get font size try { String fontSizeString = getParameter("fontsize"); if (fontSizeString != null) { fontSize = Integer.parseInt(fontSizeString); } } catch (Exception e) { fontSize = -1; } System.out.println(" fontsize=" + fontSize); } public void makeBuffer() { // allocate space for double buffer db = createImage(appletWidth, appletHeight); dbGraphics = db.getGraphics(); } public void init() { System.out.println(getAppletInfo()); getParameters(); makeBuffer(); } private String fetchMessageString() { StringBuffer buf = new StringBuffer(); URL url = null; boolean foundEOL = false; try { url = new URL(getCodeBase() + messageFile); DataInputStream in = new DataInputStream(new BufferedInputStream(url.openStream())); while (true) { int c = in.read(); if (c==-1) { break; } switch (c) { case '\n': case '\r': { if (foundEOL) { foundEOL = false; } else { foundEOL = true; buf.append((char) NEW_LINE); } break; } default: { foundEOL = false; buf.append((char) c); } } } char lastChar = buf.charAt( buf.length()-1 ); if (lastChar != NEW_LINE) { buf.append((char) NEW_LINE); } } catch (MalformedURLException e) { showStatus("Invalid URL: " + url); } catch (IOException e) { showStatus("Error " + e); } return buf.toString(); } private String fetchMessageStringOLD() { String messageString = new String(); // construct URL of message file String urlString = getCodeBase() + messageFile; // fetch the message file try { URL url = new URL(urlString); InputStream inStream = url.openStream(); DataInputStream dataStream = new DataInputStream(new BufferedInputStream(inStream)); String inLine = null; while ((inLine = dataStream.readLine()) != null) { messageString += inLine; messageString += (char) 0; //'\n'; } } catch (MalformedURLException e) { showStatus("Invalid URL: " + urlString); } catch (IOException e) { showStatus("Error " + e); } return messageString; } public void start() { if (thread == null) { thread = new Thread(this); thread.start(); } } public void stop() { if (thread != null) { thread.stop(); thread = null; } } public void update(Graphics g) { paint(dbGraphics); g.drawImage(db,0,0,null); } public void paint(Graphics g) { clearScreen(g); updateDisplay(g); } private void clearScreen(Graphics g) { g.setColor(bgColor); g.fillRect(0,0,appletWidth,appletHeight); } private void updateDisplay(Graphics g) { if (status == OK) { g.setFont(font); if (bgImageName != null) { g.drawImage (bgImage,bgImageX,bgImageY,null); } // draw the text if (textPart != null) { g.setColor(new Color((int)red, (int)green, (int)blue)); g.drawString (textPart, xPos, yPos); } } else { if (status == LOADING_IMAGES) { g.setColor(textColor); g.drawString ("Loading Images",20,20); } } } public void run() { status = OK; // load images MediaTracker tracker = new MediaTracker(this); if (bgImageName != null) { bgImage = getImage(getCodeBase(), bgImageName); tracker.addImage(bgImage,0); status = LOADING_IMAGES; } // get the message string text = fetchMessageString(); // initialise the font if (fontName == null || fontSize == -1) { font = new Font("Times New Roman", Font.BOLD, 24); } else { font = new Font(fontName, Font.BOLD, fontSize); } fm = getFontMetrics(font); textR = textColor.getRed(); textG = textColor.getGreen(); textB = textColor.getBlue(); bgR = bgColor.getRed(); bgG = bgColor.getGreen(); bgB = bgColor.getBlue(); red = bgR; green = bgG; blue = bgB; deltaR = (textR - bgR) / increment; deltaG = (textG - bgG) / increment; deltaB = (textB - bgB) / increment; fade = IN; while (true) { if (status == LOADING_IMAGES) { if (tracker.checkAll(true)) { status = OK; bgImageX = (appletWidth - bgImage.getWidth(this))/2; bgImageY = (appletHeight - bgImage.getHeight(this))/2; } } else { if (status == OK) { // update variables if (fade==IN) { if (incrementCount == increment) { fade = OUT; incrementCount = 0; } else { red += deltaR; green += deltaG; blue += deltaB; } } else { if (incrementCount == increment) { fade = IN; incrementCount = 0; nextWord = true; } else { red -= deltaR; green -= deltaG; blue -= deltaB; } } if (red<0) red=0; if (green<0) green=0; if (blue<0) blue=0; if (red>255) red=255; if (green>255) green=255; if (blue>255) blue=255; incrementCount++; if (nextWord) { if (textIndex >= text.length()) { textIndex = 0; } int nextIndex = text.indexOf(NEW_LINE,textIndex); //System.out.println("length: " + text.length() + " next: " + nextIndex + " text: " + textIndex); textPart = text.substring(textIndex, nextIndex); textIndex = nextIndex + 1; nextWord = false; } textWidth = fm.stringWidth(textPart); textAscent = fm.getAscent(); textDescent = fm.getDescent(); xPos = (appletWidth - textWidth) / 2; yPos = textAscent + (appletHeight - (textAscent + textDescent)) / 2; } repaint(); try { thread.sleep(50); } catch (InterruptedException e) { } } } } }