在第2步的基础上设计实现一个具有GUI界面的几何图形绘制系统系统,要求实现根据选择的几何图形类型来绘制和删除相应的图形,其中相关参数应通过界面输入,并可计算图形的周长和面积。 javafx代码
时间: 2024-03-31 12:35:17 浏览: 120
好的,以下是一个使用JavaFX实现的几何图形绘制系统的代码示例,包括圆形、矩形和三角形:
```java
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Shape;
import javafx.scene.shape.Polygon;
import javafx.stage.Stage;
public class GeometryDrawer extends Application {
private Shape currentShape; // 当前绘制的图形
private Pane canvas; // 画布
private TextField param1Field; // 参数1输入框
private TextField param2Field; // 参数2输入框
private Label perimeterLabel; // 周长标签
private Label areaLabel; // 面积标签
@Override
public void start(Stage primaryStage) {
// 创建画布
canvas = new Pane();
canvas.setPrefWidth(600);
canvas.setPrefHeight(400);
// 创建参数输入框
param1Field = new TextField();
param1Field.setPrefWidth(100);
param2Field = new TextField();
param2Field.setPrefWidth(100);
// 创建周长和面积标签
perimeterLabel = new Label();
areaLabel = new Label();
// 创建按钮
Button circleBtn = new Button("圆形");
circleBtn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
createCircle();
}
});
Button rectangleBtn = new Button("矩形");
rectangleBtn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
createRectangle();
}
});
Button triangleBtn = new Button("三角形");
triangleBtn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
createTriangle();
}
});
Button deleteBtn = new Button("删除");
deleteBtn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
deleteShape();
}
});
// 将控件添加到容器中
Pane controlPane = new Pane();
controlPane.getChildren().addAll(circleBtn, rectangleBtn, triangleBtn, param1Field, param2Field, deleteBtn, perimeterLabel, areaLabel);
circleBtn.setLayoutX(10);
circleBtn.setLayoutY(10);
rectangleBtn.setLayoutX(90);
rectangleBtn.setLayoutY(10);
triangleBtn.setLayoutX(170);
triangleBtn.setLayoutY(10);
param1Field.setLayoutX(10);
param1Field.setLayoutY(50);
param2Field.setLayoutX(120);
param2Field.setLayoutY(50);
deleteBtn.setLayoutX(240);
deleteBtn.setLayoutY(10);
perimeterLabel.setLayoutX(10);
perimeterLabel.setLayoutY(90);
areaLabel.setLayoutX(10);
areaLabel.setLayoutY(110);
// 创建场景
Scene scene = new Scene(new Pane(canvas, controlPane));
// 设置主舞台
primaryStage.setTitle("几何图形绘制系统");
primaryStage.setScene(scene);
primaryStage.show();
}
// 创建圆形
private void createCircle() {
double radius = Double.parseDouble(param1Field.getText());
Circle circle = new Circle(radius);
circle.setLayoutX(300);
circle.setLayoutY(200);
canvas.getChildren().add(circle);
currentShape = circle;
updatePerimeterAndArea();
}
// 创建矩形
private void createRectangle() {
double width = Double.parseDouble(param1Field.getText());
double height = Double.parseDouble(param2Field.getText());
Rectangle rectangle = new Rectangle(width, height);
rectangle.setLayoutX(300 - width / 2);
rectangle.setLayoutY(200 - height / 2);
canvas.getChildren().add(rectangle);
currentShape = rectangle;
updatePerimeterAndArea();
}
// 创建三角形
private void createTriangle() {
double side = Double.parseDouble(param1Field.getText());
double height = Double.parseDouble(param2Field.getText());
double[] points = new double[] {
300, 200 - height / 3 * 2,
300 - side / 2, 200 + height / 3,
300 + side / 2, 200 + height / 3
};
Polygon triangle = new Polygon(points);
canvas.getChildren().add(triangle);
currentShape = triangle;
updatePerimeterAndArea();
}
// 删除图形
private void deleteShape() {
if (currentShape != null) {
canvas.getChildren().remove(currentShape);
currentShape = null;
updatePerimeterAndArea();
}
}
// 更新周长和面积标签
private void updatePerimeterAndArea() {
if (currentShape instanceof Circle) {
Circle circle = (Circle) currentShape;
double perimeter = 2 * Math.PI * circle.getRadius();
double area = Math.PI * circle.getRadius() * circle.getRadius();
perimeterLabel.setText("周长:" + perimeter);
areaLabel.setText("面积:" + area);
} else if (currentShape instanceof Rectangle) {
Rectangle rectangle = (Rectangle) currentShape;
double perimeter = 2 * (rectangle.getWidth() + rectangle.getHeight());
double area = rectangle.getWidth() * rectangle.getHeight();
perimeterLabel.setText("周长:" + perimeter);
areaLabel.setText("面积:" + area);
} else if (currentShape instanceof Polygon) {
Polygon triangle = (Polygon) currentShape;
double perimeter = triangle.getStrokeDashArray().size() * (Double.parseDouble(param1Field.getText()) + Double.parseDouble(param2Field.getText()));
double area = Double.parseDouble(param1Field.getText()) * Double.parseDouble(param2Field.getText()) / 2.0;
perimeterLabel.setText("周长:" + perimeter);
areaLabel.setText("面积:" + area);
} else {
perimeterLabel.setText("");
areaLabel.setText("");
}
}
public static void main(String[] args) {
launch(args);
}
}
```
该程序使用了JavaFX的一些基本控件和图形类,通过按钮来选择要绘制的几何图形类型,通过文本框来输入相关参数,通过标签来显示图形的周长和面积,通过画布来绘制和删除图形。您可以根据实际需求进行修改和扩展。
阅读全文