/*
 * HitMiss.java    97/06/26
 *
 * Modifications by BF Caviness
 */



import java.awt.*;

/**
 *
 * This is a corrected version of the program in Figure 6.8, p. 129
 * of the book "Late Night Advanced Java."  In this version, we try
 * to stay as close to the author's original code as possible.
 * This program counts the number of times on which a button is clicked
 * (the hits) and the number of times the mouse is clicked not on the
 * button (the misses).
 *
 * Note that this code uses the deprecated Event handling methods from
 * JDK 1.0.  Event handling has been substantially changed in JDK 1.1.
 *
 */
public class HitMiss extends Frame
{
    private int hit = 0;
    private int miss = 0;
    TextField hitField, missField;

    HitMiss()
    {
        setTitle( "Hit and Miss" );	// Puts title in Frame.  Note that
        setLayout( new FlowLayout() );	// the title in the window display
        add( new Button( "button" ) );	// in Fig. 6.8, p. 129 of Late Night
        add( new Label( "Hits:" ) );	// Java is incorrect.
        hitField = new TextField( 5 );
        add( hitField );
        add( new Label( "Misses:" ) );
        missField = new TextField( 5 );
        add( missField );
    }

    // Respond to mouse clicks (other than on Buttons)
    public boolean mouseDown( Event evt, int x, int y )
    {
        System.out.println("HitMiss.mouseDown: " + "evt = " + evt );
        addMiss();
        return true;
    }

    // Respond to button clicks
    public boolean action( Event e, Object arg )
    {
        System.out.println("HitMiss.action: e = " + e + ", arg = " + arg );
        if ( e.arg == "button" )
        {
            addHit();
            return true;
        }
        else					// Pass event to higher
        {					// level for processing
            return super.action( e, arg );
        }
    }

    public void addHit()
    {
        hit++;
        repaint();
    }

    public void addMiss()
    {
        miss++;
        repaint();
    }

    // Respond to window destroy
    public boolean handleEvent( Event e )
    {
        System.out.println("HitMiss.handleEvent: e = " + e );
        if ( e.id == Event.WINDOW_DESTROY )
        {
             System.exit( 0 );
        }
        return super.handleEvent( e );
    }

    public void paint( Graphics g )
    {
        hitField.setText( String.valueOf( hit ) );
        missField.setText( String.valueOf( miss ) );
    }

    public static void main( String argv[] )
    {
        HitMiss f = new HitMiss();
        f.resize( 400,100 );
        f.show();
    }
}
