0

I have an Array that is made up of 50 circles and I can get them to display when running the application. I feel like either I have faulty logic when trying to determine the distance between circles or I'm not setting the color properly after making the comparison.

This is my MAIN Circle class. One of the assignment requirements was that the circle be its own separate class.

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package CircleJavier;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.Group;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.control.Label;
import java.util.Random;
import javafx.scene.paint.Paint;
import javafx.scene.transform.Scale;
import javafx.scene.shape.*;


/**
 *
 * @author javyc
 */
public class Circ extends Group {
        Random rand = new Random();
        int x = rand.nextInt(300);
        int y = rand.nextInt(300);
        int rad = rand.nextInt(35);
        Paint fill = Color.RED;

    public Circ()
    {


        Circle cockpit = new Circle();
            cockpit.setCenterX(x);
            cockpit.setCenterY(y);
            cockpit.setRadius(rad);
            cockpit.setFill(fill);



        boolean addAll;
        addAll = getChildren().addAll(cockpit);
                   }


                         }

This is my main application code which I think is the one harboring the problem in the second for loop?

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package CircleJavier;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.Group;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.control.Label;
import java.util.Random;
import javafx.scene.paint.Paint;
import javafx.scene.transform.Scale;
import javafx.scene.shape.*;

/**
 *
 * @author javyc
 */
public class CircleJavier extends Application {

    @Override
    public void start(Stage primaryStage) {
//        Circ circ1 = new Circ();
//        Circ circ2 = new Circ();
//        Circ circ3 = new Circ();
//        Circ circ4 = new Circ();
//        Circ circ5 = new Circ();

        Circ[] circleArray = new Circ[50];
            for (int i = 0 ; i < circleArray.length; i++){
                Circ circle = new Circ();
                circleArray[i] = circle;

                boolean overlaps = false;

                for (int j = 0; j<i; j++){
                    double center = circleArray[j].x;
                    double dx = center - circle.x;
                    double y = circleArray[j].y;
                    double dy = y - circle.y;
                    double radius = circleArray[j].rad;

                    double dist = Math.sqrt((dx * dx)+(dy *dy));

                    if (dist <= (circle.rad + radius)) {

                        circleArray[i].fill = Color.AQUAMARINE;               
                        overlaps = true;
                        circleArray[j].fill = Color.CADETBLUE;
                    } 
                }
                if (!overlaps){
                    circleArray[i].fill = Color.BLACK;

                }
            }

        Group root = new Group();
        root.getChildren().addAll(circleArray);
        Scene Scene = new Scene(root, 500,350, Color.WHITE);



        primaryStage.setTitle("Elephant Javier!");
        primaryStage.setScene(Scene);
        primaryStage.show();   

    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}
user1803551
  • 12,965
  • 5
  • 47
  • 74
  • Could it be that Im just calculating the distance wrong? – Javier Ruiz Velez Apr 16 '19 at 02:49
  • You assign a field in your class which doesn't do anything except for changing the value of the field, since the constructor has already completed at that time and no other codes seems to use it... – fabian Apr 16 '19 at 03:08
  • can you use `intersects`? https://stackoverflow.com/questions/15013913/checking-collision-of-shapes-with-javafx – SedJ601 Apr 16 '19 at 03:44

1 Answers1

0

The main problem is that you are just updating the reference of the paint with a new value. The will not apply to the circle. If you want to apply on circle, create a method in Circ that accepts Paint and set on circle.

public class Circ extends Group {
    Random rand = new Random();
    int x = rand.nextInt(300);
    int y = rand.nextInt(300);
    int rad = rand.nextInt(35);
    Paint fill = Color.RED;
    Circle cockpit;
    public Circ() {
        cockpit = new Circle();
        cockpit.setStyle("-fx-stroke-width:1px;-fx-stroke:black;-fx-opacity:.5");
        cockpit.setCenterX(x);
        cockpit.setCenterY(y);
        cockpit.setRadius(rad);
        cockpit.setFill(fill);
        getChildren().addAll(cockpit);
    }

    public void setFill(Color clr) {
        cockpit.setFill(clr);
    }
}

And set the fill by calling the setFill of Circ.

circleArray[i].setFill(Color.AQUAMARINE);
circleArray[j].setFill(Color.CADETBLUE);
circleArray[i].setFill(Color.BLACK);

Setting some stroke and opacity can give the visual feedback of the overlapping ;)

Sai Dandem
  • 8,229
  • 11
  • 26