import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/*
 * LightweightRepaintTest.java
 *
 * Created on February 6, 2006, 2:29 PM
 */

/** Repaint some lightweights, to see if that works.
 * You should see a yellow box moving along, once per second, with the
 * label text under the box changing each time.
 *
 * @author scott
 */
public class LightweightRepaintTest extends Frame
{
  public static class LighweightLabel extends Component
  {
    String labelText;
    LighweightLabel (String title)
    {
      labelText = title;
    }
    public void setText (String text)
    {
      labelText = text;
    }
    public void paint ( java.awt.Graphics graphics )
    {
      /*
      System.out.println ("Painting: "
        + "size=" + getWidth () + "x" + getHeight ()
        + ", label=" + this
        + ", graphics=" + graphics
        + ", clip=" + graphics.getClip ()
        );
      */
      graphics.setColor (getBackground ());
      graphics.fillRect (0,0,getWidth (),getHeight ());
      graphics.setColor (getForeground ());
      graphics.drawString (labelText,2,15);
    }
  }
  private LighweightLabel [] labels = new LighweightLabel[3];

  public LightweightRepaintTest ()
  {
    setLayout (new GridLayout (1,0,1,1));
    setBackground (Color.gray);
    for (int i=0; i<labels.length; i++)
    {
      labels[i] = new LighweightLabel ("--");
      labels[i].setForeground (Color.black);
      labels[i].setBackground (Color.white);
      add(labels[i]);
    }
    pack ();
  }
  
  private int labelIndex = labels.length-1;
  private int pass = 0;
  public void doSomething ()
  {
    int oIndex = labelIndex;
    if (++labelIndex >= labels.length)
      labelIndex = 0;
    pass++;
    System.out.println ("Pass " + pass);
    labels[labelIndex].setText ("Pass " + pass);
    labels[labelIndex].setBackground (Color.yellow);
    labels[labelIndex].repaint ();
    labels[oIndex].setBackground (Color.white);
    labels[oIndex].repaint ();
  }
  
  private Dimension SZ = new Dimension (400,60);
  public Dimension getPreferredSize ()
  {
    return SZ;
  }
  public static void main (String[] args)
  {
    LightweightRepaintTest t = new LightweightRepaintTest ();
    t.addWindowListener (new WindowAdapter ()
    {
      public void windowClosing (WindowEvent evt)
      {
        System.exit (0);
      }
    }
    );
    t.setVisible (true);
    try
    {
      for (int i=0; i<10; i++)
      {
        Thread.currentThread ().sleep (1000);
        t.doSomething ();
      }
    } catch (InterruptedException ex)
    {
      ex.printStackTrace();
    }
    System.exit (0);
  }
  
}
