目录
题目:**16.8 (几何:两个圆相交吗?)
编写一个程序,让用户指定两个圆的位置和大小,并且显示两个圆是否相交,如图16-39a 所示。用户可以通过鼠标单击圆内部区域并且拖动圆。圆被拖动时,文本域中的圆心坐标被更新。
-
习题思路
- 创建一个总VBox,以后的所有布局都添加到这个VBox中
- 创建一个HBox(放置文本),新建一个Text放置在布局中
- 创建一个Pane(放置圆),新建两个圆Circle并添加到Pane中
- 创建一个HBox(放置两个圆的信息设置),再分别创建两个VBox,用TextField与Label交替的方式设置组件(Center X、Center Y、Radius),最后将两个小VHox放在HBox中
- 创建一个HBox(放置绘制按钮),新建一个Button放置在布局中
- 依次把 2、3、4 、5的布局添加到VBox中
- 为两个圆设置拖动事件(将鼠标的位置设置为圆心的位置),为TextField设置键盘事件(当按下Enter键时把相应的参数改为输入框里的值),为Button注册事件(将所有节点的参数设置为默认值),在每个事件中调用判断两个圆是否重叠的方法
- (判断两个圆是否重叠的方法)如果两个圆心的距离小于等于r1+r2,两个圆重叠,否则不重叠,设置text的内容
-
代码示例
编程练习题16_8DoTwoCirclesIntersect.java
package chapter_16;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class 编程练习题16_8DoTwoCirclesIntersect extends Application{
Text text = new Text("");
@Override
public void start(Stage primaryStage) throws Exception {
//总布局
VBox vBox = new VBox();
HBox textBox = new HBox();
textBox.getChildren().add(text);
textBox.setAlignment(Pos.CENTER);
textBox.setPadding(new Insets(10, 10, 10, 10));
Pane pane = new Pane();
Circle c1 = new Circle(100, 100, 20);
Circle c2 = new Circle(200, 100, 20);
c1.setFill(Color.TRANSPARENT);
c2.setFill(Color.TRANSPARENT);
c1.setStroke(Color.BLACK);
c2.setStroke(Color.BLACK);
pane.getChildren().addAll(c1, c2);
HBox settingInfo = new HBox();//
settingInfo.setPadding(new Insets(10, 10, 10, 10));
settingInfo.setSpacing(20);
VBox vBox1 = new VBox();
Label lbInfo = new Label("Enter circle info:");
TextField tfX = new TextField("100");
tfX.setPrefWidth(50);
Label lbX = new Label("Center X",tfX);
lbX.setContentDisplay(ContentDisplay.RIGHT);
TextField tfY = new TextField("100");
Label lbY = new Label("Center Y",tfY);
tfY.setPrefWidth(50);
lbY.setContentDisplay(ContentDisplay.RIGHT);
TextField tfR = new TextField("20");
Label lbR = new Label(" Radius",tfR);
tfR.setPrefWidth(50);
lbR.setContentDisplay(ContentDisplay.RIGHT);
vBox1.getChildren().addAll(lbInfo,lbX,tfX, lbY,tfY, lbR,tfR);
vBox1.setAlignment(Pos.CENTER_LEFT);
vBox1.setPadding(new Insets(5, 5, 5, 5));
vBox1.setStyle("-fx-border-Color:black");
VBox vBox2 = new VBox();
Label lbInfo2 = new Label("Enter circle info:");
TextField tfX2 = new TextField("200");
tfX2.setPrefWidth(50);
Label lbX2 = new Label("Center X",tfX2);
lbX2.setContentDisplay(ContentDisplay.RIGHT);
TextField tfY2 = new TextField("100");
tfY2.setPrefWidth(50);
Label lbY2 = new Label("Center Y",tfY2);
lbY2.setContentDisplay(ContentDisplay.RIGHT);
TextField tfR2 = new TextField("20");
Label lbR2 = new Label("Radius",tfR2);
tfR2.setPrefWidth(50);
lbR2.setContentDisplay(ContentDisplay.RIGHT);
vBox2.getChildren().addAll(lbInfo2,lbX2,tfX2, lbY2,tfY2, lbR2,tfR2);
vBox2.setAlignment(Pos.CENTER_RIGHT);
vBox2.setPadding(new Insets(5, 5, 5, 5));
vBox2.setStyle("-fx-border-Color:black");
settingInfo.getChildren().addAll(vBox1,vBox2);
HBox drawBox = new HBox();
Button btDraw = new Button("Redraw Circles");
drawBox.getChildren().add(btDraw);
drawBox.setAlignment(Pos.CENTER);
vBox.getChildren().addAll(textBox, pane, settingInfo,drawBox);
DoesIntersect(c1, c2);
c1.setOnMouseDragged(e ->{
c1.setCenterX(e.getX());
c1.setCenterY(e.getY());
tfX.setText(e.getX()+"");
tfY.setText(e.getY()+"");
DoesIntersect(c1, c2);
});
c2.setOnMouseDragged(e ->{
c2.setCenterX(e.getX());
c2.setCenterY(e.getY());
tfX2.setText(e.getX()+"");
tfY2.setText(e.getY()+"");
DoesIntersect(c1, c2);
});
tfX.setOnKeyPressed(e ->{
if(e.getCode() == KeyCode.ENTER&&tfX.getText()!="") {
c1.setCenterX(Double.parseDouble(tfX.getText()));
DoesIntersect(c1, c2);
}
});
tfY.setOnKeyPressed(e ->{
if(e.getCode() == KeyCode.ENTER&&tfY.getText()!="") {
c1.setCenterY(Double.parseDouble(tfY.getText()));
DoesIntersect(c1, c2);
}
});
tfR.setOnKeyPressed(e ->{
if(e.getCode() == KeyCode.ENTER&&tfR.getText()!="") {
c1.setRadius(Double.parseDouble(tfR.getText()));
DoesIntersect(c1, c2);
}
});
tfX2.setOnKeyPressed(e ->{
if(e.getCode() == KeyCode.ENTER&&tfX2.getText()!="") {
c2.setCenterX(Double.parseDouble(tfX2.getText()));
DoesIntersect(c1, c2);
}
});
tfY2.setOnKeyPressed(e ->{
if(e.getCode() == KeyCode.ENTER&&tfY2.getText()!="") {
c2.setCenterY(Double.parseDouble(tfY2.getText()));
DoesIntersect(c1, c2);
}
});
tfR2.setOnKeyPressed(e ->{
if(e.getCode() == KeyCode.ENTER&&tfR2.getText()!="") {
c2.setRadius(Double.parseDouble(tfR2.getText()));
DoesIntersect(c1, c2);
}
});
btDraw.setOnAction(e ->{
c1.setCenterX(100);
c1.setCenterY(100);
c1.setRadius(20);
c2.setCenterX(200);
c2.setCenterY(100);
c2.setRadius(20);
tfX.setText("100");
tfY.setText("100");
tfR.setText("20");
tfX2.setText("200");
tfY2.setText("100");
tfR2.setText("20");
DoesIntersect(c1, c2);
});
Scene scene = new Scene(vBox,280, 350);
primaryStage.setTitle("编程练习题16_8DoTwoCirclesIntersect");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
public void DoesIntersect(Circle c1, Circle c2) {
double x1 = c1.getCenterX();
double y1 = c1.getCenterY();
double r1 = c1.getRadius();
double x2 = c2.getCenterX();
double y2 = c2.getCenterY();
double r2 = c1.getRadius();
double d = Math.sqrt(Math.pow(x2-x1, 2)+Math.pow(y2-y1, 2));
if(d > r1+r2) {
text.setText("Two circles intersect? No");
}else {
text.setText("Two circles intersect? Yes");
}
}
}
-
结果展示