import java.awt.*; import java.applet.*; import java.io.*; import java.net.*; public class Filewindow extends Applet implements Runnable{ /** diplays a window into the given file parameter:fname-file name, speed-how fast to change, font-font type*/ String filename; URL thisurl; String s[]; int current; // index of line displayed int maxlines; Thread windower; int speed; FontMetrics fm; Dimension size; //height and width public void advance() { if (current==maxlines) current=0; else current=current+1;} public void paint(Graphics g) { g.clearRect(0,0,size.width,size.height); g.drawString(s[current],1,10); } public void start() { advance(); windower = new Thread(this); windower.start(); } public void stop() { windower.stop();} public void run() { while (true) { try {Thread.currentThread().sleep(speed);} catch (InterruptedException e){}; advance(); repaint(); } } public boolean mouseDown(Event eve,int x, int y) { try {getAppletContext().showDocument(thisurl);} catch (Exception e) {System.out.println("Sorry, can't find it");} return true; } private int c=' '; //static function var for readline private StringBuffer nextword=new StringBuffer(); private String readline(InputStream f) { //read a line from the stream fitting on our line, null if end if (c<0) return null; String line=new String(); int sizelimit=size.width-10; // cut up into lines while (c>=0){ try {c=f.read();} catch (IOException e) { System.out.println("Read error. :"+(char)c+" "); c=-2; break;} // System.out.print((char)c+" "+c+","); if (c=='<') { //skip blocks of <> *****warning! can't do single < correctly try { while(f.read() !='>'); } catch (IOException e) {System.out.print("skipping <>"); c=-2;} continue;} if ((c=='\n' || c=='\r') && nextword.length()==0 && line.length()==0) continue; //skip blank lines if (c==' ' || c=='\t' || c=='\n' || c=='\r' ) { //new word if (fm.stringWidth(line+nextword.toString()) >=sizelimit) {//buffer full nextword.append(' '); break;} //put next word on next line with space line=line+nextword.toString()+" "; nextword.setLength(0); if (c=='\n' || c=='\r') break; } //hard line break in file else nextword.append((char)c); } // just new letter //current char is whitespace if (line.length()>0) return line; else return null; //error } public void init() { InputStream f; int i=0; String fontname; Font font; size=size(); s=new String[300]; //will later declare it after getting file size setBackground(new Color(180,180,255)); filename=getParameter("fname"); if ((fontname=getParameter("font"))==null) fontname="TimesRoman"; font = new Font(fontname, Font.BOLD, size.height-1); setFont(font); fm=getFontMetrics(font); String att = getParameter("speed"); speed = (att == null) ? 5000 : (1000 * Integer.parseInt(att)); try {thisurl=new URL(getDocumentBase(),filename); f=thisurl.openStream();} catch (IOException e) {System.out.println("File not available");return;} //read in lines from file while ((s[i++]=readline(f))!=null) if (i>=s.length-2) {//resize array String[] temp=new String[2*s.length]; System.arraycopy(s,0,temp,0,s.length); s=temp;} maxlines=i-2; current=0; try {f.close();} catch (IOException e) {System.out.print("can't close()");}} }