/*
 * 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 show
 * how to handle all events through the overloaded method handleEvent().
 * This eliminates the need for the methods mouseDown() and action() found
 * in the other version.
 *
 * 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_v2 extends Frame
{
    private int hit = 0;
    private int miss = 0;
    TextField hitField, missField;

    HitMiss_v2()
    {
        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 );
    }

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

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

    // Respond to all events
    public boolean handleEvent( Event e )
    {
        System.out.println("HitMiss_v2.handleEvent: e = " + e );
        switch ( e.id )
        {
            case Event.MOUSE_DOWN:      addMiss();
                                        return true;
            case Event.ACTION_EVENT:    if ( e.arg == "button" )
                                            {
                                                addHit();
                                                return true;
                                            }
                                        break;
            case Event.WINDOW_DESTROY:  System.exit( 0 );
        }

        return super.handleEvent( e );	// Handle all other events
    }

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

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