import java.applet.Applet;

import java.awt.*;
import java.awt.event.*;

/** Example code showing use of tooltips / balloon help */

public class HelpDemo extends Applet implements
ActionListener,
ItemListener
{

    private Panel toolbar;

    private BubbleHelper BH;

    private RadioGroup RG;

    private Checkbox
	CB_Up,
	CB_Down;

    private Button
	B_One,
	B_Two;

    private TextArea textarea;

    private MappingBrick MB1, MB2, MB3;

/** To run the demo as an application,
 * construct it and show it in a frame.
 * This must be a static (class not instance) method,
 * because the instance won't exist until we construct it here. */

  public static void main (String []argv) {

      Frame ff = new Frame ("HelpDemo");
      ff.setBounds (100, 100, 600, 500);

      HelpDemo cd = new HelpDemo ();
      ff.add (cd);

      cd.init ();

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

/* Only now that it is showing we can get the layout right */

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

/** Constructor */

  public HelpDemo () {

	setBackground (Color.gray);

	BH = new BubbleHelper ();

	setLayout (new GridLayout (2, 0));

        add (toolbar = new Panel ());

	toolbar.setLayout (new WrapLayout ());

	toolbar.add
	   (B_One
	   = new Button ("One"));

        BH.setHelp (B_One, "Button One");

	toolbar.add
	   (B_Two
	   = new Button ("Two"));

        BH.setHelp (B_Two, "Button Two");

/* Manage a Radio Group,
 * extension of CheckboxGroup that can manage both,
 * regular Checkbox and CheckImageButton */

	RG = new RadioGroup ();

        toolbar.add
	  (CB_Up
	  = new Checkbox ("Up"));

        BH.setHelp (CB_Up, "Up CB");

	CB_Up.setCheckboxGroup (RG);

        toolbar.add
	  (CB_Down
	  = new Checkbox ("Down"));

        BH.setHelp (CB_Down, "Down CB");

	CB_Down.setCheckboxGroup (RG);

        toolbar.add (MB1 = new MappingBrick (50, 40, Color.green));
	BH.setMappingHelp (MB1);

        toolbar.add (MB2 = new MappingBrick (40, 50, Color.yellow));
	BH.setMappingHelp (MB2);

        toolbar.add (MB3 = new MappingBrick (50, 50, Color.red));
	BH.setMappingHelp (MB3);

        add (textarea = new TextArea (12, 60));
	textarea.append ("\nHelpDemo\n");
    }

    public void init () {

	textarea.append ("HelpDemo\n");
	textarea.append ("Hello\n");

/* Make all possible connections between
 * all our listener methods and all our toolbar components */

        ListenerAdder.add_Listeners (this, toolbar);

        toolbar.invalidate ();
        invalidate ();

        validate ();
    }

    public void itemStateChanged (ItemEvent e) {
	textarea.append ("\n "+e);
    }

    public void actionPerformed (ActionEvent e) {
	textarea.append ("\n "+e);
    }
}

class MappingBrick extends Component implements Mapping {

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

    public MappingBrick (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;
    }

/** Simple example of the Mapping interface,
 * just tells which quadrant is under the cursor,
 * or near the exact center. */

    public String XYToString (int x, int y) {
	Dimension d = size ();
	String LR = "Right", TB = "Bottom";

        int dxx = x - d.width / 2,
	    dyy = y - d.height / 2;

        if ((0 == dxx / 4)
	 && (0 == dyy / 4))
	     return "Center";

	if (dxx < 0)
	    LR = "Left";

	if (dyy < 0)
	    TB = "Top";

	return LR+" "+TB;
    }

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