FileDocCategorySizeDatePackage
ProximityDetector.javaAPI DocExample1696Sun Feb 13 17:12:30 GMT 2000gui2000

ProximityDetector

public class ProximityDetector extends Thread

Fields Summary
private Vector
aircraft
private static final int
NEAR_MISS
private static final int
CRASH
private static final int
OK
Constructors Summary
public ProximityDetector(Vector a)

  aircraft = a;
   	setDaemon( true );
   	start();
   
Methods Summary
private intclose(Aircraft a1, Aircraft a2)


          
   	int eastDiff = Math.abs( a1.east - a2.east);
   	int northDiff = Math.abs( a1.north - a2.north);
   	int heightDiff = Math.abs( a1.height - a2.height);
   	if( eastDiff < 100 && northDiff < 100 && heightDiff < 100 )
   		return CRASH;
   	else if( eastDiff < 500 &&  northDiff < 500 && heightDiff < 300 )
   		return NEAR_MISS;
   	else return OK;
   
public voidrun()

   
     
     mainLoop:
   	while( true )
      {  Object[] thePlanes = aircraft.toArray();

      	for( int a1 = 0; a1 < (thePlanes.length)-1; a1++ )
      		for( int a2 = a1+1; a2 < thePlanes.length; a2++ )
      		{  int howNear = close( (Aircraft)thePlanes[a1], (Aircraft)thePlanes[a2] );
      			if( howNear == NEAR_MISS )
      			{	((Aircraft)thePlanes[a1]).radio( "Help! Another aircraft is close!" );
      				((Aircraft)thePlanes[a2]).radio( "What is that?" );
      			}
      			else if( howNear == CRASH )
      			{	((Aircraft)thePlanes[a1]).radio( "OH NO !!" );
      				((Aircraft)thePlanes[a2]).radio( "Oh . . ." );
      				synchronized( aircraft )
      				{	aircraft.remove( thePlanes[a1] );
      					aircraft.remove( thePlanes[a2] );
      				}
      				continue mainLoop;
      			}
      		}

         try
			{  sleep( 1000 );
			}
			catch( Exception e )
			{}
      }