home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-11-03 | 1.7 KB | 65 lines |
- import java.awt.*;
-
- public class Ticker extends java.applet.Applet implements Runnable {
- String message;
- Font f;
- FontMetrics fm;
- int position;
- Thread runner;
- Graphics offscreenG;
- Image offscreenImg;
-
-
- public void init () {
- message = getParameter("tickertext");
- f = new Font("Arial", Font.BOLD, 16);
- fm = getFontMetrics(f);
- position = 0;
- offscreenImg = createImage (size().width, size().height);
- offscreenG = offscreenImg.getGraphics();
- }
-
- public void start () {
- if (runner == null) {
- runner = new Thread(this);
- runner.start();
- }
- }
-
- public void stop () {
- if (runner != null) {
- runner.stop();
- runner = null;
- }
- }
-
- public void run () {
- while (true) {
- position++;
- repaint();
- if (position > size().width + fm.stringWidth(message)) {
- position = 0;
- }
- try {
- runner.sleep(25);
- }
- catch (InterruptedException e) {
- }
- }
- }
-
- public void update(Graphics g) {
- g.clipRect(0, 0, size().width, size().height);
- paint(g);
- }
-
- public void paint(Graphics g) {
- offscreenG.setFont(f);
- offscreenG.setColor(Color.black);
- offscreenG.fillRect(0, 0, size().width, size().height);
- offscreenG.setColor(Color.white);
- offscreenG.drawString(message, size().width - position, 20);
-
- g.drawImage(offscreenImg, 0, 0, this);
- }
- }