?JavaFx Program that bounds the rectangle to the outter most circles. The circle
ID: 3687295 • Letter: #
Question
?JavaFx Program that bounds the rectangle to the outter most circles. The circles are placed by the user clicking with the mouse in the parent box. The program should look similar to this...
Create an application that solves the programming 15.17 on page 623 of Liang. Try it out: CircleBounds.jar
Hint: Interface? A plain Pane for the interface. Create circle? A listener for the Pane that creates a circle at the point clicked, provided that the left button, PRIMARY, is clicked. Remove circle? A listener for the circles that removes the circle from the Pane, provided that the right button, SECONDARY, is clicked. What is the target of the remove circle listener? The circle listener is added to the circle when it is created. Which circle to remove? In the circle listener, use the getSource() method to get the Circle object that was clicked. EventSource() returns an Object, but you don’t really need to cast it to Circle, except for debugging purposes. Computing bounding box? Go through the children of the pane. All of the children are circles, except the bounding rectangle The rectangle can be checked for by using == Keep track of minY, maxY, minX and maxX Computing bounding box details? Subtract the circle radius from minX to get the bound rectangle x Add the circle diameter to the difference of maxX and minX to get the bounding rectangle width Similar for y and height What if the rectangle boundary is thicker? Subtract half the stroke width of the boundary from the bounding rectangle x computed above Add the stroke width of the bboundary to the bounding box width computed above. Similar for y and height
Explanation / Answer
PROGRAM:
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.*;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import java.util.ArrayList;
import javafx.scene.shape.*;
public class CircleCollisionTester extends Application
{
private ArrayList<Shape> nodes;
public static void main(String[] args) { launch(args);
}
public void start(Stage primaryStage)
{
primaryStage.setTitle("Drag circles around to see collisions");
Group root = new Group();
Scene scene = new Scene(root, 400, 400);
nodes = new ArrayList<>();
nodes.add(new Circle(15, 15, 30));
nodes.add(new Circle(90, 60, 30));
nodes.add(new Circle(40, 200, 30));
for (Shape block : nodes)
{
setDragListeners(block);
}
root.getChildren().addAll(nodes);
checkShapeIntersection(nodes.get(nodes.size() - 1));
primaryStage.setScene(scene);
primaryStage.show();
}
public void setDragListeners(final Shape block)
{
final Delta dragDelta = new Delta();
block.setOnMousePressed(new EventHandler<MouseEvent>()
{
public void handle(MouseEvent mouseEvent)
{
dragDelta.x = block.getLayoutX() - mouseEvent.getSceneX();
dragDelta.y = block.getLayoutY() - mouseEvent.getSceneY();
block.setCursor(Cursor.NONE);
}
});
block.setOnMouseReleased(new EventHandler<MouseEvent>()
{
public void handle(MouseEvent mouseEvent)
{
block.setCursor(Cursor.HAND);
}
});
block.setOnMouseDragged(new EventHandler<MouseEvent>()
{
public void handle(MouseEvent mouseEvent)
{
block.setLayoutX(mouseEvent.getSceneX() + dragDelta.x);
block.setLayoutY(mouseEvent.getSceneY() + dragDelta.y);
checkShapeIntersection(block);
}
});
}
private void checkShapeIntersection(Shape block)
{
boolean collisionDetected = false;
for (Shape static_bloc : nodes)
{
if (static_bloc != block)
{
static_bloc.setFill(Color.GREEN);
Shape intersect = Shape.intersect(block, static_bloc);
if (intersect.getBoundsInLocal().getWidth() != -1)
{
collisionDetected = true;
}
}
}
if (collisionDetected)
{
block.setFill(Color.BLUE);
}
else
{
block.setFill(Color.GREEN);
}
}
class Delta { double x, y; }
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.