this is a Java applet I once wrote that scroll text.
it does not support graphics or links, though.
--------------------------
this applet accept three parameters at the html :
speed : the speed of the scrolling.
delay : ms to delay when a line got to the top of the applet.
filename : the headlines filename on the server, a headlines files is a text file, where each line is
a headline.
new line (enter) seperate headlines.
example :
-- headlines.txt begin --
new! scroller applet by the man!
blah!
blah!
another blah!!
-- headlines.txt end --
I put some extra lines to make it nice.
this file goes to the applet codebase on the http server.
feel free to modify it, but leave the comment specifying I am the author.
(you may want to change colors and stuff)
//-------------- scroller.java begin ---
/*
Scroller applet , Omry Yadan 2000.
*/
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.net.*;
import java.util.*;
import java.io.*;
public class scroller extends Applet implements Runnable
{
private volatile Thread scroller;
int speed;
int delay;
String filename;
Vector strings = new Vector();
boolean headline_at_top = false; // when head line at top, its time to pause (wait the dalay)
int current_shown_line = 0;
int current_line_pos = 0; // 0 is the buttom of the applet.
final int headlines_gap = 5; // gap in pixels between headlines.
final int line_height = 20;
//Get a parameter value
public String getParameter(String key, String def)
{
return getParameter(key) != null ? getParameter(key) : def;
}
//Construct the applet
public scroller()
{
}
//Initialize the applet
public void init()
{
try
{
speed = Integer.parseInt(this.getP
}
catch(Exception e)
{
e.printStackTrace();
}
try { speed = Integer.parseInt(this.getP
e) { e.printStackTrace(); }
try
{
delay = Integer.parseInt(this.getP
}
catch(Exception e)
{
e.printStackTrace();
}
try { delay = Integer.parseInt(this.getP
e) { e.printStackTrace(); }
try
{
filename = this.getParameter("filenam
}
catch(Exception e)
{
e.printStackTrace();
}
try
{
loadHeadlines();
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception
{
}
public void reshape(int x, int y, int w, int h)
{
super.reshape(x,y,w,h);
current_line_pos= bounds().height;
}
//Start the applet
public void start()
{
scroller = new Thread(this);
scroller.start();
}
//Stop the applet
public void stop()
{
scroller = null;
}
//Destroy the applet
public void destroy()
{
}
//Get Applet information
public String getAppletInfo()
{
return "Applet Information";
}
//Get parameter info
public String[][] getParameterInfo()
{
String[][] pinfo =
{
{"speed", "int", "1 to 10"},
{"delay", "int", ""},
{"filename", "String", ""},
};
return pinfo;
}
private void loadHeadlines()
{
try {
URL headlines = new URL(getCodeBase(),filename
DataInputStream text=new DataInputStream(new BufferedInputStream(headli
String s = null;
while((s = text.readLine()) != null)
{
strings.addElement(s);
}
}
catch (Exception ex) {
ex.printStackTrace();
}
}
public void run()
{
Thread thisThread = Thread.currentThread();
while (scroller == thisThread)
{
try
{
thisThread.sleep(speed * 10);
if(headline_at_top)
{
Object first = strings.elementAt(0);
strings.removeElementAt(0)
strings.addElement(first);
System.err.println(strings
thisThread.sleep(delay);
headline_at_top = false;
current_line_pos += heightOf(current_shown_lin
System.err.println("");
current_shown_line++;
}
}
catch (InterruptedException e){}
repaint();
current_line_pos--;
}
}
private int heightOf(int index)
{
return line_height; // currently hardcoded, should be implemented better.
}
public void paint(Graphics g)
{
int y = 0;
if(current_line_pos <= 0)
headline_at_top = true;
for (int i =0;i<strings.size();i++ )
{
String line = (String)strings.elementAt(
g.drawString(line,5,curren
}
}
}
//-------------- scroller.java end ----
Main Topics
Browse All Topics





by: scientistPosted on 2001-03-16 at 12:00:45ID: 5935877
You can learn to write applet by studying the tutorial for applet provided by number of sites: ooks/tutor ial/applet /index.htm l om/program ming_langu ages/java/ applets.ht ml
The Links are:
http://java.sun.com/docs/b
These people gives you number of sites providing free appletes and applet tutorials:
http://www.freesitetools.c