I am building a program the draws three circle, three line connecting the circle
ID: 3863066 • Letter: I
Question
I am building a program the draws three circle, three line connecting the circles to make a triangle, and display text of the calculated angle. When you move the circles the angles should re-compute and the lines should move as well.Additionally the shapes should change color according to user input, R = red, B = blue, G = green, C = black.. The only problem I am having is I cannot get the circles, text, or lines to change color according to keyboard input in my keypressed() method in DragAnglePane Class. The javafx program runs fine but does not change color.
***************************************************************************MainClass***************************************************************************************************************************
import javafx.embed.swing.JFXPanel;
import javafx.application.Platform;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
//import javafx.scene.control.Button;
public class Main
{
public void buildUI(){
Stage primaryStage = new Stage();
Scene scene = new Scene( new DragAnglePane(), 500, 500);
primaryStage.setTitle("Drag Angle Pane");
primaryStage.setScene(scene);
primaryStage.show();
}
public void launchFX(){
new JFXPanel();
Platform.setImplicitExit(false);
Platform.runLater( () -> buildUI() );
}
public static void main(String []args){
Main app = new Main();
app.launchFX();
}
}
**************************************************************************DragAnglePaneClass**************************************************************************************************************
import javafx.scene.layout.Pane;
import javafx.scene.shape.Circle;
import javafx.scene.layout.BorderPane;
import javafx.geometry.Pos;
import javafx.scene.layout.Border;
import javafx.scene.layout.BorderStroke;
import javafx.scene.layout.BorderStrokeStyle;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.scene.text.Text;
import javafx.geometry.Point2D;
import java.awt.event.KeyEvent.*;
import javafx.event.EventHandler;
import javafx.scene.input.KeyCode;
/**
* Write a description of class DragAnglePane here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class DragAnglePane extends Pane
{
Pane pane;
Color Blue = Color.BLUE;
private int radius = 10;
Circle[] circle = {new Circle(40, 40, 10),
new Circle(140, 40, 10), new Circle(60, 140, 10)};
Line line1 = new Line();
Line line2 = new Line();
Line line3 = new Line();
Text[] text = {new Text(), new Text(), new Text()};
/**
* Constructor for objects of class DragAnglePane
*/
public DragAnglePane()
{
buildUI();
customizeUI();
registerHandlers();
}
public void buildUI(){
pane = new Pane();
setLines();
getChildren().addAll(circle[0], circle[1], circle[2],
line1, line2, line3, text[0], text[1], text[2]);
}
public void customizeUI(){
pane.setBorder( new Border( new BorderStroke(
Color.BLUE, BorderStrokeStyle.SOLID, null, null) ) );
}
public void registerHandlers(){
circle[0].setOnMouseDragged(e -> {
if (circle[0].contains(e.getX(), e.getY())) {
// Recompute and display angles
circle[0].setCenterX(e.getX());
circle[0].setCenterY(e.getY());
setLines();
}}
);
circle[1].setOnMouseDragged(e -> {
if (circle[1].contains(e.getX(), e.getY())) {
// Recompute and display angles
circle[1].setCenterX(e.getX());
circle[1].setCenterY(e.getY());
setLines();
}}
);
circle[2].setOnMouseDragged(e -> {
if (circle[2].contains(e.getX(), e.getY())) {
// Recompute and display angles
circle[2].setCenterX(e.getX());
circle[2].setCenterY(e.getY());
setLines();
}}
);
keyPressed();
}
private void setLines() {
line1.setStartX(circle[0].getCenterX());
line1.setStartY(circle[0].getCenterY());
line1.setEndX(circle[1].getCenterX());
line1.setEndY(circle[1].getCenterY());
line2.setStartX(circle[0].getCenterX());
line2.setStartY(circle[0].getCenterY());
line2.setEndX(circle[2].getCenterX());
line2.setEndY(circle[2].getCenterY());
line3.setStartX(circle[1].getCenterX());
line3.setStartY(circle[1].getCenterY());
line3.setEndX(circle[2].getCenterX());
line3.setEndY(circle[2].getCenterY());
/* Using Point2D constructor to create new instance of Point2D. Set X an Y magnitudes. Using the
distance method to get the distance between two points. In this case circles 3 and 2.*/
double a = new Point2D(circle[2].getCenterX(), circle[2].getCenterY()).
distance(circle[1].getCenterX(), circle[1].getCenterY());
/* Using Point2D constructor to create new instance of Point2D. Set X an Y magnitudes. Using the
distance method to get the distance between two points. In this case circles 3 and 1.*/
double b = new Point2D(circle[2].getCenterX(), circle[2].getCenterY()).
distance(circle[0].getCenterX(), circle[0].getCenterY());
/* Using Point2D constructor to create new instance of Point2D. Set X an Y magnitudes. Using the
distance method to get the distance between two points. In this case circles 2 and 1.*/
double c = new Point2D(circle[1].getCenterX(), circle[1].getCenterY()).
distance(circle[0].getCenterX(), circle[0].getCenterY());
// create an array of doubles called array to store the values of the calculated angles.
double[] angle = new double[3];
/*Using the math calss and arccosine function to compute the angles of the circles.
* Found this formula at http://www.cs.colostate.edu/~cs164/.Spring17/slides/LiangChapter4.pdf
A = acos((a * a - b * b - c * c) / (-2 * b * c))
B = acos((b * b - a * a - c * c) / (-2 * a * c))
C = acos((c * c - b * b - a * a) / (-2 * a * b))
*/
angle[0] = Math.acos((a * a - b * b - c * c) / (-2 * b * c));
angle[1] = Math.acos((b * b - a * a - c * c) / (-2 * a * c));
angle[2] = Math.acos((c * c - b * b - a * a) / (-2 * a * b));
//Move the each text and set text equal to the individual values of angle[] with each circle.
for (int i = 0; i < 3; i++) {
text[i].setX(circle[i].getCenterX());
text[i].setY(circle[i].getCenterY() - radius);
text[i].setText(String.format("%.2f", Math.toDegrees(angle[i])));
}
}
public void keyPressed() {
setOnKeyPressed(e -> {
if (e.getCode() == KeyCode.R) {
for(int i = 0; i > 3; i++){
circle[i].setFill(Color.RED);
text[i].setStroke(Color.RED);
}
line1.setStroke(Color.RED);
line2.setStroke(Color.RED);
line3.setStroke(Color.RED);
}else if (e.getCode() == KeyCode.B) {
for(int i = 0; i > 3; i++){
circle[i].setFill(Color.BLUE);
text[i].setStroke(Color.BLUE);
}
line1.setStroke(Color.BLUE);
line2.setStroke(Color.BLUE);
line3.setStroke(Color.BLUE);
}else if (e.getCode() == KeyCode.G) {
for(int i = 0; i > 3; i++){
circle[i].setFill(Color.GREEN);
text[i].setStroke(Color.GREEN);
}
line1.setStroke(Color.GREEN);
line2.setStroke(Color.GREEN);
line3.setStroke(Color.GREEN);
}else if (e.getCode() == KeyCode.C) {
for(int i = 0; i > 3; i++){
circle[i].setFill(Color.BLACK);
text[i].setStroke(Color.BLACK);
}
line1.setStroke(Color.BLACK);
line2.setStroke(Color.BLACK);
line3.setStroke(Color.BLACK);
}}
);
}
}
Explanation / Answer
your for loops are incorrect. The correct condition should be
for (i = 0 ; i< 3 ; i++) you have i > 3 instead of i <3
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.