
/*
   SpaceWar
   Burton Rosenberg
   Thu Jul 10 16:34:42 EDT 1997

*/

package SpaceWar ;
import java.applet.* ;
import java.awt.* ;

public class SpaceWarTest extends Applet {

    SpaceCanvas sc ;
    Scrollbar directionControl ;
    Button boosterControl ;
    Label speedometer ;
    Label heading ;

    final static int SCROLLBAR_MAX = 361 ;
    final static int INITIAL_HEADING = 180 ;
    int spacecraftDirection = INITIAL_HEADING ;
    
    public void init() {
        Panel controlPanel ;
        Panel leftPanel ;
        Panel rightPanel ;
        Panel centerPanel ;
        setLayout(new BorderLayout()) ;
        add("Center", sc = new SpaceCanvas() ) ;
        add("South", controlPanel = new Panel() ) ;
        controlPanel.setLayout(new GridLayout(0,2)) ;
        controlPanel.add( leftPanel = new Panel() ) ;
        controlPanel.add( rightPanel = new Panel() ) ;
        leftPanel.setLayout(new BorderLayout()) ;
        leftPanel.add( "South", directionControl = new Scrollbar( 
                      Scrollbar.HORIZONTAL, 
                      INITIAL_HEADING, 1, 0, SCROLLBAR_MAX )) ;
        leftPanel.add( "Center", centerPanel = new Panel() ) ;
        centerPanel.add( speedometer = new Label("Velocity = 0.000") ) ;
        centerPanel.add( heading = 
                         new Label("Heading = " + INITIAL_HEADING ) ) ;
        sc.setSpacecraftDirection(INITIAL_HEADING) ;
        rightPanel.setLayout(new GridLayout(0,5)) ;
        rightPanel.add( new Label("") ) ;
        rightPanel.add( new Label("") ) ;
        rightPanel.add( boosterControl = new Button("Booster") ) ;

        validate() ;
    }

    public boolean handleEvent(Event e) {
        if ( e.target == directionControl ) {
             spacecraftDirection = directionControl.getValue() ;
             sc.setSpacecraftDirection(spacecraftDirection) ;
             heading.setText( "Heading = " + spacecraftDirection ) ;
             //heading.repaint() ;
             return true ; 
        } 
        else return super.handleEvent(e) ;
    }              

}

class SpaceCanvas extends Canvas {

    int spacecraftDirection = 0 ;

    public void setSpacecraftDirection(int spacecraftDirection) {
        this.spacecraftDirection = spacecraftDirection ;
        this.repaint() ;
    }

    final static double R = 60.0 ;

    public void paint(Graphics gc) {
         Dimension d = size() ;
         double heading = Math.PI*((double)spacecraftDirection)/180.0 ;
         int xd = (int)(Math.cos(heading) * R) ;
         int yd = (int)(Math.sin(heading) * R) ;
         gc.setColor( Color.red ) ;
         gc.drawLine( d.width/2, d.height/2, 
                      d.width/2+xd, d.height/2+yd ) ;
    }

}
