Modify Listing 28.10, ConnectedCircles.java, to enable the user to remove a circle when the mouse is clicked
Question:
Modify Listing 28.10, ConnectedCircles.java, to enable the user to remove a circle when the mouse is clicked inside the 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: 55% (9 reviews)
To enable user to remove a circle the program must check for mouse cursor falling inside the circle ...View the full answer
Answered By
Muhammad Salman Alvi
Well, I am a student of Electrical Engineeing from Information Technology University of Punjab. Just getting into my final year. I have always been good at doing Mathematics, Physics, hardware and technical subjects. Teaching profession requires a alot of responsibilities and challenges.
My teaching experience started as an home tutor a year ago. When I started teaching mathematics and physic subjects to an O Level student. He was about 14 years old. His name was Ibrahim and I used to teach him for about 2 hours daily. Teaching him required a lot of patience but I had to be polite with him. I used to give him a 5 min break after 1 hour session. He was quite weak in basic maths and calculation. He used to do quite a lot of mistakes in his homework which I gave him weekly. So I decided to teach him basics from scratch. He used to say that he got the concept even if he didn't. So I had to ask him again and again. I worked on his basics for a month and after that I started taking a weekly test sesions. After few months he started to improve gradually. Now after teaching him for about a year I can proudly say that he has improved alot. The most important thing was he managed to communicate all the difficullties he was facing. He was quite capable and patient. I had a sincere desire to help him reach to its full potential. So I managed to do that. We had a very good honest relationship of a student and a teacher. I loved teaching him as a tutor. Now having an experience of one year teaching I can read students quite well. I look forward to work as an online tutor who could help students in solving their all sort of difficulties, problems and queries.
4.90+
29+ Reviews
43+ 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
-
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...
-
Modify Listing 28.10, ConnectedCircles.java, to enable the user to drag and move a circle. Listing 1 import javafx.application. Application; 2 import javafx.geometry.Point2D; 3 import...
-
Modify Listing 28.1, TestGraph.java, to create a file representing graph1. The file format is described in Programming Exercise 28.1. Create the file from the array defined in lines 8?21 in Listing...
-
The schedule of accounts receivable, schedule of accounts payable and trial balance for Let it Snow are shown below. Schedule of Accounts Receivable as at 31 May 20XX Customer Invoice No. Terms Date...
-
Explain why prokaryotic ribosomes can translate a circular mRNA molecule, whereas eukaryotic ribosomes normally cannot, even in the presence of the required cofactors.
-
Find the linear inequality whose graph is given in Figure 14. Write the boundary line equation in the form Ax + By = C, where A, B, and C are integers, before stating the inequality. Figure 14 10 5...
-
Jerome M. Eisenberg is an antiquities dealer and a self-proclaimed expert in classical antiquities with a doctorate in Roman, Egyptian, and Near Eastern art. Maurice E. Hall Jr. is an art dealer who...
-
Suppose Nordstrom, Inc., which operates department stores in numerous states, has the following selected financial statement data for the year ending January 30, 2014. For the year, net sales were...
-
5. Let v = [1,0], w = [1,1]. Find the projector P that projects R onto (v) along (u). What are range(P) and null(P)?
-
The March 31, 2020, adjusted trial balance for Amusement Park Repair is shown below with accounts in alphabetical order. Debit Credit Accounts payable $ 31,000 Accounts receivable $ 48,000...
-
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...
-
Rewrite Programming Exercise 20.5 using a thread to animate bouncing ball movements. The example in Section displays multiple bouncing balls. Extend the example to detect collisions. Once two balls...
-
For an input of Vx = 50 mV in the circuit of Fig. 10.75, determine the maximum frequency that may be used. The op-amp slew rate SR = 0.4 V//xs. 200 k FIG. 10.75 Problems 16, 20, 21, and 22
-
Barton Industries can issue perpetual preferred stock at a price of $46 per share. The stock would pay a constant annual dividend of $4.10 per share. If the firm's marginal tax rate is 40%, what is...
-
What material was most helpful for you? Why? How does this material relate to your life? How will you use the information at your work?
-
Farmers Bank offers to lend you $30,000 at a nominal rate of 6.0%, simple interest, with interest paid quarterly. Gold Coast offers to lend you the $30,000, but it will charge 7.0%, simple interest,...
-
Nada recently set up a tax-deferred annuity to save for his retirement. He arranged to have BD 50 taken out of each of his monthly checks; it will earn 4% annual interest. She just had his 25...
-
Two payments of $49 000 each must be made 3 year and 5 year from now. If money can earn 4.9% compounded monthly, what single payment 5 years from now would be equivalent to the two scheduled payments?
-
Byte Computer Company, a manufacturing organization, has just completed an order that Grater, Ltd., placed for 80 computers. Direct materials, purchased parts, and direct labor costs for the Grater...
-
Explain the term "Equivalent Units". Why are they calculated in process costing? [4 Marks] [minimum 350 words]
-
We said that a network layers service model defines the characteristics of end-to-end transport of packets between sending and receiving hosts. What is the service model of the Internet s network...
-
What is the role of the forwarding table within a router?
-
We made a distinction between the forwarding function and the routing function performed in the network layer. What are the key differences between routing and forwarding?
-
Melbourne, VIC . The auction property has been passed in and you are negotiating between vendor and the highest bidder but they remain apart with your vendor reluctant to reduce from their reserve,...
-
On January 1, 2018, Shark Company acquired equipment at a total cost of $66,000. The equipment had a useful life of 5 years with a residual value of $6,000. Shark Company used Double-declining...
-
Electric Car Maker Tesla makes 100,000 units of "2170 Cell" peryear in its Nevada based gigafactory for use in Model 3 Electriccar. Data concerning the unit production costs of the "2170 Cell"are as...
Study smarter with the SolutionInn App