SlideShare a Scribd company logo
1 of 10
Download to read offline
package chapter15;
import javafx.application.Application;
import javafx.beans.property.DoubleProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;
/**
* Java Programming: Comprehensive (10e); Liang; Pearson 2014
*
* *15.21(Drag points) Draw a circle with three random points on the circle. Connect
* the points to form a triangle. Display the angles in the triangle. Use the mouse
* to drag a point along the perimeter of the circle. As you drag it, the triangle and
* angles are redisplayed dynamically, as shown in Figure 15.30b. For computing
* angles in a triangle, see Listing 4.1.
*
* {@code A = Math.acos((a * a - b * b - c * c) / (-2 * b * c))}
* {@code B = Math.acos((b * b - a * a - c * c) / (-2 * a * c))}
* {@code C = Math.acos((c * c - b * b - a * a) / (-2 * a * b))}
*
* @author ncoop
*/
@SuppressWarnings("WeakerAccess")
public class DragPoints extends Application {
@Override
public void start(Stage primaryStage) {
final PointPane pane = new PointPane(640, 480);
pane.setStyle("-fx-background-color: wheat;");
Label label = new Label("Click and drag the points.");
BorderPane borderPane = new BorderPane(pane);
BorderPane.setAlignment(label, Pos.CENTER);
label.setPadding(new Insets(5));
borderPane.setBottom(label);
Scene scene = new Scene(borderPane);
primaryStage.setTitle("Exercise15_21");
primaryStage.setScene(scene);
primaryStage.show();
}
private class PointPane extends Pane {
final Circle circle = new Circle();
final Vertex[] v = new Vertex[3];
final int strokeWidth = 2;
final Color circleStroke = Color.GRAY, legStroke = Color.BLACK;
@SuppressWarnings("SameParameterValue")
PointPane(double w, double h) {
this.setPrefSize(w, h);
this.setWidth(w);
this.setHeight(h);
circle.setStroke(circleStroke);
circle.setFill(Color.TRANSPARENT);
circle.setStrokeWidth(strokeWidth);
circle.radiusProperty().bind(this.heightProperty().multiply(0.4));
circle.centerXProperty().bind(this.widthProperty().divide(2));
circle.centerYProperty().bind(this.heightProperty().divide(2));
this.getChildren().add(circle);
// create the vertices at random angles
for (int i = 0; i < v.length; i++) {
v[i] = new Vertex(circle, 2 * Math.PI / v.length * (i + Math.random()));
v[i].radiusProperty().bind(circle.radiusProperty().divide(10));
v[i].setPosition();
v[i].setStroke(new Color(i == 0 ? 1 : 0, i == 1 ? 1 : 0, i == 2 ? 1 : 0, 1));
v[i].setFill(Color.TRANSPARENT);
v[i].setStrokeWidth(strokeWidth);
this.getChildren().add(v[i]);
v[i].setOnMouseDragged(new EventHandler() {
@Override
public void handle(MouseEvent event) {
int i;
for (i = 0; i < v.length; i++)
if (v[i] == event.getSource())
break;
v[i].setAngle(event.getX(), event.getY());
moveUpdate((Vertex) event.getSource());
}
});
}
// create the legs and bind their endpoints to their corresponding vertices
for (int i = 0; i < v.length; i++) {
int j = i + 1 < v.length ? i + 1 : 0;
int k = j + 1 < v.length ? j + 1 : 0;
v[i].bindLeg(v[j], v[k]);
v[i].leg.setStroke(legStroke);
v[i].leg.setStrokeWidth(strokeWidth);
this.getChildren().add(v[i].leg);
// create the text nodes and bind their locations to their corresponding vertices
this.getChildren().add(v[i].text);
}
for(DoubleProperty p: new DoubleProperty[]
{circle.radiusProperty(), circle.centerXProperty(), circle.centerYProperty()})
p.addListener(new ResizeListener());
moveUpdate(v[0]);
}
void moveUpdate(Vertex vert) {
vert.setPosition();
double[] legLength = new double[3];
for (int i = 0; i < v.length; i++)
legLength[i] = v[i].getLegLength();
for (int i = 0; i < v.length; i++) {
int j = i + 1 < v.length ? i + 1 : 0;
int k = j + 1 < v.length ? j + 1 : 0;
double a = legLength[i], b = legLength[j], c = legLength[k];
double d = Math.toDegrees(Math.acos((a * a - b * b - c * c) / (-2 * b * c)));
v[i].setText(d);
}
}
private class ResizeListener implements ChangeListener {
@Override
public void changed(ObservableValue observableValue, Number oldWidth, Number
newWidth) {
for (Vertex aV : v) {
aV.setPosition();
}
}
}
}
private class Vertex extends Circle {
final Circle circle;
final Line leg;
final Text text;
double centerAngle;
Vertex(Circle circle, double centerAngle) {
this.circle = circle;
this.setAngle(centerAngle);
this.leg = new Line();
this.text = new Text();
this.text.setFont(Font.font(20));
this.text.setStroke(Color.BLACK);
this.text.setTextAlignment(TextAlignment.CENTER);
this.text.xProperty().bind(this.centerXProperty().add(25));
this.text.yProperty().bind(this.centerYProperty().subtract(10));
}
double getCenterAngle() {return this.centerAngle;}
void setPosition() {
this.setCenterX(circle.getCenterX() + circle.getRadius() *
Math.cos(this.getCenterAngle()));
this.setCenterY(circle.getCenterY() + circle.getRadius() *
Math.sin(this.getCenterAngle()));
}
void setAngle(double centerAngle) {
this.centerAngle = centerAngle;
}
void setAngle(double x, double y) {
this.setAngle(Math.atan2(y - circle.getCenterY(), x - circle.getCenterX()));
}
void bindLeg(Vertex v1, Vertex v2) {
leg.startXProperty().bind(v1.centerXProperty());
leg.startYProperty().bind(v1.centerYProperty());
leg.endXProperty().bind(v2.centerXProperty());
leg.endYProperty().bind(v2.centerYProperty());
}
double getLegLength() {
return Math.sqrt(Math.pow(leg.getStartX()-leg.getEndX(),2) +
Math.pow(leg.getStartY()-leg.getEndY(),2));
}
void setText(double angle) {
this.text.setText(String.format("%.0fu00B0", angle));
}
}
}
Solution
package chapter15;
import javafx.application.Application;
import javafx.beans.property.DoubleProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;
/**
* Java Programming: Comprehensive (10e); Liang; Pearson 2014
*
* *15.21(Drag points) Draw a circle with three random points on the circle. Connect
* the points to form a triangle. Display the angles in the triangle. Use the mouse
* to drag a point along the perimeter of the circle. As you drag it, the triangle and
* angles are redisplayed dynamically, as shown in Figure 15.30b. For computing
* angles in a triangle, see Listing 4.1.
*
* {@code A = Math.acos((a * a - b * b - c * c) / (-2 * b * c))}
* {@code B = Math.acos((b * b - a * a - c * c) / (-2 * a * c))}
* {@code C = Math.acos((c * c - b * b - a * a) / (-2 * a * b))}
*
* @author ncoop
*/
@SuppressWarnings("WeakerAccess")
public class DragPoints extends Application {
@Override
public void start(Stage primaryStage) {
final PointPane pane = new PointPane(640, 480);
pane.setStyle("-fx-background-color: wheat;");
Label label = new Label("Click and drag the points.");
BorderPane borderPane = new BorderPane(pane);
BorderPane.setAlignment(label, Pos.CENTER);
label.setPadding(new Insets(5));
borderPane.setBottom(label);
Scene scene = new Scene(borderPane);
primaryStage.setTitle("Exercise15_21");
primaryStage.setScene(scene);
primaryStage.show();
}
private class PointPane extends Pane {
final Circle circle = new Circle();
final Vertex[] v = new Vertex[3];
final int strokeWidth = 2;
final Color circleStroke = Color.GRAY, legStroke = Color.BLACK;
@SuppressWarnings("SameParameterValue")
PointPane(double w, double h) {
this.setPrefSize(w, h);
this.setWidth(w);
this.setHeight(h);
circle.setStroke(circleStroke);
circle.setFill(Color.TRANSPARENT);
circle.setStrokeWidth(strokeWidth);
circle.radiusProperty().bind(this.heightProperty().multiply(0.4));
circle.centerXProperty().bind(this.widthProperty().divide(2));
circle.centerYProperty().bind(this.heightProperty().divide(2));
this.getChildren().add(circle);
// create the vertices at random angles
for (int i = 0; i < v.length; i++) {
v[i] = new Vertex(circle, 2 * Math.PI / v.length * (i + Math.random()));
v[i].radiusProperty().bind(circle.radiusProperty().divide(10));
v[i].setPosition();
v[i].setStroke(new Color(i == 0 ? 1 : 0, i == 1 ? 1 : 0, i == 2 ? 1 : 0, 1));
v[i].setFill(Color.TRANSPARENT);
v[i].setStrokeWidth(strokeWidth);
this.getChildren().add(v[i]);
v[i].setOnMouseDragged(new EventHandler() {
@Override
public void handle(MouseEvent event) {
int i;
for (i = 0; i < v.length; i++)
if (v[i] == event.getSource())
break;
v[i].setAngle(event.getX(), event.getY());
moveUpdate((Vertex) event.getSource());
}
});
}
// create the legs and bind their endpoints to their corresponding vertices
for (int i = 0; i < v.length; i++) {
int j = i + 1 < v.length ? i + 1 : 0;
int k = j + 1 < v.length ? j + 1 : 0;
v[i].bindLeg(v[j], v[k]);
v[i].leg.setStroke(legStroke);
v[i].leg.setStrokeWidth(strokeWidth);
this.getChildren().add(v[i].leg);
// create the text nodes and bind their locations to their corresponding vertices
this.getChildren().add(v[i].text);
}
for(DoubleProperty p: new DoubleProperty[]
{circle.radiusProperty(), circle.centerXProperty(), circle.centerYProperty()})
p.addListener(new ResizeListener());
moveUpdate(v[0]);
}
void moveUpdate(Vertex vert) {
vert.setPosition();
double[] legLength = new double[3];
for (int i = 0; i < v.length; i++)
legLength[i] = v[i].getLegLength();
for (int i = 0; i < v.length; i++) {
int j = i + 1 < v.length ? i + 1 : 0;
int k = j + 1 < v.length ? j + 1 : 0;
double a = legLength[i], b = legLength[j], c = legLength[k];
double d = Math.toDegrees(Math.acos((a * a - b * b - c * c) / (-2 * b * c)));
v[i].setText(d);
}
}
private class ResizeListener implements ChangeListener {
@Override
public void changed(ObservableValue observableValue, Number oldWidth, Number
newWidth) {
for (Vertex aV : v) {
aV.setPosition();
}
}
}
}
private class Vertex extends Circle {
final Circle circle;
final Line leg;
final Text text;
double centerAngle;
Vertex(Circle circle, double centerAngle) {
this.circle = circle;
this.setAngle(centerAngle);
this.leg = new Line();
this.text = new Text();
this.text.setFont(Font.font(20));
this.text.setStroke(Color.BLACK);
this.text.setTextAlignment(TextAlignment.CENTER);
this.text.xProperty().bind(this.centerXProperty().add(25));
this.text.yProperty().bind(this.centerYProperty().subtract(10));
}
double getCenterAngle() {return this.centerAngle;}
void setPosition() {
this.setCenterX(circle.getCenterX() + circle.getRadius() *
Math.cos(this.getCenterAngle()));
this.setCenterY(circle.getCenterY() + circle.getRadius() *
Math.sin(this.getCenterAngle()));
}
void setAngle(double centerAngle) {
this.centerAngle = centerAngle;
}
void setAngle(double x, double y) {
this.setAngle(Math.atan2(y - circle.getCenterY(), x - circle.getCenterX()));
}
void bindLeg(Vertex v1, Vertex v2) {
leg.startXProperty().bind(v1.centerXProperty());
leg.startYProperty().bind(v1.centerYProperty());
leg.endXProperty().bind(v2.centerXProperty());
leg.endYProperty().bind(v2.centerYProperty());
}
double getLegLength() {
return Math.sqrt(Math.pow(leg.getStartX()-leg.getEndX(),2) +
Math.pow(leg.getStartY()-leg.getEndY(),2));
}
void setText(double angle) {
this.text.setText(String.format("%.0fu00B0", angle));
}
}
}

More Related Content

Similar to package chapter15;import javafx.application.Application;import j.pdf

Reactive programming with RxJS - ByteConf 2018
Reactive programming with RxJS - ByteConf 2018Reactive programming with RxJS - ByteConf 2018
Reactive programming with RxJS - ByteConf 2018Tracy Lee
 
Please implement in Java. comments would be appreciated 5.pdf
Please implement in Java. comments would be appreciated 5.pdfPlease implement in Java. comments would be appreciated 5.pdf
Please implement in Java. comments would be appreciated 5.pdffms12345
 
ES6 patterns in the wild
ES6 patterns in the wildES6 patterns in the wild
ES6 patterns in the wildJoe Morgan
 
Import java.awt.; Import acm.program.; Import acm.graphics.;.pdf
Import java.awt.; Import acm.program.; Import acm.graphics.;.pdfImport java.awt.; Import acm.program.; Import acm.graphics.;.pdf
Import java.awt.; Import acm.program.; Import acm.graphics.;.pdfapexcomputer54
 
JavaFX 2.0 With Alternative Languages - JavaOne 2011
JavaFX 2.0 With Alternative Languages - JavaOne 2011JavaFX 2.0 With Alternative Languages - JavaOne 2011
JavaFX 2.0 With Alternative Languages - JavaOne 2011Stephen Chin
 
code for quiz in my sql
code for quiz  in my sql code for quiz  in my sql
code for quiz in my sql JOYITAKUNDU1
 
Creating an Uber Clone - Part VIII - Transcript.pdf
Creating an Uber Clone - Part VIII - Transcript.pdfCreating an Uber Clone - Part VIII - Transcript.pdf
Creating an Uber Clone - Part VIII - Transcript.pdfShaiAlmog1
 
openFrameworks – パーティクルを動かす、静的配列と動的配列 - 多摩美メディアアートII
openFrameworks – パーティクルを動かす、静的配列と動的配列 - 多摩美メディアアートIIopenFrameworks – パーティクルを動かす、静的配列と動的配列 - 多摩美メディアアートII
openFrameworks – パーティクルを動かす、静的配列と動的配列 - 多摩美メディアアートIIAtsushi Tadokoro
 
The Ring programming language version 1.5.1 book - Part 79 of 180
The Ring programming language version 1.5.1 book - Part 79 of 180The Ring programming language version 1.5.1 book - Part 79 of 180
The Ring programming language version 1.5.1 book - Part 79 of 180Mahmoud Samir Fayed
 
I wanna add the shape creator like rectangular , square , circle etc.pdf
I wanna add the shape creator like rectangular , square , circle etc.pdfI wanna add the shape creator like rectangular , square , circle etc.pdf
I wanna add the shape creator like rectangular , square , circle etc.pdfherminaherman
 
The Ring programming language version 1.9 book - Part 96 of 210
The Ring programming language version 1.9 book - Part 96 of 210The Ring programming language version 1.9 book - Part 96 of 210
The Ring programming language version 1.9 book - Part 96 of 210Mahmoud Samir Fayed
 
Go: It's Not Just For Google
Go: It's Not Just For GoogleGo: It's Not Just For Google
Go: It's Not Just For GoogleEleanor McHugh
 
Raspberry Pi à la GroovyFX
Raspberry Pi à la GroovyFXRaspberry Pi à la GroovyFX
Raspberry Pi à la GroovyFXStephen Chin
 
openFrameworks 007 - 3D
openFrameworks 007 - 3DopenFrameworks 007 - 3D
openFrameworks 007 - 3Droxlu
 
can you add a delete button and a add button to the below program. j.pdf
can you add a delete button and a add button to the below program. j.pdfcan you add a delete button and a add button to the below program. j.pdf
can you add a delete button and a add button to the below program. j.pdfsales88
 
Mashup caravan android-talks
Mashup caravan android-talksMashup caravan android-talks
Mashup caravan android-talkshonjo2
 

Similar to package chapter15;import javafx.application.Application;import j.pdf (20)

Reactive programming with RxJS - ByteConf 2018
Reactive programming with RxJS - ByteConf 2018Reactive programming with RxJS - ByteConf 2018
Reactive programming with RxJS - ByteConf 2018
 
Applications
ApplicationsApplications
Applications
 
Please implement in Java. comments would be appreciated 5.pdf
Please implement in Java. comments would be appreciated 5.pdfPlease implement in Java. comments would be appreciated 5.pdf
Please implement in Java. comments would be appreciated 5.pdf
 
ES6 patterns in the wild
ES6 patterns in the wildES6 patterns in the wild
ES6 patterns in the wild
 
662305 11
662305 11662305 11
662305 11
 
Import java.awt.; Import acm.program.; Import acm.graphics.;.pdf
Import java.awt.; Import acm.program.; Import acm.graphics.;.pdfImport java.awt.; Import acm.program.; Import acm.graphics.;.pdf
Import java.awt.; Import acm.program.; Import acm.graphics.;.pdf
 
JavaFX 2.0 With Alternative Languages - JavaOne 2011
JavaFX 2.0 With Alternative Languages - JavaOne 2011JavaFX 2.0 With Alternative Languages - JavaOne 2011
JavaFX 2.0 With Alternative Languages - JavaOne 2011
 
code for quiz in my sql
code for quiz  in my sql code for quiz  in my sql
code for quiz in my sql
 
662305 10
662305 10662305 10
662305 10
 
Creating an Uber Clone - Part VIII - Transcript.pdf
Creating an Uber Clone - Part VIII - Transcript.pdfCreating an Uber Clone - Part VIII - Transcript.pdf
Creating an Uber Clone - Part VIII - Transcript.pdf
 
openFrameworks – パーティクルを動かす、静的配列と動的配列 - 多摩美メディアアートII
openFrameworks – パーティクルを動かす、静的配列と動的配列 - 多摩美メディアアートIIopenFrameworks – パーティクルを動かす、静的配列と動的配列 - 多摩美メディアアートII
openFrameworks – パーティクルを動かす、静的配列と動的配列 - 多摩美メディアアートII
 
The Ring programming language version 1.5.1 book - Part 79 of 180
The Ring programming language version 1.5.1 book - Part 79 of 180The Ring programming language version 1.5.1 book - Part 79 of 180
The Ring programming language version 1.5.1 book - Part 79 of 180
 
I wanna add the shape creator like rectangular , square , circle etc.pdf
I wanna add the shape creator like rectangular , square , circle etc.pdfI wanna add the shape creator like rectangular , square , circle etc.pdf
I wanna add the shape creator like rectangular , square , circle etc.pdf
 
The Ring programming language version 1.9 book - Part 96 of 210
The Ring programming language version 1.9 book - Part 96 of 210The Ring programming language version 1.9 book - Part 96 of 210
The Ring programming language version 1.9 book - Part 96 of 210
 
Go: It's Not Just For Google
Go: It's Not Just For GoogleGo: It's Not Just For Google
Go: It's Not Just For Google
 
Raspberry Pi à la GroovyFX
Raspberry Pi à la GroovyFXRaspberry Pi à la GroovyFX
Raspberry Pi à la GroovyFX
 
openFrameworks 007 - 3D
openFrameworks 007 - 3DopenFrameworks 007 - 3D
openFrameworks 007 - 3D
 
003 - JavaFX Tutorial - Layouts
003 - JavaFX Tutorial - Layouts003 - JavaFX Tutorial - Layouts
003 - JavaFX Tutorial - Layouts
 
can you add a delete button and a add button to the below program. j.pdf
can you add a delete button and a add button to the below program. j.pdfcan you add a delete button and a add button to the below program. j.pdf
can you add a delete button and a add button to the below program. j.pdf
 
Mashup caravan android-talks
Mashup caravan android-talksMashup caravan android-talks
Mashup caravan android-talks
 

More from KARTIKINDIA

We first see what is the Small Intestine – It connects the stomach a.pdf
We first see what is the Small Intestine – It connects the stomach a.pdfWe first see what is the Small Intestine – It connects the stomach a.pdf
We first see what is the Small Intestine – It connects the stomach a.pdfKARTIKINDIA
 
Water needs to be present. The 2 compounds will only react after.pdf
Water needs to be present. The 2 compounds will only react after.pdfWater needs to be present. The 2 compounds will only react after.pdf
Water needs to be present. The 2 compounds will only react after.pdfKARTIKINDIA
 
True.SolutionTrue..pdf
True.SolutionTrue..pdfTrue.SolutionTrue..pdf
True.SolutionTrue..pdfKARTIKINDIA
 
1) option d is the answer, i.e. 11Solution1) option d is the a.pdf
1) option d is the answer, i.e. 11Solution1) option d is the a.pdf1) option d is the answer, i.e. 11Solution1) option d is the a.pdf
1) option d is the answer, i.e. 11Solution1) option d is the a.pdfKARTIKINDIA
 
The same Mechanisms of genetic exchange in bacteria 1. Conjugation .pdf
The same Mechanisms of genetic exchange in bacteria 1. Conjugation .pdfThe same Mechanisms of genetic exchange in bacteria 1. Conjugation .pdf
The same Mechanisms of genetic exchange in bacteria 1. Conjugation .pdfKARTIKINDIA
 
The answer is b) facilitates O2 diffusion through alveolar membrane.pdf
The answer is b) facilitates O2 diffusion through alveolar membrane.pdfThe answer is b) facilitates O2 diffusion through alveolar membrane.pdf
The answer is b) facilitates O2 diffusion through alveolar membrane.pdfKARTIKINDIA
 
The cam material will be UHMW polyethylene because it had the lost c.pdf
The cam material will be UHMW polyethylene because it had the lost c.pdfThe cam material will be UHMW polyethylene because it had the lost c.pdf
The cam material will be UHMW polyethylene because it had the lost c.pdfKARTIKINDIA
 
tan(x)=1x=pi4Solutiontan(x)=1x=pi4.pdf
tan(x)=1x=pi4Solutiontan(x)=1x=pi4.pdftan(x)=1x=pi4Solutiontan(x)=1x=pi4.pdf
tan(x)=1x=pi4Solutiontan(x)=1x=pi4.pdfKARTIKINDIA
 
Ques-1 antinormative collective pro-social behaviorReasonOnlin.pdf
Ques-1 antinormative collective pro-social behaviorReasonOnlin.pdfQues-1 antinormative collective pro-social behaviorReasonOnlin.pdf
Ques-1 antinormative collective pro-social behaviorReasonOnlin.pdfKARTIKINDIA
 
Physical properties can be observed or measured without changing the.pdf
Physical properties can be observed or measured without changing the.pdfPhysical properties can be observed or measured without changing the.pdf
Physical properties can be observed or measured without changing the.pdfKARTIKINDIA
 
(E) a+ and b+ are more closely linked than a+ and c+.   If two gen.pdf
(E) a+ and b+ are more closely linked than a+ and c+.   If two gen.pdf(E) a+ and b+ are more closely linked than a+ and c+.   If two gen.pdf
(E) a+ and b+ are more closely linked than a+ and c+.   If two gen.pdfKARTIKINDIA
 
Please follow the data and description 1) An association indicate.pdf
Please follow the data and description 1) An association indicate.pdfPlease follow the data and description 1) An association indicate.pdf
Please follow the data and description 1) An association indicate.pdfKARTIKINDIA
 
Mononucleotides are monomer of polynucleotides. Its three uses are-.pdf
Mononucleotides are monomer of polynucleotides. Its three uses are-.pdfMononucleotides are monomer of polynucleotides. Its three uses are-.pdf
Mononucleotides are monomer of polynucleotides. Its three uses are-.pdfKARTIKINDIA
 
Intelligence comes from the Latin verb intellegere, which means .pdf
Intelligence comes from the Latin verb intellegere, which means .pdfIntelligence comes from the Latin verb intellegere, which means .pdf
Intelligence comes from the Latin verb intellegere, which means .pdfKARTIKINDIA
 
Initial compromise is the method that is adopted by intruders to ent.pdf
Initial compromise is the method that is adopted by intruders to ent.pdfInitial compromise is the method that is adopted by intruders to ent.pdf
Initial compromise is the method that is adopted by intruders to ent.pdfKARTIKINDIA
 
I agree .Ethernet nodes listen to the medium when they want to tra.pdf
I agree .Ethernet nodes listen to the medium when they want to tra.pdfI agree .Ethernet nodes listen to the medium when they want to tra.pdf
I agree .Ethernet nodes listen to the medium when they want to tra.pdfKARTIKINDIA
 
A. 1.Virus modified exoxomes are specialized form of nano sized vesi.pdf
A. 1.Virus modified exoxomes are specialized form of nano sized vesi.pdfA. 1.Virus modified exoxomes are specialized form of nano sized vesi.pdf
A. 1.Virus modified exoxomes are specialized form of nano sized vesi.pdfKARTIKINDIA
 
Hi,pease find ansers for Questions1.5 Fill in the Blanksa) The.pdf
Hi,pease find ansers for Questions1.5 Fill in the Blanksa) The.pdfHi,pease find ansers for Questions1.5 Fill in the Blanksa) The.pdf
Hi,pease find ansers for Questions1.5 Fill in the Blanksa) The.pdfKARTIKINDIA
 
(1)White matter in the cerebellumThe arbor vitae refers to the c.pdf
(1)White matter in the cerebellumThe arbor vitae refers to the c.pdf(1)White matter in the cerebellumThe arbor vitae refers to the c.pdf
(1)White matter in the cerebellumThe arbor vitae refers to the c.pdfKARTIKINDIA
 
Debt ratio=Debttotal assetsHence debt=0.55total assetsHence tot.pdf
Debt ratio=Debttotal assetsHence debt=0.55total assetsHence tot.pdfDebt ratio=Debttotal assetsHence debt=0.55total assetsHence tot.pdf
Debt ratio=Debttotal assetsHence debt=0.55total assetsHence tot.pdfKARTIKINDIA
 

More from KARTIKINDIA (20)

We first see what is the Small Intestine – It connects the stomach a.pdf
We first see what is the Small Intestine – It connects the stomach a.pdfWe first see what is the Small Intestine – It connects the stomach a.pdf
We first see what is the Small Intestine – It connects the stomach a.pdf
 
Water needs to be present. The 2 compounds will only react after.pdf
Water needs to be present. The 2 compounds will only react after.pdfWater needs to be present. The 2 compounds will only react after.pdf
Water needs to be present. The 2 compounds will only react after.pdf
 
True.SolutionTrue..pdf
True.SolutionTrue..pdfTrue.SolutionTrue..pdf
True.SolutionTrue..pdf
 
1) option d is the answer, i.e. 11Solution1) option d is the a.pdf
1) option d is the answer, i.e. 11Solution1) option d is the a.pdf1) option d is the answer, i.e. 11Solution1) option d is the a.pdf
1) option d is the answer, i.e. 11Solution1) option d is the a.pdf
 
The same Mechanisms of genetic exchange in bacteria 1. Conjugation .pdf
The same Mechanisms of genetic exchange in bacteria 1. Conjugation .pdfThe same Mechanisms of genetic exchange in bacteria 1. Conjugation .pdf
The same Mechanisms of genetic exchange in bacteria 1. Conjugation .pdf
 
The answer is b) facilitates O2 diffusion through alveolar membrane.pdf
The answer is b) facilitates O2 diffusion through alveolar membrane.pdfThe answer is b) facilitates O2 diffusion through alveolar membrane.pdf
The answer is b) facilitates O2 diffusion through alveolar membrane.pdf
 
The cam material will be UHMW polyethylene because it had the lost c.pdf
The cam material will be UHMW polyethylene because it had the lost c.pdfThe cam material will be UHMW polyethylene because it had the lost c.pdf
The cam material will be UHMW polyethylene because it had the lost c.pdf
 
tan(x)=1x=pi4Solutiontan(x)=1x=pi4.pdf
tan(x)=1x=pi4Solutiontan(x)=1x=pi4.pdftan(x)=1x=pi4Solutiontan(x)=1x=pi4.pdf
tan(x)=1x=pi4Solutiontan(x)=1x=pi4.pdf
 
Ques-1 antinormative collective pro-social behaviorReasonOnlin.pdf
Ques-1 antinormative collective pro-social behaviorReasonOnlin.pdfQues-1 antinormative collective pro-social behaviorReasonOnlin.pdf
Ques-1 antinormative collective pro-social behaviorReasonOnlin.pdf
 
Physical properties can be observed or measured without changing the.pdf
Physical properties can be observed or measured without changing the.pdfPhysical properties can be observed or measured without changing the.pdf
Physical properties can be observed or measured without changing the.pdf
 
(E) a+ and b+ are more closely linked than a+ and c+.   If two gen.pdf
(E) a+ and b+ are more closely linked than a+ and c+.   If two gen.pdf(E) a+ and b+ are more closely linked than a+ and c+.   If two gen.pdf
(E) a+ and b+ are more closely linked than a+ and c+.   If two gen.pdf
 
Please follow the data and description 1) An association indicate.pdf
Please follow the data and description 1) An association indicate.pdfPlease follow the data and description 1) An association indicate.pdf
Please follow the data and description 1) An association indicate.pdf
 
Mononucleotides are monomer of polynucleotides. Its three uses are-.pdf
Mononucleotides are monomer of polynucleotides. Its three uses are-.pdfMononucleotides are monomer of polynucleotides. Its three uses are-.pdf
Mononucleotides are monomer of polynucleotides. Its three uses are-.pdf
 
Intelligence comes from the Latin verb intellegere, which means .pdf
Intelligence comes from the Latin verb intellegere, which means .pdfIntelligence comes from the Latin verb intellegere, which means .pdf
Intelligence comes from the Latin verb intellegere, which means .pdf
 
Initial compromise is the method that is adopted by intruders to ent.pdf
Initial compromise is the method that is adopted by intruders to ent.pdfInitial compromise is the method that is adopted by intruders to ent.pdf
Initial compromise is the method that is adopted by intruders to ent.pdf
 
I agree .Ethernet nodes listen to the medium when they want to tra.pdf
I agree .Ethernet nodes listen to the medium when they want to tra.pdfI agree .Ethernet nodes listen to the medium when they want to tra.pdf
I agree .Ethernet nodes listen to the medium when they want to tra.pdf
 
A. 1.Virus modified exoxomes are specialized form of nano sized vesi.pdf
A. 1.Virus modified exoxomes are specialized form of nano sized vesi.pdfA. 1.Virus modified exoxomes are specialized form of nano sized vesi.pdf
A. 1.Virus modified exoxomes are specialized form of nano sized vesi.pdf
 
Hi,pease find ansers for Questions1.5 Fill in the Blanksa) The.pdf
Hi,pease find ansers for Questions1.5 Fill in the Blanksa) The.pdfHi,pease find ansers for Questions1.5 Fill in the Blanksa) The.pdf
Hi,pease find ansers for Questions1.5 Fill in the Blanksa) The.pdf
 
(1)White matter in the cerebellumThe arbor vitae refers to the c.pdf
(1)White matter in the cerebellumThe arbor vitae refers to the c.pdf(1)White matter in the cerebellumThe arbor vitae refers to the c.pdf
(1)White matter in the cerebellumThe arbor vitae refers to the c.pdf
 
Debt ratio=Debttotal assetsHence debt=0.55total assetsHence tot.pdf
Debt ratio=Debttotal assetsHence debt=0.55total assetsHence tot.pdfDebt ratio=Debttotal assetsHence debt=0.55total assetsHence tot.pdf
Debt ratio=Debttotal assetsHence debt=0.55total assetsHence tot.pdf
 

Recently uploaded

ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 

Recently uploaded (20)

ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 

package chapter15;import javafx.application.Application;import j.pdf

  • 1. package chapter15; import javafx.application.Application; import javafx.beans.property.DoubleProperty; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.event.EventHandler; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.input.MouseEvent; import javafx.scene.layout.BorderPane; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.scene.shape.Line; import javafx.scene.text.Font; import javafx.scene.text.Text; import javafx.scene.text.TextAlignment; import javafx.stage.Stage; /** * Java Programming: Comprehensive (10e); Liang; Pearson 2014 * * *15.21(Drag points) Draw a circle with three random points on the circle. Connect * the points to form a triangle. Display the angles in the triangle. Use the mouse * to drag a point along the perimeter of the circle. As you drag it, the triangle and * angles are redisplayed dynamically, as shown in Figure 15.30b. For computing * angles in a triangle, see Listing 4.1. * * {@code A = Math.acos((a * a - b * b - c * c) / (-2 * b * c))} * {@code B = Math.acos((b * b - a * a - c * c) / (-2 * a * c))}
  • 2. * {@code C = Math.acos((c * c - b * b - a * a) / (-2 * a * b))} * * @author ncoop */ @SuppressWarnings("WeakerAccess") public class DragPoints extends Application { @Override public void start(Stage primaryStage) { final PointPane pane = new PointPane(640, 480); pane.setStyle("-fx-background-color: wheat;"); Label label = new Label("Click and drag the points."); BorderPane borderPane = new BorderPane(pane); BorderPane.setAlignment(label, Pos.CENTER); label.setPadding(new Insets(5)); borderPane.setBottom(label); Scene scene = new Scene(borderPane); primaryStage.setTitle("Exercise15_21"); primaryStage.setScene(scene); primaryStage.show(); } private class PointPane extends Pane { final Circle circle = new Circle(); final Vertex[] v = new Vertex[3]; final int strokeWidth = 2; final Color circleStroke = Color.GRAY, legStroke = Color.BLACK; @SuppressWarnings("SameParameterValue") PointPane(double w, double h) { this.setPrefSize(w, h); this.setWidth(w); this.setHeight(h); circle.setStroke(circleStroke); circle.setFill(Color.TRANSPARENT); circle.setStrokeWidth(strokeWidth); circle.radiusProperty().bind(this.heightProperty().multiply(0.4)); circle.centerXProperty().bind(this.widthProperty().divide(2)); circle.centerYProperty().bind(this.heightProperty().divide(2));
  • 3. this.getChildren().add(circle); // create the vertices at random angles for (int i = 0; i < v.length; i++) { v[i] = new Vertex(circle, 2 * Math.PI / v.length * (i + Math.random())); v[i].radiusProperty().bind(circle.radiusProperty().divide(10)); v[i].setPosition(); v[i].setStroke(new Color(i == 0 ? 1 : 0, i == 1 ? 1 : 0, i == 2 ? 1 : 0, 1)); v[i].setFill(Color.TRANSPARENT); v[i].setStrokeWidth(strokeWidth); this.getChildren().add(v[i]); v[i].setOnMouseDragged(new EventHandler() { @Override public void handle(MouseEvent event) { int i; for (i = 0; i < v.length; i++) if (v[i] == event.getSource()) break; v[i].setAngle(event.getX(), event.getY()); moveUpdate((Vertex) event.getSource()); } }); } // create the legs and bind their endpoints to their corresponding vertices for (int i = 0; i < v.length; i++) { int j = i + 1 < v.length ? i + 1 : 0; int k = j + 1 < v.length ? j + 1 : 0; v[i].bindLeg(v[j], v[k]); v[i].leg.setStroke(legStroke); v[i].leg.setStrokeWidth(strokeWidth); this.getChildren().add(v[i].leg); // create the text nodes and bind their locations to their corresponding vertices this.getChildren().add(v[i].text); } for(DoubleProperty p: new DoubleProperty[] {circle.radiusProperty(), circle.centerXProperty(), circle.centerYProperty()}) p.addListener(new ResizeListener());
  • 4. moveUpdate(v[0]); } void moveUpdate(Vertex vert) { vert.setPosition(); double[] legLength = new double[3]; for (int i = 0; i < v.length; i++) legLength[i] = v[i].getLegLength(); for (int i = 0; i < v.length; i++) { int j = i + 1 < v.length ? i + 1 : 0; int k = j + 1 < v.length ? j + 1 : 0; double a = legLength[i], b = legLength[j], c = legLength[k]; double d = Math.toDegrees(Math.acos((a * a - b * b - c * c) / (-2 * b * c))); v[i].setText(d); } } private class ResizeListener implements ChangeListener { @Override public void changed(ObservableValue observableValue, Number oldWidth, Number newWidth) { for (Vertex aV : v) { aV.setPosition(); } } } } private class Vertex extends Circle { final Circle circle; final Line leg; final Text text; double centerAngle; Vertex(Circle circle, double centerAngle) { this.circle = circle; this.setAngle(centerAngle); this.leg = new Line(); this.text = new Text(); this.text.setFont(Font.font(20));
  • 5. this.text.setStroke(Color.BLACK); this.text.setTextAlignment(TextAlignment.CENTER); this.text.xProperty().bind(this.centerXProperty().add(25)); this.text.yProperty().bind(this.centerYProperty().subtract(10)); } double getCenterAngle() {return this.centerAngle;} void setPosition() { this.setCenterX(circle.getCenterX() + circle.getRadius() * Math.cos(this.getCenterAngle())); this.setCenterY(circle.getCenterY() + circle.getRadius() * Math.sin(this.getCenterAngle())); } void setAngle(double centerAngle) { this.centerAngle = centerAngle; } void setAngle(double x, double y) { this.setAngle(Math.atan2(y - circle.getCenterY(), x - circle.getCenterX())); } void bindLeg(Vertex v1, Vertex v2) { leg.startXProperty().bind(v1.centerXProperty()); leg.startYProperty().bind(v1.centerYProperty()); leg.endXProperty().bind(v2.centerXProperty()); leg.endYProperty().bind(v2.centerYProperty()); } double getLegLength() { return Math.sqrt(Math.pow(leg.getStartX()-leg.getEndX(),2) + Math.pow(leg.getStartY()-leg.getEndY(),2)); } void setText(double angle) { this.text.setText(String.format("%.0fu00B0", angle)); } } } Solution
  • 6. package chapter15; import javafx.application.Application; import javafx.beans.property.DoubleProperty; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.event.EventHandler; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.input.MouseEvent; import javafx.scene.layout.BorderPane; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.scene.shape.Line; import javafx.scene.text.Font; import javafx.scene.text.Text; import javafx.scene.text.TextAlignment; import javafx.stage.Stage; /** * Java Programming: Comprehensive (10e); Liang; Pearson 2014 * * *15.21(Drag points) Draw a circle with three random points on the circle. Connect * the points to form a triangle. Display the angles in the triangle. Use the mouse * to drag a point along the perimeter of the circle. As you drag it, the triangle and * angles are redisplayed dynamically, as shown in Figure 15.30b. For computing * angles in a triangle, see Listing 4.1. * * {@code A = Math.acos((a * a - b * b - c * c) / (-2 * b * c))} * {@code B = Math.acos((b * b - a * a - c * c) / (-2 * a * c))} * {@code C = Math.acos((c * c - b * b - a * a) / (-2 * a * b))}
  • 7. * * @author ncoop */ @SuppressWarnings("WeakerAccess") public class DragPoints extends Application { @Override public void start(Stage primaryStage) { final PointPane pane = new PointPane(640, 480); pane.setStyle("-fx-background-color: wheat;"); Label label = new Label("Click and drag the points."); BorderPane borderPane = new BorderPane(pane); BorderPane.setAlignment(label, Pos.CENTER); label.setPadding(new Insets(5)); borderPane.setBottom(label); Scene scene = new Scene(borderPane); primaryStage.setTitle("Exercise15_21"); primaryStage.setScene(scene); primaryStage.show(); } private class PointPane extends Pane { final Circle circle = new Circle(); final Vertex[] v = new Vertex[3]; final int strokeWidth = 2; final Color circleStroke = Color.GRAY, legStroke = Color.BLACK; @SuppressWarnings("SameParameterValue") PointPane(double w, double h) { this.setPrefSize(w, h); this.setWidth(w); this.setHeight(h); circle.setStroke(circleStroke); circle.setFill(Color.TRANSPARENT); circle.setStrokeWidth(strokeWidth); circle.radiusProperty().bind(this.heightProperty().multiply(0.4)); circle.centerXProperty().bind(this.widthProperty().divide(2)); circle.centerYProperty().bind(this.heightProperty().divide(2)); this.getChildren().add(circle);
  • 8. // create the vertices at random angles for (int i = 0; i < v.length; i++) { v[i] = new Vertex(circle, 2 * Math.PI / v.length * (i + Math.random())); v[i].radiusProperty().bind(circle.radiusProperty().divide(10)); v[i].setPosition(); v[i].setStroke(new Color(i == 0 ? 1 : 0, i == 1 ? 1 : 0, i == 2 ? 1 : 0, 1)); v[i].setFill(Color.TRANSPARENT); v[i].setStrokeWidth(strokeWidth); this.getChildren().add(v[i]); v[i].setOnMouseDragged(new EventHandler() { @Override public void handle(MouseEvent event) { int i; for (i = 0; i < v.length; i++) if (v[i] == event.getSource()) break; v[i].setAngle(event.getX(), event.getY()); moveUpdate((Vertex) event.getSource()); } }); } // create the legs and bind their endpoints to their corresponding vertices for (int i = 0; i < v.length; i++) { int j = i + 1 < v.length ? i + 1 : 0; int k = j + 1 < v.length ? j + 1 : 0; v[i].bindLeg(v[j], v[k]); v[i].leg.setStroke(legStroke); v[i].leg.setStrokeWidth(strokeWidth); this.getChildren().add(v[i].leg); // create the text nodes and bind their locations to their corresponding vertices this.getChildren().add(v[i].text); } for(DoubleProperty p: new DoubleProperty[] {circle.radiusProperty(), circle.centerXProperty(), circle.centerYProperty()}) p.addListener(new ResizeListener()); moveUpdate(v[0]);
  • 9. } void moveUpdate(Vertex vert) { vert.setPosition(); double[] legLength = new double[3]; for (int i = 0; i < v.length; i++) legLength[i] = v[i].getLegLength(); for (int i = 0; i < v.length; i++) { int j = i + 1 < v.length ? i + 1 : 0; int k = j + 1 < v.length ? j + 1 : 0; double a = legLength[i], b = legLength[j], c = legLength[k]; double d = Math.toDegrees(Math.acos((a * a - b * b - c * c) / (-2 * b * c))); v[i].setText(d); } } private class ResizeListener implements ChangeListener { @Override public void changed(ObservableValue observableValue, Number oldWidth, Number newWidth) { for (Vertex aV : v) { aV.setPosition(); } } } } private class Vertex extends Circle { final Circle circle; final Line leg; final Text text; double centerAngle; Vertex(Circle circle, double centerAngle) { this.circle = circle; this.setAngle(centerAngle); this.leg = new Line(); this.text = new Text(); this.text.setFont(Font.font(20)); this.text.setStroke(Color.BLACK);
  • 10. this.text.setTextAlignment(TextAlignment.CENTER); this.text.xProperty().bind(this.centerXProperty().add(25)); this.text.yProperty().bind(this.centerYProperty().subtract(10)); } double getCenterAngle() {return this.centerAngle;} void setPosition() { this.setCenterX(circle.getCenterX() + circle.getRadius() * Math.cos(this.getCenterAngle())); this.setCenterY(circle.getCenterY() + circle.getRadius() * Math.sin(this.getCenterAngle())); } void setAngle(double centerAngle) { this.centerAngle = centerAngle; } void setAngle(double x, double y) { this.setAngle(Math.atan2(y - circle.getCenterY(), x - circle.getCenterX())); } void bindLeg(Vertex v1, Vertex v2) { leg.startXProperty().bind(v1.centerXProperty()); leg.startYProperty().bind(v1.centerYProperty()); leg.endXProperty().bind(v2.centerXProperty()); leg.endYProperty().bind(v2.centerYProperty()); } double getLegLength() { return Math.sqrt(Math.pow(leg.getStartX()-leg.getEndX(),2) + Math.pow(leg.getStartY()-leg.getEndY(),2)); } void setText(double angle) { this.text.setText(String.format("%.0fu00B0", angle)); } } }