/*
 * part of SpaceWars project
 * Burton Rosenberg
 * May 2004
 */
 
import java.awt.* ;
import javax.swing.* ;

class TheView extends JPanel {
        // drawing panel for spacewars
        
        static final Color ROCKET_COLOR = Color.yellow ;
        static final Color FIRE_COLOR = Color.red ;
        static final Color BACKGROUND_COLOR = Color.black ;
        static final Color STAR_COLOR = Color.white ;  

        static final int STAR_SIZE = 1 ;
        static final double SCALE = 10.0 ;
        
        TheModel tm ; 
    
        TheView( TheModel tm ) {
		setBackground( BACKGROUND_COLOR ) ;
                this.tm = tm ;
                tm.setTheView(this) ;
        }
        
        static final int XP_LENGTH = 10 ;
        double [] XP = { 0.0, -1.0, 0.0, 1.0, 0.0, 0.0, 0.0, -0.3, 0.0, 0.3 } ;
        double [] YP = { 0.0, 1.0, -2.0, 1.0, 0.0, 2.0, 0.0, 1.4, 0.0, 1.4 } ;
 
        int [] xp = new int[XP_LENGTH] ;
        int [] yp = new int[XP_LENGTH] ;
        int [] xp2 = new int[XP_LENGTH] ;
        int [] yp2 = new int[XP_LENGTH] ;
        
        int y_location_trace = 0 ;
        int x_location_trace = 0 ;
        
        private void fullCalculateModel() {
             
             Dimension d = getSize() ;
             int x_offset = d.width / 2 ;
             int y_offset = d.height / 2 ;

             int x_location = (int) tm.getXlocation() ;
             int y_location = (int) tm.getYlocation() ;

             double theta = tm.getTheta() ;
             double cos_theta = Math.cos(theta) ;
             double sin_theta = Math.sin(theta) ;
                      
             // from XP, YP, angle and scale factors, 
             // fill in values for xp, yp
             for ( int i=0; i<xp.length; i++ ) {
                    
                    //rotation around zero in the model's coordinates
                    double x = cos_theta * XP[i] + sin_theta * YP[i] ;
                    double y = - sin_theta * XP[i] + cos_theta * YP[i] ;
                    
                    /// translate and then map to view's coordinates
                    xp[i] = (int)( x * SCALE ) ;
                    xp2[i] = xp[i] + x_location + x_offset  ;
                    yp[i] = (int)( y * SCALE ) ;
                    yp2[i] = yp[i] + y_location + y_offset ;
                    
             }
        }
            
        private void quickCalculateModel() {
                                                  
             Dimension d = getSize() ;
             int x_offset = d.width / 2 ;
             int y_offset = d.height / 2 ;
     
             int x_location = (int) tm.getXlocation() ;
             int y_location = (int) tm.getYlocation() ;
  
             for ( int i=0; i<xp.length ; i++ ) {
                  xp2[i] = xp[i] + x_location + x_offset ;
                  yp2[i] = yp[i] + y_location + y_offset ;
             }
        }
 
        public void paintComponent( Graphics gc ) { 
            super.paintComponent(gc) ;
            
            // calculate need offset
            
            //  if theta is changed, recalc model
            if ( tm.isNeededRotateUpdate() ) {
                    fullCalculateModel() ;
            }
            // else just apply new offset to existing model
            else {
                    quickCalculateModel() ;
            }
               
            gc.setColor( ROCKET_COLOR ) ;
            gc.fillPolygon( xp2, yp2, 4 ) ;
                
            if ( tm.isThrustOn() ) {
                    gc.setColor( FIRE_COLOR ) ;
                    // line width should be set
                    for ( int i=4; i<xp.length; i+= 2 ) {
                      gc.drawLine( xp2[i], yp2[i], xp2[i+1], yp2[i+1] ) ; 
                    }
            }

           int [] sx = tm.getStarX() ;
           if ( sx!=null ) {
                gc.setColor( STAR_COLOR ) ;
                // no critical race for sy != null
                int [] sy = tm.getStarY() ;
                for ( int i=0; i<sx.length; i++ ) {
                   gc.fillRect( sx[i], sy[i], STAR_SIZE, STAR_SIZE ) ;
                }
           }

        }
}
