/*
   MySecondButton.java
   Burton Rosenberg
   Tue Mar 24 14:30:32 EST 1998
*/

import java.awt.* ;
import java.awt.event.* ;
import java.applet.Applet ;

public class MySecondButton
extends Applet
implements ActionListener
{

     Button bUp = new Button("Count Up") ;
     Button bDown = new Button("Count Down") ;
     Panel buttonPanel = new Panel() ;
     DrawingPanel drawingPanel = new DrawingPanel() ;
  
     public void init()
     {
         bDown.addActionListener(this) ;
         bUp.addActionListener(this) ;
         setLayout( new BorderLayout() ) ;
         add( "Center", drawingPanel ) ;
         add("North", buttonPanel) ;
         buttonPanel.add(bDown) ;
         buttonPanel.add(bUp) ;
         validate() ;
     }

     public void actionPerformed( ActionEvent ae )
     {
         Object o = ae.getSource() ;
         if ( o==bUp ) {
            drawingPanel.addToN(+1) ;
         }
         if ( o==bDown ) {
            drawingPanel.addToN(-1) ;
         }
      
     }

}

class DrawingPanel
extends Canvas
{
     int n = 0 ;
     private final static int SCALE = 10 ;
     private final static int BAR_HEIGHT = 10 ;
     private final static int BAR_Y_POS = 20 ;

     void addToN( int delta ) {
         n+=delta ;
         repaint() ;
     }

     public void paint( Graphics gc )
     {
        Dimension d = getSize() ;

        gc.setColor( Color.blue ) ;
        gc.fillRect(
           d.width/2,BAR_Y_POS, BAR_HEIGHT/2, BAR_HEIGHT ) ;
        gc.setColor( Color.red ) ;
        gc.fillRect(
           d.width/2-BAR_HEIGHT/2, BAR_Y_POS, BAR_HEIGHT/2, BAR_HEIGHT ) ;

        if ( n>0 ) {
           gc.setColor( Color.blue ) ;
           gc.fillRect( d.width/2, BAR_Y_POS, SCALE*n, BAR_HEIGHT ) ;
        }
        else if ( n<0 ) {
           gc.setColor( Color.red ) ;
           gc.fillRect( d.width/2+SCALE*n, BAR_Y_POS, -SCALE*n, BAR_HEIGHT) ;
        }
             
     }

}
