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: 66% (9 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...
-
Note: This project uses the chapter appendix. Bermuda Corporation leases the equipment that it uses in operations. Bermuda prefers operating leases (versus capital leases) in order to keep the lease...
-
What process do companies use to develop products and services?
-
Tennis Products, Inc., produces three models of high-quality tennis rackets. The following table contains recent information on the sales, costs, and profitability of the three models: The company is...
-
Current Attempt in Progress Carla Vista Co, began operations on January 1, 2022. The following information is available for Carla Ve Notes payable $ 9,800 Rent expense 11.400 Accounts payable...
-
Of the members of the Blue River Health Club, 43% have a lifetime membership and exercise regularly (three or more times a week). If 75% of the club members exercise regularly, find the probability...
-
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...
-
Miguel and Katrina have 2 children under age 17, have earned income of $16,000, and pay $1,836 in Social Security tax. Their tax liability is $1,050 before the child credit. a. What amount can they...
-
1. What is DBMS and what is its utility? Explain RDBMS with examples. 2. What is a Database? 3. Mention the issues with traditional file-based systems that make DBMS a better choice 4. Explain a few...
-
1. State the difference between lists and tuples in Python programming. 2. Explain why Python is an Interpreted Language
-
1. How does Python handle memory? 2. Python's ternary operators: how do they work? 3. How is Python's multithreading implemented
-
In a relational database, explain the difference between Inner join & Outer join. Provide an example query for each and describe the result set produced by each query.
-
A time-and-motion study measures the time required for an assembly line worker to perform a repetitive task. The data show that the time X required to bring a part from a bin to its position on an...
-
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...
-
GM has a current stock price of $97.88. If they issued a dividend of $3.18 last week, and the dividend is projected to grow at 4.9% what is the cost of equity capital for GM? Please provide all...
-
Show your work Has anything similar to Earth's weather patterns been seen on other planets
-
I am struggling to answer the rest of REQUIREMENTS 5-9. Please assist if possible. I have included an example of a worked-out problem for a reference and then under I will post my actual problem that...
Study smarter with the SolutionInn App