import java.applet.*;
import java.awt.*;

public class Stub extends Applet {
    final static boolean CREATE_ONE = true;

    final static String TITLE = "Create Calculator";
    final static Font FONT = new Font("Helvetica", Font.BOLD, 14);
    final static Color FORE = Color.white;
    final static Color BACK = Color.blue;

    final static int WIDTH = 180;
    final static int HEIGHT = 30;
    
    Item button_;
    Item clicked_;
    boolean lastInside_;

    public void init() {
        button_ = new Item(TITLE, 0, 0, WIDTH, HEIGHT, TITLE,
			   FONT, FORE, BACK, 0);
        if (CREATE_ONE)
            create();
    }
    
    public void create() {
        Calc.main(null);
    }
    
    public void update(Graphics g) {    
        paint(g);
    }

    public void paint(Graphics g) {
        button_.draw(g);
    }
    
    public boolean handleEvent(Event evt) {
        // if a button is being clicked
        if (clicked_ != null) {
            // track drags to make sure button state is correct
            if (evt.id == Event.MOUSE_DRAG) {
                boolean inside = clicked_.inside(evt.x, evt.y);
                if (inside != lastInside_) {
                    lastInside_ = inside;
                    clicked_.pressed_ = inside;
                    repaint();
                    }
                return true;
            }
            // on mouse up, if mouse is inside call clicked
            else if (evt.id == Event.MOUSE_UP) {
                // button was clicked?
                if (lastInside_)
                    create();

                lastInside_ = false;
                clicked_.pressed_ = false;
                repaint();
                clicked_ = null;
                return true;
            }
            return false;
        }

        // on mouse down, if someone clicks inside an item start
        // tracking
        if (evt.id == Event.MOUSE_DOWN) {
            Item target = button_;
            if (target != null && target.enabled_) {
                target.pressed_ = true;
                clicked_ = target;
                lastInside_ = true;
                repaint();
            }
            return true;
        }

        return false;
    }
}
