/*
* 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 Chapter15.EndofChapter15.Exerc15dot16;
/**
*
* @author Rogerio Biscaia
*/
/*
(Two movable vertices and their distances)
Write a program that displays
two circles with radius 10 at
location (40, 40) and (120, 150)
with a line connecting the two circles, as shown in Figure.
The distance between the circles
is displayed along the line.
The user can drag a circle.
When that happens, the
circle and its line are moved and
the distance between the circles is updated.
*/
import java.util.*;
import java.math.*;
import javafx.event.Event;
import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.scene.Node;
import javafx.stage.Stage;
import javafx.scene.shape.Line;
import javafx.scene.shape.Circle;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.MouseButton;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
public class MoveTwoCircles extends Application {
private Circle circle1;
private Circle circle2;
private Line lineConnect;
private final int CIRCLESRADIUS = 10;
private ObservableList <Node> listNodes;
@Override
public void start(Stage primaryStage) throws Exception {
int maxX;
int maxY;
double circle1CenterX, circle1CenterY;
double circle2CenterX, circle2CenterY;
maxX = 301;
maxY = 301;
circle1CenterX = 40.0;
circle1CenterY = 40.0;
System.out.println("The circle1 has center X " + circle1CenterX + " and Y " + circle1CenterY);
circle2CenterX = 120.0;
circle2CenterY = 150.0;
System.out.println("The circle2 has center X " + circle2CenterX + " and Y " + circle2CenterY);
/*
Create the two Circles
*/
circle1 = new Circle( circle1CenterX, circle1CenterY, CIRCLESRADIUS );
circle1.setStroke(Color.BLACK);
circle1.setFill(null);
circle2 = new Circle( circle2CenterX, circle2CenterY, CIRCLESRADIUS );
circle2.setStroke(Color.BLACK);
circle2.setFill(null);
/*
Create the line connecting the
two circles
*/
lineConnect = getLine(circle1CenterX, circle1CenterY, circle2CenterX, circle2CenterY);
/*
Create the pane
*/
Pane pane = new Pane();
/*
Place the Circles and Line
in the pane
*/
pane.getChildren().clear();
pane.getChildren().addAll( circle1, circle2, lineConnect );
//listNodes = pane.getChildren();
//listNodes.add(circle1);
/*
Create the handler
for dragging Circle1
*/
circle1.setOnMouseDragged( event -> {
double getX;
double getY;
getX = event.getX();
getY = event.getY();
System.out.println("Clicked Circle in Pane");
System.out.println( "Button: " + event.getButton() );
System.out.println("Event To String: " + event.toString() );
System.out.println("Event Target: " + event.getTarget() );
circle1.setCenterX(getX);
circle1.setCenterY(getY);
System.out.println("Updated the Circle 1 center x to " + getX + " and center y to " + getY);
} ); //end of circle1.setOnMouseDragged( e -> {} );
/*
Create the handler
for dragging Circle2
*/
/*
Create a scene and place it in the Stage
*/
Scene scene = new Scene(pane, maxX + 50, maxY + 50);
primaryStage.setTitle("Display Two Circles And Line");
primaryStage.setScene(scene);
primaryStage.show();
} //end of the method public void start(Stage primaryStage)
/*
main
The main method is only needed for the IDE with
limited JavaFX support.
Not needed for running from the
command line
*/
public static void main (String[] args) {
try {
Application.launch(args);
} catch(Exception ex1) {
System.out.println("Exception in the main in the " +
"class MoveTwoCircles");
System.out.println(ex1.toString());
}
//Application.launch(args);
} //end of main
/*
Create the line connecting the
two circles
*/
public Line getLine( double circle1CenterX, double circle1CenterY, double circle2CenterX, double circle2CenterY ) {
double lowerCircleX, lowerCircleY;
double higherCircleX, higherCircleY;
double angleToXAxis;
double anglePointLowerCircle;
double pointLowerCircleX;
double pointLowerCircleY;
double anglePointHigherCircle;
double pointHigherCircleX;
double pointHigherCircleY;
Rational slope;
Rational slopeMultiplyX;
Rational b;
angleToXAxis = 0.0;
/*
We have to calculate the
linear equation for the line
Enter x1, y1, x2, y2, x3, y3, x4, y4: 2 2 5 -1.0 4.0 2.0 -1.0 -2.0
Slope = (rise) / (run)
Slope = (y2 - y1) / (x2 - x1)
(-1 - 2) / (5 - 2)
(-3) / (3)
-1
y = mx + b
m = Slope
2 = (-1) * 2 + b
b = 4
y = (-1) * x + 4
*/
//slope = (randomStartY - randomEndY) / ( randomStartX - randomEndX );
try {
slope = new Rational();
b = new Rational();
Rational slopeNumerator = Rational.getFraction(circle1CenterY - circle2CenterY);
//System.out.println("After creating the Rational for slopeNumerator");
Rational slopeDenominator = Rational.getFraction( circle1CenterX - circle2CenterX );
//System.out.println("After creating the Rational for slopeDenominator");
Rational lineStartX = Rational.getFraction(circle1CenterX);
Rational lineStartY = Rational.getFraction(circle1CenterY);
slope = slopeNumerator.divide(slopeDenominator);
slopeMultiplyX = slope.multiply(lineStartX);
b = lineStartY.subtract(slopeMultiplyX);
System.out.println("The Linear Equation for this line is y = " + slope + " * x + " + b);
angleToXAxis = Math.toDegrees(Math.atan( slope.doubleValue() ) );
} catch(Exception ex1) {
System.out.println("Exception when trying to create a Rational Object " +
" in the class MoveTwoCircles");
System.out.println(ex1.toString());
}
/*
Now that we have the linear equation for the
line
If one of the lines of the
angle between two lines is y= mx + b and
the other line is the x-axis, then
θ= Tan m elevated to -1.
*/
System.out.println("The angle between the line between two circles and the X Axis is " + angleToXAxis);
/*
Define the Lower Circle
that has the Higher Y
*/
if(circle1CenterY > circle2CenterY) {
lowerCircleX = circle1CenterX;
lowerCircleY = circle1CenterY;
higherCircleX = circle2CenterX;
higherCircleY = circle2CenterY;
} else {
lowerCircleX = circle2CenterX;
lowerCircleY = circle2CenterY;
higherCircleX = circle1CenterX;
higherCircleY = circle1CenterY;
}
/*
Get the Starting points for the
line on the lower circle and
higher circle
*/
anglePointLowerCircle = 0;
anglePointHigherCircle = 0;
if(angleToXAxis < 0) {
anglePointLowerCircle = 90 - Math.abs(angleToXAxis);
anglePointHigherCircle = 270 - Math.abs(angleToXAxis);
} else {
anglePointLowerCircle = 0 - (90 - angleToXAxis);
anglePointHigherCircle = 180 - (90 - angleToXAxis);
}
pointLowerCircleX = lowerCircleX + (CIRCLESRADIUS * Math.sin( Math.toRadians( anglePointLowerCircle ) ) );
pointLowerCircleY = lowerCircleY - (CIRCLESRADIUS * Math.cos( Math.toRadians( anglePointLowerCircle ) ) );
pointHigherCircleX = higherCircleX + (CIRCLESRADIUS * Math.sin( Math.toRadians( anglePointHigherCircle ) ) );
pointHigherCircleY = higherCircleY - (CIRCLESRADIUS * Math.cos( Math.toRadians( anglePointHigherCircle ) ) );
/*
Create the
line Connecting the Two circles
but not
entering inside the circles
*/
lineConnect = new Line(pointLowerCircleX, pointLowerCircleY, pointHigherCircleX, pointHigherCircleY);
lineConnect.setStroke(Color.BLUE);
return lineConnect;
} //end of the method public Line getLine( double circle1CenterX, double circle1CenterY, double circle2CenterX, double circle2CenterY )
} //end of hte class MoveTwoCircles