Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(Java language, IDE NetBeans 8.0.2, book \"introduction to JAVA programming comp

ID: 670582 • Letter: #

Question

(Java language, IDE NetBeans 8.0.2, book "introduction to JAVA programming comprehensive version 10th edition" page 624 15.21 problem, *IMPORTANT - this problem must be done without JFrame, and done with JavaFx i know this is harder but this is how we are learning from the basics for now.*)

(drag points, GUI program) Draw a circle with three frandom 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, (example is shown in example 15_21), The points should display its point example 83.37 or 51.07 on the circle

*It wouldnt let me upload the 15.21 picture but its just a picture of a circle with a triangle drawn on it with 3 points on the circle and you can move the points around on the circle. sorry for this.*

Explanation / Answer

isPointOnCircle is the important method, which moves the trinagle point if it lies approximately on the circle (diff = 3)

This is what I can do.

package javafxprog;
import javafx.scene.Scene;

import javafx.application.Application;
import javafx.beans.property.*;
import javafx.beans.value.*;
import javafx.collections.*;
import javafx.event.EventHandler;
import javafx.scene.*;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
import javafx.stage.Stage;

/** Drag the anchors around to change a polygon's points. */
public class CircleJavaFx extends Application {
  
private Circle circle;
public static void main(String[] args) throws Exception { launch(args); }

// main application layout logic.
@Override public void start(final Stage stage) throws Exception {
  
   circle = createCircle();
  
   double radius = circle.getRadius();
  
    Polygon triangle = createInitialTriangle(radius,circle.getCenterX(), circle.getCenterY());

    Group root = new Group();
    root.getChildren().add(circle);
  
    root.getChildren().add(triangle);
    root.getChildren().addAll(createControlAnchorsFor(triangle.getPoints()));

    stage.setTitle("Triangle in Circle");
    stage.setScene(
        new Scene(
            root,
            400, 400, Color.ALICEBLUE
        )
    );
    stage.show();
}

// creates a circle.
private Circle createCircle() {
      double radius = 100.0;
      Circle circle = new Circle();
      circle.setCenterX(175.0d);
      circle.setCenterY(175.0d);
      circle.setRadius(radius);
      circle.setStroke(Color.RED);
      circle.setStrokeWidth(4);
      circle.setFill(Color.CORNSILK.deriveColor(0, 1.2, 1, 0.6));
      return circle;
}

private double[] getPointOnCircle(double centerX, double centerY, double radius) {
        double point[] = new double[2];
        double circleAngle =Math.random() *Math.PI *2;
        point[0] = centerX+ Math.cos(circleAngle)*radius;
        point[1] = centerY+ Math.sin(circleAngle)*radius;
        return point;
}

// creates a triangle.
private Polygon createInitialTriangle(double radius, double centerX, double centerY) {
    Polygon triangle = new Polygon();

  
    double a[] = getPointOnCircle(centerX,centerY,radius);
    double b[] = getPointOnCircle(centerX,centerY,radius);
    double c[] = getPointOnCircle(centerX,centerY,radius);
    triangle.getPoints().setAll(
        a[0], a[1],
        b[0], b[1],
        c[0], c[1]
    );

    triangle.setStroke(Color.FORESTGREEN);
    triangle.setStrokeWidth(4);
    triangle.setStrokeLineCap(StrokeLineCap.ROUND);
    triangle.setFill(Color.CORNSILK.deriveColor(0, 1.2, 1, 0.6));

    return triangle;
}

// @return a list of anchors which can be dragged around to modify points in the format [x1, y1, x2, y2...]
private ObservableList<Anchor> createControlAnchorsFor(final ObservableList<Double> points) {
    ObservableList<Anchor> anchors = FXCollections.observableArrayList();

    for (int i = 0; i < points.size(); i+=2) {
      final int idx = i;

      DoubleProperty xProperty = new SimpleDoubleProperty(points.get(i));
      DoubleProperty yProperty = new SimpleDoubleProperty(points.get(i + 1));

      xProperty.addListener(new ChangeListener<Number>() {
        @Override public void changed(ObservableValue<? extends Number> ov, Number oldX, Number x) {
          points.set(idx, x.doubleValue());
        }
      });

      yProperty.addListener(new ChangeListener<Number>() {
        @Override public void changed(ObservableValue<? extends Number> ov, Number oldY, Number y) {
          points.set(idx + 1, y.doubleValue());
        }
      });

      anchors.add(new Anchor(Color.GOLD, xProperty, yProperty));
    }

    return anchors;
}

// a draggable anchor displayed around a point.
class Anchor extends Circle {
    private final DoubleProperty x, y;

    Anchor(Color color, DoubleProperty x, DoubleProperty y) {
      super(x.get(), y.get(), 10);
      setFill(color.deriveColor(1, 1, 1, 0.5));
      setStroke(color);
      setStrokeWidth(2);
      setStrokeType(StrokeType.OUTSIDE);

      this.x = x;
      this.y = y;

      x.bind(centerXProperty());
      y.bind(centerYProperty());
      enableDrag();
    }

    // make a node movable by dragging it around with the mouse.
    private void enableDrag() {
      final Delta dragDelta = new Delta();
      setOnMousePressed(new EventHandler<MouseEvent>() {
        @Override public void handle(MouseEvent mouseEvent) {
          // record a delta distance for the drag and drop operation.
          dragDelta.x = getCenterX() - mouseEvent.getX();
          dragDelta.y = getCenterY() - mouseEvent.getY();
          getScene().setCursor(Cursor.MOVE);
        }
      });
      setOnMouseReleased(new EventHandler<MouseEvent>() {
        @Override public void handle(MouseEvent mouseEvent) {
          getScene().setCursor(Cursor.HAND);
        }
      });
      setOnMouseDragged(new EventHandler<MouseEvent>() {
        @Override public void handle(MouseEvent mouseEvent) {
          double newX = mouseEvent.getX() + dragDelta.x;
          double newY = mouseEvent.getY() + dragDelta.y;

       
          if(isPointOnCircle(newX, newY)) {
           // System.out.println(" drawing point ");
              //find whether the point lies on circle or not
              if (newX > 0 && newX < getScene().getWidth()) {
                  setCenterX(newX);
                }
               if (newY > 0 && newY < getScene().getHeight()) {
                setCenterY(newY);
              }
          }
        }
      });
    
      setOnMouseEntered(new EventHandler<MouseEvent>() {
        @Override public void handle(MouseEvent mouseEvent) {
          if (!mouseEvent.isPrimaryButtonDown()) {
            getScene().setCursor(Cursor.HAND);
          }
        }
      });
      setOnMouseExited(new EventHandler<MouseEvent>() {
        @Override public void handle(MouseEvent mouseEvent) {
          if (!mouseEvent.isPrimaryButtonDown()) {
            getScene().setCursor(Cursor.DEFAULT);
          }
        }
      });
    }

    //change this logic - it approximately moves the point on the circle
    private boolean isPointOnCircle(double x, double y) {
      
       double sum2 = Math.pow((x-circle.getCenterX()) , 2)+Math.pow((y-circle.getCenterY()) , 2);
       double distance = Math.pow(sum2, 0.5);
       //System.out.println(" x = "+x+" y = "+y+" cenX="+circle.getCenterX()+" cenY= "+circle.getCenterY()+" distance = "+distance+" radius= "+circle.getRadius());
       double diff =   distance - circle.getRadius();
       if(diff >= -3 && diff <= 3)
           return true;
       return false;
    }
    // records relative x and y co-ordinates.
    private class Delta { double x, y; }
}
}