import java.awt.*;

import  java.awt.event.*;
import java.util.*;
import java.io.*;

/** Example code showing use of CursorPanel */

public class CursorDemo extends java.applet.Applet
 implements AdjustmentListener
 {

  private TextArea textarea;
  private CursorPanel SP = null;
  private ColorBrick [] CBs = new ColorBrick [6];
  private CursorLabel [] CLs = new CursorLabel [6];
  private PanelCursor iacx = null;
  private PanelCursor ibcx = null;

/** To run demo as application,
 * construct it and show it in a frame */

  public static void main (String []argv) {
      Frame ff = new Frame ("CursorDemo");
      ff.setBounds (100, 100, 500, 500);
      CursorDemo cd = new CursorDemo ();
      ff.add (cd);
      cd.init ();

      ff.pack ();
      ff.show ();

     cd.invalidate ();
     cd.validate ();

      ff.pack ();
  }

  public CursorDemo () {

     setLayout (new GridLayout (0, 2, 5, 5));

     add (textarea = new TextArea (12, 20));

     CursorPanel SP = new CursorPanel ();

     add (SP);

     SP.setLayout (new GridLayout (0, 1, 5, 5));

/* Peer-based widgets will draw **OVER** our Cursor,
 * and completely hide it unless there are gaps between them.
 * but Canvas-based widgets will be drawn **UNDER** our Cursor. */

     SP.add (CBs [0] = new ColorBrick (50, 40, Color.blue));
     SP.add (CBs [1] = new ColorBrick (50, 40, Color.green));
     SP.add (CBs [2] = new ColorBrick (50, 40, Color.yellow));

     SP.add (new Button ("Who"));
     SP.add (new Button ("Why"));
     SP.add (new Button ("What"));

     SP.add (CBs [3] = new ColorBrick (30, 40, Color.green));
     SP.add (CBs [4] = new ColorBrick (30, 40, Color.yellow));
     SP.add (CBs [5] = new ColorBrick (30, 40, Color.red));

     SP.invalidate ();
     invalidate ();
     validate ();

     SP.invalidate ();
     invalidate ();
     validate ();

/* Short form constructor */

     iacx = SP.addCursor (Color.green, true);
     iacx.addAdjustmentListener (this);
     iacx.setVisible (true);

/* Long form constructor */

     ibcx = SP.addCursor (null, (float)0.5, 3, Color.blue, -3, true);
     ibcx.addAdjustmentListener (this);
     ibcx.setVisible (true);
  }

/* We added (this) as listener on both cursors,
 * but only using one of them here. */

  public void adjustmentValueChanged (AdjustmentEvent ae) {
      if (ae.getSource () == iacx) {
          update_readouts
           (((PanelCursor)(ae.getSource ())).getFraction ());

          for (int ii = 0; ii < CBs.length; ii++)
             if (null != CLs[ii])
               CLs[ii].setString (""+oldf);
             else
               CLs[ii] = iacx.addCursorLabel (CBs[ii], ""+oldf);
       }
   }

   private float oldf = 0;

   void update_readouts (float fraction) {
       if ((oldf - 0.05 < fraction)
	&& (oldf + 0.05 > fraction))
	    return;
       textarea.append ("\n"+fraction);
       oldf = fraction;
   }
}

/* Some kind of stripchart or other linear data plot. */

class ColorBrick extends Component {

    int w;
    int h;
    Color c;
    Dimension prefSize;

    public ColorBrick (int w, int h, Color c) {
	this.w = w;
	this.h = h;
	this.c = c;
	prefSize = new Dimension (w, h);
    }

    public Dimension minimumSize () {
        return getMinimumSize ();
    }

    public Dimension getMinimumSize () {
        return getPreferredSize ();
    }

    public Dimension preferredSize () {
        return getPreferredSize ();
    }

    public Dimension getPreferredSize () {
        return prefSize;
    }

/** Displayable value corresponding to some position along the row */

    public String atCursor (float fraction) {
       return ""+fraction;
    }

    public void paint (Graphics g) {
        Dimension S = getSize ();
	g.setColor (c);
	g.fillRect (0, 0, S.width, S.height);
    }
}
/* <IMG SRC="/cgi-bin/counter">*/
