Modify Listing 28.10, ConnectedCircles.java, to enable the user to drag and move a circle. Listing 1 import
Question:
Modify Listing 28.10, ConnectedCircles.java, to enable the user to drag and move a circle.
Listing
Transcribed Image Text:
1 import javafx.application. Application; 2 import javafx.geometry.Point2D; 3 import javafx.scene.Node; 4 import javafx.scene.Scene; 5 import javafx.scene.layout.Pane; 6 import javafx.scene.paint.Color; 7 import javafx.scene.shape.Circle; 8 import javafx.stage.Stage; 10 public class ConnectedCircles extends Application { 11 12 13 14 15 16 17 18 19 @Override // Override the start method in the Application class public void start(Stage primaryStage) { // Create a scene and place it in the stage Scene scene = new Scene(new CirclePane(), 450, 350); primaryStage.setTitle("ConnectedCircles"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage /** Pane for displaying circles */ class CirclePane extends Pane { 20 21 public CirclePane () { this.setOnMouseClicked(e -> { if (!isInsideACircle(new Point2D(e.getX(), e.getY()))) { // Add a new circle getChildren ().add(new Circle(e.getX(), e.getY(), 20)); colorIfConnected(); 22 23 24 25 26 27 28 }); 29 30 /** Returns true if the point is inside an existing circle */ private boolean isInsideACircle(Point2D p) { for (Node circle: this.getChildren()) if (circle.contains (p)) 33 34 35 36 return true; 37 38 return false; 39 40 41 42 /** Color all circles if they are connected */ private void colorIfConnected() { if (getChildren().size() == 0) return; // No circles in the pane 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 // Build the edges java.util.List
1 import javafx.application. Application; 2 import javafx.geometry.Point2D; 3 import javafx.scene.Node; 4 import javafx.scene.Scene; 5 import javafx.scene.layout.Pane; 6 import javafx.scene.paint.Color; 7 import javafx.scene.shape.Circle; 8 import javafx.stage.Stage; 10 public class ConnectedCircles extends Application { 11 12 13 14 15 16 17 18 19 @Override // Override the start method in the Application class public void start(Stage primaryStage) { // Create a scene and place it in the stage Scene scene = new Scene(new CirclePane(), 450, 350); primaryStage.setTitle("ConnectedCircles"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage /** Pane for displaying circles */ class CirclePane extends Pane { 20 21 public CirclePane () { this.setOnMouseClicked(e -> { if (!isInsideACircle(new Point2D(e.getX(), e.getY()))) { // Add a new circle getChildren ().add(new Circle(e.getX(), e.getY(), 20)); colorIfConnected(); 22 23 24 25 26 27 28 }); 29 30 /** Returns true if the point is inside an existing circle */ private boolean isInsideACircle(Point2D p) { for (Node circle: this.getChildren()) if (circle.contains (p)) 33 34 35 36 return true; 37 38 return false; 39 40 41 42 /** Color all circles if they are connected */ private void colorIfConnected() { if (getChildren().size() == 0) return; // No circles in the pane 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 // Build the edges java.util.List edges = new java.util.ArrayList<>(); for (int i = 0; i < getChildren().size(); i++) for (int j = i + 1; j < getChildren().size(); j++) if (overlaps ((Circie) (getChildren().get(i)), (Circle) (getChildren().get(j)))) { edges.add(new AbstractGraph.Edge (i, j)); edges.add(new AbstractGraph. Edge (j, i)); 58 59 60 61 62 63 // Create a graph with circles as vertices Graph graph = new UnweightedGraph<> ((java.util.List)getChildren(), edges); AbstractGraph.Tree tree = graph.dfs (0); // a DFS tree boolean isAl1CirclesConnected = getChildren ().size() == tree .getNumberofVerticesFound (); 64 for (Node circle: getChildren()) { if (isA11CirclesConnected) { // All circles are connected ((Circle)circle).setFill(Color.RED); 65 66 67 68 else { ((Circle)circle).setStroke(Color.BLACK); (Circle)circle).setFill(Color.WHITE); 69 70 71 72 73 74 { 75 public static boolean overlaps (Circle circlel, Circle circle2) { return new Point2D(circlel.getCenterX(), circlel.getCenterYO). distance(circle2.getCenterX(), circle2.getCenterY()) <= circlel.getRadius () + circle2.getRadius(); 76 77 78 79 80 81 } mmm mmcmmmO
Fantastic news! We've Found the answer you've been seeking!
Step by Step Answer:
Answer rating: 71% (7 reviews)
To enable user to drag and move a circle the Pane must be set to listen that event It can be done by ...View the full answer
Answered By
Nazrin Ziad
I am a post graduate in Zoology with specialization in Entomology.I also have a Bachelor degree in Education.I posess more than 10 years of teaching as well as tutoring experience.I have done a project on histopathological analysis on alcohol treated liver of Albino Mice.
I can deal with every field under Biology from basic to advanced level.I can also guide you for your project works related to biological subjects other than tutoring.You can also seek my help for cracking competitive exams with biology as one of the subjects.
3.30+
2+ Reviews
10+ Question Solved
Related Book For
Introduction to Java Programming, Comprehensive Version
ISBN: 978-0133761313
10th Edition
Authors: Y. Daniel Liang
Question Posted:
Students also viewed these Computer science questions
-
Modify Listing 25.9, BSTAnimation.java, to add three new buttons?Show Inorder, Show Preorder, and Show Postorder?to display the result in a label, as shown in Figure 25.24. You need also to modify...
-
Add a slider to enable the user to set the current time for the video and a label to display the current time and the total time for the video. As shown in Figure 16.47a, the total time is 5 minutes...
-
Rewrite Programming Exercise 28.14 to enable the user to set an initial pattern of the 4 * 4 16 tails problem (see Figure 28.23a). The user can click the Solve button to display the solution, as...
-
QUESTION 9 The output expression for an AND-OR-Invert circuit having one AND gate with inputs A, B and C and one AND gate with inputs D, E and Fis O(A+B+C)(D+E+F) O (A+B+C)(D+E+F) ABC + DEF...
-
EF-Tu binds all aminoacyl-tRNAs with approximately equal affinity so that it can deliver them to the ribosome with the same efficiency. Based on the experimentally determined binding constants for...
-
A hospital patient is required to have at least 84 units of drug A and 120 units of drug B each day (assume that an overdose of either drug is harmless). Each gram of substance M contains 10 units of...
-
Defendant Monty J. Person began working for Garage Solutions, LLC, in March 2015. Three months into his employment, Person was sent by the owner of Garage Solutions, Mark Fontenot, to Rexburg, Idaho,...
-
The accounting records of Clear Photography, Inc., reflected the following balances as of January 1, 2012: Cash .......... $18,000 Beginning inventory .... 13,500 (150 units @ $90) Common stock...
-
Thompson's Hardware spent $46,370 this year on business insurance alone. If total sales were $765,500, what percent of total sales was spent on business insurance? Round to the nearest tenth.
-
C. George (Controls) Ltd manufactures a thermostat that can be used in a range of kitchen appliances. The manufacturing process is, at present, semi-automated. The equipment used cost £540,000...
-
Modify Listing 28.10, ConnectedCircles.java, to display sets of connected circles in different colors. That is, if two circles are connected, they are displayed using the same color; otherwise, they...
-
Listing 28.10, ConnectedCircles.java, allows the user to create circles and determine whether they are connected. Rewrite the program for rectangles. The program lets the user create a rectangle by...
-
Let \((S, D)\) be a couple of price-dividend processes not admitting arbitrage opportunities and let \(\mathbb{P}^{*}\) be a risk neutral probability measure (which exists by Theorem 6.23). As in...
-
Tanner owns an investment that is expected to pay him 4,380 dollars per quarter forever with the next payment of 4,380 dollars expected in 3 months from today. The investment has an annual return of...
-
Sheridan Corporation recently reported an EBITDA of $32.70 million and net income of $9.7 million. The company had $6.8 million in interest expense, and it's average corporate tax rate was 35...
-
- A car's velocity in meters per second is given by the function v(t) = 2t2 3t+5. Find the car's acceleration at t = 2.
-
Reba Dixon is a fifth-grade school teacher who earned a salary of $37,760 in 2023. She is 45 years old and receives $1,200 of alimony payments each month from her former husband (divorced in 2016)....
-
You are considering purchasing a small mixed-use building. As part of your analysis, you want to project the NOI for next year (20X1). The seller provides you with the following rent roll and...
-
Clean Laboratories produces biodegradable liquid detergents that leave no soap film. The production process has been automated, so the product can now be produced in one operation instead of in a...
-
The bookkeeper for Riley, Inc., made the following errors: a. A cash purchase of supplies of $357 was recorded as a debit to Supplies for $375 and a credit to Cash of $375. b. A cash sale of $3,154...
-
Consider the topology shown in Figure 4.20. Denote the three subnets with hosts (starting clockwise at 12:00) as Networks A, D, and C. Denote the subnets without hosts as Networks D, E, and F. a....
-
Three types of switching fabrics are discussed in Section 4.2. List and briefly describe each type. Which if any, can send multiple packets across the fabric in parallel?
-
Suppose that an arriving packet matches two or more entries in a routers forwarding table. With traditional destination-based forwarding, what rule does a router apply to determine which of these...
-
Colleen Company has gathered the following data pertaining to activities it performed for two of its major customers. Number of orders Units per order Sales returns: Number of returns Total units...
-
Starting next year you plan on saving for a trip 10 years from now. You plan to save a $1000 a year from year 1 to year 9 because in year 10 there is no point in putting money away if you are going...
-
Write a C++ program to implement a stack using linked lists and provide functions for push, pop, and peek operations.
Study smarter with the SolutionInn App