I am working on a class project it's a flight radar simulator.
The situation is that when 2 planes are dangerously close the image of both flights changes, and when they are in a safe distance they change again.
The problem is that most of the time only one of those images is changed to red and I have no idea where the problem could be.
This is the method I am using to check the closeness condition, very simple and only checking for distance between them.
private void checkConflicts(ArrayList<Flight> flightsInArea) {
for (int i = 0; i < flightsInArea.size(); i++) {
for (int j = i + 1; j < flightsInArea.size(); j++) {
Coordinate currentFlight1 = flightsInArea.get(i).getCurrentPosition();
Coordinate currentFlight2 = flightsInArea.get(j).getCurrentPosition();
double cathetusX = Math.pow((currentFlight1.getPositionX() - currentFlight2.getPositionX()), 2);
double cathetusY = Math.pow((currentFlight1.getPositionY() - currentFlight2.getPositionY()), 2);
double distance = Math.sqrt(cathetusX + cathetusY);
if (distance < 100) {
flightsInArea.get(i).establishImage(true);
flightsInArea.get(j).establishImage(true);
} else {
flightsInArea.get(i).establishImage(false);
flightsInArea.get(j).establishImage(false);
}
}
}
}
This is the method use to establish the new image.
public void establishImage(boolean conflict) {
try {
if (conflict) {
image = ImageIO.read(this.getClass().getResource("red_plane.png"));
} else {
image = ImageIO.read(this.getClass().getResource("blue_plane.png"));
}
}
catch (IOException ex) {
Logger.getLogger(Vuelo.class.getName()).log(Level.SEVERE, null, ex);
}
}