import java.awt.Graphics; import java.awt.Color; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Image; import java.awt.Event; import java.net.URL; import java.net.MalformedURLException; import java.io.*; public class scroller extends java.applet.Applet implements Runnable { //***************// // declarations: // //***************// Image OffScreenImage; // reduce flicker Graphics OffScreen; // reduce flicker Color BackColor; // background color Color ForeColor; // foreground color Font UsedFont; // font to be used int TextLeft; // left space to frame Thread Runner; // use multithreading int SpeedDelay; // speed settings int MsgDelay; // speed settings int CurrentY; // holds current Y-position of scrolling text int CurrentMsg; // holds item index for current item boolean Up = true; // direction Up if true, else direction is down String ErrorMsg=""; // contains ErrorMessage, if an error occurs String[] Items; //*************// // functions: // //*************// //******************************// // gets the specified parameter // // and decodes it to a Color // //******************************// Color DecodeColor(String param) { String pString = getParameter(param); Color Result = new Color(Integer.parseInt(pString.substring(0,2), 16), Integer.parseInt(pString.substring(2,4), 16), Integer.parseInt(pString.substring(4,6), 16)); return Result; } //**************************************// // checks, if string contains substring // //**************************************// boolean SubStringExists(String MainString, String SubString) { int index = MainString.indexOf(SubString); boolean Result; if ((index >= 0) & (index < MainString.length())) Result = true; else Result = false; return Result; } //*****************************// // get the specified parameter // // and decodes it to a Font // //*****************************// Font DecodeFont(String param) { int FontStyle; int FontSize; String FontName; Font Result; String pString = getParameter(param); if (pString == null) Result = new Font("Arial", Font.PLAIN, 12); else { int ende = pString.indexOf(","); FontSize = Integer.parseInt(pString.substring(0, ende)); int ende1 = pString.indexOf(",", ende+1); FontName = pString.substring(ende+2, ende1); pString = pString.toUpperCase(); FontStyle = Font.PLAIN; if (SubStringExists(pString, "ITALIC")) FontStyle += Font.ITALIC; if (SubStringExists(pString, "BOLD")) FontStyle += Font.BOLD; Result = new Font(FontName, FontStyle, FontSize); } return Result; } //****************************// // gets specified parameter // // and converts it to integer // //****************************// int DecodeInteger(String param) { String pString = getParameter(param); return Integer.parseInt(pString); } //**************************// // checks, if param exists // //**************************// boolean ParamExists(String param) { String pString = getParameter(param); boolean Result; if (pString == null) Result = false; else Result = true; return Result; } //***********************// // resizes Items[]-array // //***********************// String[] CreateItemArray(int count) { String ItemArray[] = new String[count]; for (int i = 0; i <= ItemArray.length; i++) { ItemArray[0] = ""; } return ItemArray; } //*************************************// // encodes a byte value to a character // //*************************************// String EncodeByte(byte Byte) { String Result; switch (Byte) { case -28: Result = "ä"; break; case -10: Result = "ö"; break; case -4: Result = "ü"; break; case -42: Result = "Ö"; break; case -60: Result = "Ä"; break; case -36: Result = "Ü"; break; case -33: Result = "ß"; break; case -27: Result = "å"; break; case -59: Result = "Å"; break; default: String[] List = { "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", " ", "!", "'", "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ":", ";", "<", "=", ">", "?", "@", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", " ", "_", " ", "^", "_", " ", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "{", "|", "}", "~", " ", "€", " ", "‚", "ƒ", "„", "…", "†", "‡", "ˆ", "‰", "Š", "‹", "Œ", " ", "Ž", " ", " ", "‘", "’", "“", "”", "•", "–", "—", "˜", "™", "š", "›", "œ", " ", "ž", "Ÿ", " ", "¡", "¢", "£", "¤", "¥", "¦", "§", "¨", "©", "ª", "«", "¬", "­", "®", "¯", "°", "±", "²", "³", "´", "µ", "¶", "·", "¸", "¹", "º", "»", "¼", "½", "¾", "¿", "À", "Á", "Â", "Ã", "Ä", "Å", "Æ", "Ç", "È", "É", "Ê", "Ë", "Ì", "Í", "Î", "Ï", "Ð", "Ñ", "Ò", "Ó", "Ô", "Õ", "Ö", "×", "Ø", "Ù", "Ú", "Û", "Ü", "Ý", "Þ", "ß", "à", "á", "â", "ã", "ä", "å", "æ", "ç", "è", "é", "ê", "ë", "ì", "í", "î", "ï", "ð", "ñ", "ò", "ó", "ô", "õ", "ö", "÷", "ø", "ù", "ú", "û", "ü", "ý", "þ", "ÿ", " " }; Result = List[Byte-1]; break; } return Result; } //********************// // applet functions: // //********************// public void init() { //initialize input stream: InputStream File = null; try { File = new URL(getDocumentBase(), getParameter("file")).openStream(); byte b[] = new byte[1]; byte cr[] = new byte[4]; File.read(cr); int CR = -1; int Error = 0; int ItemCount = 0; if ((cr[0]==0x2A) && (cr[2]==0x2A)) CR = 1; if ((cr[0]==0x2A) && (cr[3]==0x2A)) CR = 2; File.close(); File = new URL(getDocumentBase(), getParameter("file")).openStream(); switch (CR) { case -1: ErrorMsg = "internal error: file is empty!"; break; case 1: //carriage return only #13 OR #10 { for (int i=0;i<=3;i++) { File.read(b); } while (Error != -1) { Error = File.read(b); if ((b[0]==13) || (b[0]==10)) ItemCount += 1; } Items = CreateItemArray(ItemCount); for (int i=0; i <= Items.length-1; i++) Items[i] = ""; //String Items[] = new String[ItemCount]; File.close(); File = new URL(getDocumentBase(), getParameter("file")).openStream(); //jump over *#13#10*#13#10: for (int i=0;i<=5;i++) { File.read(b); } Error = 0; int CurrentItem = 0; while (Error != -1) { Error = File.read(b); if ((b[0] != 13) && (b[0] != 10)) { if (Error != -1) Items[CurrentItem] += EncodeByte(b[0]); } else { CurrentItem = CurrentItem + 1; } } } break; case 2: //carriage return plus line feed #13#10 { // * #13 #10 * #13 #10 // 0 1 2 3 4 5 for (int i=0;i<=4;i++) { File.read(b); } //determine ItemCount: while (Error != -1) { Error = File.read(b); if ((b[0]==13) || (b[0]==10)) { Error = File.read(b); ItemCount += 1; } } Items = CreateItemArray(ItemCount); for (int i=0; i <= Items.length-1; i++) Items[i] = ""; //String Items[] = new String[ItemCount]; File.close(); File = new URL(getDocumentBase(), getParameter("file")).openStream(); //jump over *#13#10*#13#10: for (int i=0;i<=5;i++) { File.read(b); } Error = 0; int CurrentItem = 0; while (Error != -1) { Error = File.read(b); if ((b[0] != 13) && (b[0] != 10)) { if (Error != -1) Items[CurrentItem] += EncodeByte(b[0]); } else { Error = File.read(b); CurrentItem = CurrentItem + 1; } } } break; } } catch (Exception e) { ErrorMsg = e.toString(); } //free stream if operations finished: try { if (File != null) File.close(); } catch (Exception e) { ErrorMsg = e.toString(); } //get colors: if (ParamExists("background")) BackColor = DecodeColor("background"); else BackColor = Color.white; if (ParamExists("foreground")) ForeColor = DecodeColor("foreground"); else ForeColor = Color.black; //get font and its style: UsedFont = DecodeFont("font"); //get the timer parameters: if (ParamExists("speeddelay")) SpeedDelay = DecodeInteger("speeddelay"); else SpeedDelay = 10; if (ParamExists("msgdelay")) MsgDelay = DecodeInteger("msgdelay"); else MsgDelay = 1500; //left frame: if (ParamExists("left")) TextLeft = DecodeInteger("left"); else TextLeft = 10; if (Up) CurrentY = getSize().height+UsedFont.getSize()+5; else CurrentY = -5; //initialize OffScreen Images: OffScreenImage = createImage(getSize().width, getSize().height); OffScreen = OffScreenImage.getGraphics(); OffScreen.setFont(UsedFont); } public void start() { if (Runner == null) { Runner = new Thread(this); Runner.start(); } } public void stop() { if (Runner != null) { Runner = null; } } public void run() { Thread thisThread = Thread.currentThread(); while (Runner == thisThread) { repaint(); try { Thread.sleep(SpeedDelay); if (CurrentY == ((int) (getSize().height/2+UsedFont.getSize()/2-1))) { try { Thread.sleep(MsgDelay); } catch (InterruptedException e) { ErrorMsg = e.toString(); } } } catch (InterruptedException e) { ErrorMsg = e.toString(); } } } public void update(Graphics g) { paint(g); } public void destroy() { OffScreen.dispose(); } public void paint(Graphics canvas) { //fill rectangel with background color: OffScreen.setColor(BackColor); OffScreen.fillRect(0, 0, getSize().width, getSize().height); OffScreen.setColor(ForeColor); setBackground(BackColor); if (Up == true) { CurrentY--; if (CurrentY < -5) { CurrentY = getSize().height+5+UsedFont.getSize(); CurrentMsg++; if (CurrentMsg > (Items.length - 1)) CurrentMsg = 0; } OffScreen.drawString(Items[CurrentMsg], TextLeft, CurrentY); } else { CurrentY++; if (CurrentY >= getSize().height+5+UsedFont.getSize()) { CurrentY = -5; CurrentMsg++; if (CurrentMsg > (Items.length - 1)) CurrentMsg = 0; } OffScreen.drawString(Items[CurrentMsg], TextLeft, CurrentY); } //if (ErrorMsg != "") // OffScreen.drawString("ErrorMsg: "+ErrorMsg, 0,getSize().height-1); //copy OffScreenImage to Screen: canvas.drawImage(OffScreenImage, 0, 0, this); } public String getAppletInfo() { return "Title: Scroller-Applet \nAuthor: Klaus Burgstaller \nThis applet is a kind of news scroller \nlast modified on 29.11.2001 \ncopyright (c) by Klaus Burgstaller. \nwww.crosswinds.net/~burgstaller"; } public String[][] getParameterInfo() { String[][] info = { {"file", "path string", "Specifies file containing data to display as relative path. \nThis parameter has no default value. The component doesn't work otherwise!"}, {"foreground", "hex color", "Foreground color in hex format: RRGGBB"}, {"background", "hex color", "Works the same way for the back ground color"}, {"font", "font size, font name, font style", "contains description about the font style: \nfirst parameter is font size, second font name, \nadditional font stylings can be given: BOLD, ITALIC, PLAIN. \nYou may write the font stylings upper od lower case. \nDefault = Arial, plain, size:12 ."}, {"speeddelay", "int", "Motion speed in milli seconds between graphics refresh, default: 10"}, {"msgdelay", "int", "Text will stop for xxx milliseconds, then go on, default: 1500"}, {"left", "int", "Specifies the space between left border and text in pixels, default: 10"} }; return info; } }