// Generated by Together
package gui2000;
import java.util.*;
class ProximityDetector extends Thread
{ public ProximityDetector( Vector a )
{ aircraft = a;
setDaemon( true );
start();
}
private Vector aircraft = null;
public void run()
{ 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 )
{}
}
}
private final static int NEAR_MISS = 2;
private final static int CRASH = 1;
private final static int OK = 0;
private int close( 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;
}
}
|