patternjavaMinor
Detecting two intersecting circles with editable x, y, and radius in JavaFX
Viewed 0 times
withintersectingcirclesradiustwoanddetectingjavafxeditable
Problem
I was actually pretty proud of this programming project that I came across in this book I'm working through. I only really had problems figuring out the formula for detecting whether or not the two circles had intersecting paths. I'm still getting the hang of object-oriented programming, primarily when and where to use
```
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
/**
* Created by John on 7/28/2014.
*/
public class Project16_8 extends Application {
private static String intersectAnswer = "Two circles intersect? ";
private static Label intersect;
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
// Initialize the intersect Label
intersect = new Label(intersectAnswer + "No");
// Pane to hold the circles
Pane circlePane = new Pane();
circlePane.setPadding(new Insets(5));
CircleObject circle1 = new CircleObject(52.0, 60.0, 30);
CircleObject circle2 = new CircleObject(180.0, 56.0, 40);
circlePane.getChildren().addAll(circle1, circle2);
// Circle 1 info Pane
Pane circle1Pane = new Pane();
CircleInfo circle1Info = new CircleInfo("Enter circle 1 info: ",
52.0, 60.0, 30);
circle1Pane.getChildren().add(circle1Info);
// Circle 2 info Pane
static, private, public, etc... but I think I'm on the right path. Let me know what you think.```
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
/**
* Created by John on 7/28/2014.
*/
public class Project16_8 extends Application {
private static String intersectAnswer = "Two circles intersect? ";
private static Label intersect;
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
// Initialize the intersect Label
intersect = new Label(intersectAnswer + "No");
// Pane to hold the circles
Pane circlePane = new Pane();
circlePane.setPadding(new Insets(5));
CircleObject circle1 = new CircleObject(52.0, 60.0, 30);
CircleObject circle2 = new CircleObject(180.0, 56.0, 40);
circlePane.getChildren().addAll(circle1, circle2);
// Circle 1 info Pane
Pane circle1Pane = new Pane();
CircleInfo circle1Info = new CircleInfo("Enter circle 1 info: ",
52.0, 60.0, 30);
circle1Pane.getChildren().add(circle1Info);
// Circle 2 info Pane
Solution
-
Constructor
can be simplified to
because the constructor can get x, y and radius from
-
The same for
-
-
-
Try to remove modifier
Constructor
CircleInfo(String circleLabel, double centerX, double centerY, double radius)can be simplified to
CircleInfo(String circleLabel, CircleObject obj)because the constructor can get x, y and radius from
CircleObject.-
The same for
checkIntersection(...). This method can belong to CircleObject, such that you can call it from the object itself, i.e., circle1.checkIntersection(circle2). The code is more readable, and the method is easier to extend with polymorphism to support SquareObject in the future. Property settings for CircleObject can also be simplified.-
paintCircleInfo() re-creates labels and edits the box every time Redraw Cirlce is clicked. This can be a performance issue, so usually these items are only created in the first pass.-
private static String intersectAnswer = ...; is good because you use intersectAnswer as a constant. It can be better by adding modifier final because you are not going to change it. Meanwhile, naming convention for constant in Java would capitalize letters and words are separated with under score, i.e., private static final String INTERSECT_ANWSER = ... is better.-
Try to remove modifier
static from private static Label intersect;. You may refer to: Why are static variables considered evil?Code Snippets
CircleInfo(String circleLabel, double centerX, double centerY, double radius)CircleInfo(String circleLabel, CircleObject obj)Context
StackExchange Code Review Q#58419, answer score: 7
Revisions (0)
No revisions yet.