Question

Q2 Write a JavaFX program that displays a rectangle with upper-left corner point at (40, 40), wid...

Q2 Write a JavaFX program that displays a rectangle with upper-left corner point at (40, 40), width 40, and height 60. Display a circle with radius 30 centered at the mouse point when the mouse is clicked. Display a text to indicate whether the circle contains, intersects, or is outside of the rectangle, as shown in Figure 3. Create the GUI programmatically. DO not use FXML.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class CircleInstersectRectangle extends Application {
  
private Rectangle rectangle;
private Circle circle;
private Label text;
  
private boolean isContaining = false, isIntersecting = false;
  
  
@Override
public void start(Stage primaryStage) {
Group mainGroup = new Group();
Group drawGroup = new Group();
Group textGroup = new Group();
  
text = new Label("None");
  
rectangle = new Rectangle(40, 40, 40, 60);
rectangle.setFill(Color.TRANSPARENT);
rectangle.setStroke(Color.RED);
rectangle.setStrokeWidth(1);
  
textGroup.getChildren().add(text);
drawGroup.getChildren().add(rectangle);
mainGroup.getChildren().addAll(drawGroup, textGroup);
Scene scene = new Scene(mainGroup, 400, 250);
  
scene.addEventHandler(MouseEvent.MOUSE_CLICKED, (MouseEvent mouseEvent) -> {
if (mouseEvent.getButton().equals(MouseButton.PRIMARY)) {
circle = new Circle(mouseEvent.getX(), mouseEvent.getY(), 30, Color.BLUE);
addEventHandler(drawGroup, circle);
drawGroup.getChildren().add(circle);
  
// change the text
int rectCenterX = 60;
int rectCenterY = 70;
if(mouseEvent.getX() == rectCenterX && mouseEvent.getY() == rectCenterY)
{
text.setText("Circle is inside the rectangle!");
}
else if((mouseEvent.getX() + 30 >= 70 && mouseEvent.getX() + 30 <= 110)
&& (mouseEvent.getX() + 30 >= 70 && mouseEvent.getX() + 30 <= 130))
{
text.setText("Circle is intersecting the rectangle!");
}
  
else if((mouseEvent.getX() + 30 < 40 || mouseEvent.getX() + 30 > 110)
&& (mouseEvent.getX() + 30 < 40 || mouseEvent.getX() + 30 > 130))
{
text.setText("Circle is outside the rectangle!");
}
}
});
  
primaryStage.setTitle("Circle & Rectangle!");
primaryStage.setScene(scene);
primaryStage.show();
}
  
public void addEventHandler(Group parent, Node node)
{
node.addEventHandler(MouseEvent.MOUSE_CLICKED, (MouseEvent mouseEvent) -> {
if (mouseEvent.getButton().equals(MouseButton.SECONDARY)) {
parent.getChildren().remove(node);
text.setText("None");
}
});
}

public static void main(String[] args) {
launch(args);
}
  
}

Add a comment
Know the answer?
Add Answer to:
Q2 Write a JavaFX program that displays a rectangle with upper-left corner point at (40, 40), wid...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT