能补充这段代码吗并实现以下要求1. (简答题) 形状家族应用: 结合配置文件shape.properties和多态性定义形状家族,至少包括三种形状。并结合JavaFx实现显示形状外观、计算的面积和周长,以及控制形状在界面的位置的变更。import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.scene.shape.Rectangle; import javafx.scene.shape.Polygon; import javafx.stage.Stage; import java.io.FileInputStream; import java.io.IOException; import java.util.Properties; abstract class Shape { protected Color color; protected double area; protected double perimeter; protected double positionX; protected double positionY; public abstract void calculateArea(); public abstract void calculatePerimeter(); public abstract void draw(Pane pane); public void setPosition(double x, double y) { this.positionX = x; this.positionY = y; } } class CircleShape extends Shape { private double radius; public CircleShape(double radius) { this.radius = radius; this.color = Color.RED; } @Override public void calculateArea() { this.area = Math.PI * Math.pow(radius, 2); } @Override public void calculatePerimeter() { this.perimeter = 2 * Math.PI * radius; } @Override public void draw(Pane pane) { Circle circle = new Circle(radius); circle.setFill(color); circle.setLayoutX(positionX); circle.setLayoutY(positionY); pane.getChildren().add(circle); } } class RectangleShape extends Shape { private double width; private double height; public RectangleShape(double width, double height) { this.width = width; this.height = height; this.color = Color.BLUE; } @Override public void calculateArea() { this.area = width * height; } @Override public void calculatePerimeter() { this.perimeter = 2 * (width + height); } @Override public void draw(Pane pane) { Rectangle rectangle = new Rectangle(width, height); rectangle.setFill(color); rectangle.setLayoutX(positionX); rectangle.setLayoutY(positionY); pane.getChildren().add(rectangle); } } class TriangleShape extends Shape { private double[] points; public TriangleShape(double[] points) { this.points = points; this.color = Color.GREEN; } @Override public void calculateArea() { double x1 = points[0]; double y1 = points[1]; doubl
时间: 2023-12-06 10:41:15 浏览: 102
计算机网络简答题和论述题(1).pdf
e x2 = points[2]; double y2 = points[3]; double x3 = points[4]; double y3 = points[5]; this.area = Math.abs((x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2))/2); } @Override public void calculatePerimeter() { double x1 = points[0]; double y1 = points[1]; double x2 = points[2]; double y2 = points[3]; double x3 = points[4]; double y3 = points[5]; double side1 = Math.sqrt(Math.pow(x2-x1, 2) + Math.pow(y2-y1, 2)); double side2 = Math.sqrt(Math.pow(x3-x2, 2) + Math.pow(y3-y2, 2)); double side3 = Math.sqrt(Math.pow(x1-x3, 2) + Math.pow(y1-y3, 2)); this.perimeter = side1 + side2 + side3; } @Override public void draw(Pane pane) { Polygon triangle = new Polygon(points); triangle.setFill(color); triangle.setLayoutX(positionX); triangle.setLayoutY(positionY); pane.getChildren().add(triangle); } } public class ShapeFamilyApp extends Application { private static Properties properties; static { properties = new Properties(); try { properties.load(new FileInputStream("shape.properties")); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) throws Exception { Pane pane = new Pane(); double width = Double.parseDouble(properties.getProperty("pane.width")); double height = Double.parseDouble(properties.getProperty("pane.height")); pane.setPrefSize(width, height); for (int i = 1; i <= 3; i++) { String type = properties.getProperty("shape" + i + ".type"); double x = Double.parseDouble(properties.getProperty("shape" + i + ".x")); double y = Double.parseDouble(properties.getProperty("shape" + i + ".y")); switch (type) { case "circle": double radius = Double.parseDouble(properties.getProperty("shape" + i + ".radius")); CircleShape circle = new CircleShape(radius); circle.setPosition(x, y); circle.calculateArea(); circle.calculatePerimeter(); circle.draw(pane); break; case "rectangle": double widthR = Double.parseDouble(properties.getProperty("shape" + i + ".width")); double heightR = Double.parseDouble(properties.getProperty("shape" + i + ".height")); RectangleShape rectangle = new RectangleShape(widthR, heightR); rectangle.setPosition(x, y); rectangle.calculateArea(); rectangle.calculatePerimeter(); rectangle.draw(pane); break; case "triangle": double[] points = new double[6]; for (int j = 0; j < 6; j++) { points[j] = Double.parseDouble(properties.getProperty("shape" + i + ".point" + (j+1))); } TriangleShape triangle = new TriangleShape(points); triangle.setPosition(x, y); triangle.calculateArea(); triangle.calculatePerimeter(); triangle.draw(pane); break; default: break; } } Scene scene = new Scene(pane); primaryStage.setScene(scene); primaryStage.show(); } }
要求:
1. 至少包括三种形状,这里我们定义了圆形、矩形和三角形三种形状。
2. 使用配置文件shape.properties来定义每个形状的属性,包括类型、位置和其他特定属性。在程序中,我们使用了Java的Properties类来读取配置文件中的属性。
3. 实现了抽象类Shape,并定义了每个形状的具体实现。每个形状都有自己的颜色、面积、周长和位置,并可以在JavaFX的Pane中绘制出来。
4. 在主类ShapeFamilyApp中,读取配置文件中的属性,根据类型创建对应的形状,并设置位置、计算面积和周长,最后在JavaFX的Pane中绘制出来。
5. 在JavaFX中,可以通过拖动形状来改变它们在界面中的位置。
阅读全文