import java.applet.Applet; import java.applet.*; import java.awt.*; public class scroll extends Applet implements Runnable { private Image image; private Graphics offscreen; private Thread runner; private boolean running; private Color background; private Color foreground; private Font f; private FontMetrics fm; private static final int maxLine=100; private int lMargin=5; private String lines[]; private String text; private int whichLine=0; private int maxSize; private int maxWidth; private int typeHeight; private int typeLength; private int typePos; private int runningIndex=0; private int arrayCount=0; private int onScreenLines=0; private int where=0; private int offset=0; private int start=0; private int pointSize; private int speed; public void init() { background=Color.black; foreground=Color.gray; maxWidth=getSize().width; maxSize=getSize().height; //create offscreen image image=createImage(maxWidth,maxSize); offscreen=image.getGraphics(); //get applet parameters for scroll speed, point size & message String s=getParameter("pointSize"); if (s==null) { pointSize=30; } else pointSize=Integer.parseInt(s); if (pointSize>30 || pointSize<=0) pointSize=30; String s2=getParameter("speed"); if (s2==null) { speed=80; } else speed=Integer.parseInt(s2); text=getParameter("text"); if (text==null) text="No message. "; maxWidth=getSize().width; maxSize=getSize().height; //create offscreen image image=createImage(maxWidth,maxSize); offscreen=image.getGraphics(); //create font & type f=new Font ("Helvetica",Font.BOLD,pointSize); fm=getFontMetrics (f); typeHeight=fm.getHeight(); typePos=maxSize+typeHeight; lines=new String[maxLine]; split(); } public void update(Graphics g) { paint(g); } public void paint(Graphics g) { offscreen.setColor(background); offscreen.fillRect(0,0,maxWidth,maxSize); scroll(); g.drawImage(image,0,0,this); } public void destroy() { offscreen.dispose(); } public void start() { if (runner==null) { runner=new Thread(this); runner.start(); running=true; } } public void stop() { running=false; } public void run() { try { while (running) { runner.sleep(20); repaint(); } } catch(InterruptedException e) { running = false; } } public void split() { //parse long text string into scrollable lines int index=0; int nextSpace=0; int prevSpace=0; String runningTotal=""; String testLength=""; if ((fm.stringWidth(text)+lMargin)>maxWidth) { while (runningIndexmaxWidth || (nextSpace==text.length()-1)) { lines[arrayCount]=runningTotal.trim(); arrayCount++; runningTotal=""; index=prevSpace; } runningIndex=nextSpace+1; runningTotal=testLength; prevSpace=nextSpace; } lines[arrayCount]=runningTotal.trim(); } else { arrayCount=1; lines[0]=text.trim(); } arrayCount--; } public void scroll() { offscreen.setColor(foreground); offscreen.setFont(f); //iteration count for display calc (start=array pointer) int j=0; for (int i=start;i<=onScreenLines;i++) { offscreen.drawString(lines[i],lMargin,typePos+(j*typeHeight)); j++; } //position of lead line typePos--; //running total of scroll offset offset++; //offset=typeheight so bump array of text end pointer if (offset==typeHeight) { offset=0; onScreenLines++; } if (onScreenLines>arrayCount) onScreenLines=arrayCount; //message finished scroll so reset if ((start==onScreenLines) && (typePos<(0-typeHeight))) { start=0; onScreenLines=0; typePos=maxSize+typeHeight; } //line scrolled off top, so adjust array pointer if (typePos<(0-typeHeight)) { start++; typePos=0; } } }