Home Segments Top Top Previous Next

897: Mainline

Once you understand how the Marquee, MarqueeThread, and ChangeHandler classes work together, you can bring all three into one class using locally defined classes. The thread is created, and started, in the constructor.

import java.awt.*; 
import javax.swing.*; 
public class Marquee extends JComponent { 
 private MarqueeThread thread;           
 private String message; 
 private int position, drop, initialPosition, delta, messageWidth; 
 private Font messageFont = new Font("TimesRoman", Font.BOLD, 24); 
 private boolean ready = false; 
 public Marquee (String s) { 
  message = s; 
  thread = new MarqueeThread(this); 
  thread.start(); 
 } 
 // Definition of decrementPosition as in Segment 886 
 // Definition of paint as in Segment 886 
 class MarqueeThread extends Thread { 
  private Marquee marquee; 
  public MarqueeThread (Marquee c) { 
   marquee = c; 
  } 
  public void run () { 
   while (true) { 
    ChangeHandler changeHandler = new ChangeHandler(marquee); 
    SwingUtilities.invokeLater(changeHandler);               
    try{sleep(200);}                              
    catch (InterruptedException e) {}      
   } 
  } 
 } 
 class ChangeHandler implements Runnable { 
  private Marquee marquee; 
  public ChangeHandler (Marquee c) { 
   marquee = c; 
  } 
  public void run() { 
   marquee.decrementPosition(); 
  } 
 } 
 public Dimension getMinimumSize() {return new Dimension(300, 50);} 
 public Dimension getPreferredSize() {return new Dimension(300, 50);} 

}