// no package, stand alone Applet /* * (C) ELRIDEV SOFTWARE, 1999. ALL RIGHTS RESERVED. * THIS SOFTWARE IS THE PROPERTY OF ELRIDEV SOFTWARE, YOU CAN USE THIS SOFTWARE * ON YOUR WEBPAGE (OR USE THE SOURCE FOR LEARNING JAVA), IT PROVIDED AS AN EXAMPLE, * WITH NO WARRANTIES WHATSOEVER, SHOULD ANY DAMAGE OCCUR FROM THE USE OF THIS * PRODUCT ELRIDEV WILL BE HELD IN NO WAY LIABLE FOR DAMAGES. * YOU CAN ALSO MODIFY THIS CODE FOR YOU OWN PURPOSES, HOWEVER ONCE THIS IS DONE * YOU MUST INDICATE THAT THESE CHANGES HAVE BEEN MADE, SHOULD YOU WISH IT REMAIN * FREELY AVAILABLE. */ import java.applet.*; import java.awt.*; import java.awt.image.*; import java.net.*; import java.io.*; /** * Scrolling Applet, you can put a top scroll object directly onto a web * page, its a standalone object. It will scroll text from right to left * and add a distortion effect to the page. The speed is adjustable from * 1 very slow thru 10 very fast (4 is default) using the SPEED parameter in the HTML file. * Set the Text filename and Image Filename parameters from your HTML page. * The Text parmeter is TEXTFILE and should be set to the file name relative to * the html file's path. The Image parameter is IMAGENAME and should be a .gif * file of 256 colours or less, again this is relative to the html file. The * font must be fixed width and height. If the font width and height are not 20 pixels per * letter, you must set these too, the parameters are FONTWIDTH and FONTHEIGHT. * ALWAYS make the width divisible by the font width, and the height is * twice the size of the letters. * * @author Dave Cherry * @version Thor */ public class TopScroll extends Applet implements Runnable { // the thread class of this instance. Thread m_TopScroll = null; // the maximum number of letters in the font private final int finMaxLetters = 50; // the default scroll text name private final String finDefImgName = "scrollfont.gif"; // the maximum size of the text file containing scroller. private final int fiMaxScrollTextBuffer = 10000; // scroll sizing specific data private String m_strLetters; private int m_iSmoothScroll = 0; private int m_iNextCharPos = 0; private int m_iTotalLen; private int m_iTotalWidth; private int m_iLetterWidth = 20, m_iLetterHeight = 20, m_iLetterSize; private int m_iDistortX, m_iDistortY, m_iDistortSize; // the font image & bits. private IndexColorModel m_palColour; private byte[][] m_byPixels; // the actual double buffered screens, letter storage and distortion store private Image m_imgBuffer; private byte[] m_byScroller; private int[] m_distortion; private int[] imgData; private int[] destData; private Image totalBuffer; private Graphics gfxBuffer; // these are the scroll factors private int posscroller = 400; private int posscrolleradj=10; private int smoothadjust=4; /** * TopScroll constructor, empty, actually uses start for thread creation. */ public TopScroll() { } /** * Returns the applet information * @return Applet information string */ public String getAppletInfo() { return "Name: TopScroll V1.0\r\n" + "Author: ElriDEV Software"; } protected String readTextFile(String name) throws Exception { if(name==null || name.length()==0) return ""; StringBuffer strOutput = new StringBuffer(fiMaxScrollTextBuffer); URL url = new URL(getDocumentBase(),name); URLConnection urlCon = url.openConnection(); InputStreamReader file = new InputStreamReader(new BufferedInputStream(urlCon.getInputStream())); int i=0; while(file.ready()&&i=m_iTotalWidth-m_iDistortX) posscrolleradj=-posscrolleradj; } catch(InterruptedException e) { // something odd happened here!! showStatus("Couldnt draw the scroll text!"); } } /** * create a new thread for our Applet to run in */ public void start() { if (m_TopScroll == null) { m_TopScroll = new Thread(this); m_TopScroll.start(); } } /** * stop our applets thread, its done! */ public void stop() { if (m_TopScroll != null) { m_TopScroll.stop(); m_TopScroll = null; } } /** * just stays in a loop moving the scroller to its new location, then doing * a repaint and waiting 1/60th of a second before repeating. */ public void run() { while (true) { try { scrollit(); repaint(); // wait 1/40th of a second. Thread.sleep(25); } catch (InterruptedException e) { stop(); } } } /** * Given a character value, this function will convert this value into an * index position into the image file containing the chars * @param letter the letter to be converted * @return the index into the image file. */ protected int GetNextCharIndex(char letter) { if(letter>='A'&&letter<='Z') return letter-'A'; else if(letter>='0'&&letter<='9') return letter-17; else if(letter=='.') return 26; else if(letter==',') return 27; else if(letter=='\'') return 28; // quotation mark else if(letter=='`') return 29; // quote close. else if(letter=='{') return 30; // smiley face. else if(letter=='!') return 41; else if(letter=='-') return 42; else if(letter=='+') return 43; else if(letter=='?') return 44; else if(letter=='}') return 45; // (c) else if(letter=='@') return 46; // @ else if(letter==':') return 47; // : else if(letter=='/') return 48; // / else if(letter=='\\') return 49; // \ else return -1; // -1==space in string. } /** * move the scroller one position, if the scroller has moved by one * full letter position, then a new letter is added and the smooth * scroll offset is reset. */ protected void StepScroll() { if(m_iSmoothScroll>=m_iLetterWidth) // at the end of a scroll. { m_iSmoothScroll=0; int letter = GetNextCharIndex(m_strLetters.charAt(m_iNextCharPos++)); if(m_iNextCharPos>=m_iTotalLen) m_iNextCharPos=0; // coarse scroll 1 letter. Graphics g = m_imgBuffer.getGraphics(); g.copyArea(m_iLetterWidth,0,m_iTotalWidth-m_iLetterWidth,m_iLetterHeight*2,-m_iLetterWidth,0); g.setColor(Color.white); g.fillRect(m_iTotalWidth-m_iLetterWidth,0, m_iLetterWidth,m_iLetterHeight*2); if(letter!=-1) { Image imgFontSource = createImage(new MemoryImageSource(m_iLetterWidth, m_iLetterHeight, m_palColour, m_byPixels[letter], 0, m_iLetterWidth)); g.drawImage(imgFontSource,m_iTotalWidth-m_iLetterWidth,m_iLetterHeight/2,Color.white,this); } } else // need to scroll again. { m_iSmoothScroll+=smoothadjust; } } /** * converts an image containing the letters into a 256 colour version, thats split * up by letter rather than by line. * @param img the image file to be converted * @param byPixels the pixel array where each image will be stored * @param byReds the color index for reds * @param byGreens the color index for greens * @param byBlues the color index for blues */ protected void getIndexColourData(Image img, byte[][] byPixels, byte[] byReds, byte[] byGreens, byte[] byBlues) { int nWidth = img.getWidth(this); int nHeight = img.getHeight(this); int nPaletteIndex = 0; int[] nPixels = new int[nWidth * nHeight]; int[] nPalette = new int[256]; // get the pixels into memory. PixelGrabber pg = new PixelGrabber(img, 0, 0, nWidth, nHeight, nPixels, 0, nWidth); try { pg.grabPixels(); } catch (InterruptedException e) { } // Loop through each pixel in the image... for (int letter = 0; letter < finMaxLetters; letter++) { int strpos=0; for (int y=0;ym_iLetterSize) System.out.println("letter:"+letter+" index error"); m_byPixels[letter][strpos++] = (byte) nIndex; } } } for (int i = 0; i < 256; i++) { int r = (nPalette[i] >> 16) & 0xff; int g = (nPalette[i] >> 8 ) & 0xff; int b = (nPalette[i] ) & 0xff; byReds[i] = (byte)r; byGreens[i] = (byte)g; byBlues[i] = (byte)b; } } /** * generates the distortion effect that is rolled across the scrolling * text, this is calculated once at the start, and held in memory. * the distortion is stored as an array of offsets into a memory * representation of an image. */ protected void generateDistortion() { // now correct them to the screen int yCenter=m_iDistortY/2; double multiplier = 360. / (double)m_iDistortX; int distsize = m_iDistortY; for(int x=0;x=1.) startpos = ((double)(m_iLetterHeight/4.)*scale)-2.; else startpos = ((double)20-(10*lineskip))/2.; for(int y=0;y39.) m_distortion[x+(y*m_iDistortX)]=0; else m_distortion[x+(y*m_iDistortX)]=x+((int)startpos*m_iDistortX); startpos+=lineskip; } } } }