Answer:
See Explaination
Step-by-step explanation:
// CircleOverlap.java
import java.util.Random;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
public class CircleOverlap extends Application {
atOverride //Replace the at with at symbol
public void start(Stage primaryStage) {
//creating a Random number generator object
Random random = new Random();
//setting window size
int windowWidth = 500;
int windowHeight = 500;
//initializing array of circles
Circle array[] = new Circle[20];
//looping for 20 times
for (int i = 0; i < array.length; i++) {
//generating a value between 10 and 50 for radius
int radius = random.nextInt(41) + 10;
//generating a random x,y coordinates, ensuring that the circle fits
//within the window
int centerX = random.nextInt(windowWidth - 2 * radius) + radius;
int centerY = random.nextInt(windowHeight - 2 * radius) + radius;
//creating Circle object
Circle circle = new Circle();
circle.setCenterX(centerX);
circle.setCenterY(centerY);
circle.setRadius(radius);
//adding to array
array[i] = circle;
//flag to check if circle is overlapping any previous circle
boolean isOverlapping = false;
//looping through the previous circles to see if they are overlapping
for (int j = 0; j < i; j++) {
//finding x, y and radius of current circle under check
double x2 = array[j].getCenterX();
double dx = x2 - centerX;
double y2 = array[j].getCenterY();
double dy = y2 - centerY;
double r2 = array[j].getRadius();
//finding distance between this circle and the circle under check
double distance = Math.sqrt((dx * dx) + (dy * dy));
//checking if distance<radius1+radius2
if (distance <= (radius + r2)) {
//overlapping, setting transclucent blue color
Paint c = new Color(0, 0, 1.0, 0.3);
array[i].setFill(c);
isOverlapping = true;
//also changing the color of other circle
array[j].setFill(c);
}
}
if (!isOverlapping) {
//not overlapping, setting black color
array[i].setFill(Color.BLACK);
}
}
//creating a pane and adding all circles
Pane root = new Pane();
root.getChildren().addAll(array);
Scene scene = new Scene(root, windowWidth, windowHeight);
primaryStage.setScene(scene);
primaryStage.setTitle("");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}