在我的 JavaFX 项目中,我使用大量形状(例如 1 000 000)来表示地理数据(例如地块轮廓、街道等)。它们存储在一个组中,有时我必须清除它们(例如,当我加载包含新地理数据的新文件时)。问题是:清除/删除它们需要很多时间。所以我的想法是在单独的线程中删除形状,这显然由于 JavaFX 单线程而不起作用。
这是我想要做的事情的简化代码:
HelloApplication.java
package com.example.javafxmultithreading;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.Line;
import javafx.stage.Stage;
import java.io.IOException;
public class HelloApplication extends Application {
public static Group group = new Group();
@Override
public void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml"));
Scene scene = new Scene(fxmlLoader.load());
stage.setTitle("Hello!");
stage.setScene(scene);
stage.show();
for (int i = 0; i < 1000000; i++) {
group.getChildren().add(new Line(100, 200, 200, 300));
}
HelloController.helloController = fxmlLoader.getController();
HelloController.helloController.pane.getChildren().addAll(group); …Run Code Online (Sandbox Code Playgroud) 我有多个从 Shape 类扩展而来的几何图形,如下所示:
public class ExtendedLine extends Line {
private String attribute;
// more attributes and methods
}
Run Code Online (Sandbox Code Playgroud)
public class ExtendedCircle extends Circle {
private String attribute;
// more attributes and methods
}
Run Code Online (Sandbox Code Playgroud)
现在,为了一次访问所有几何图形,我定义了一个超类,它扩展了 Shape 并包含 ExtendedLine 和 ExtendedCircle 共有的所有属性和方法。这为我节省了很多冗余代码。
public class Geometry extends Shape {
private String attribute;
// more attributes and methods
}
Run Code Online (Sandbox Code Playgroud)
public class ExtendedLine extends Geometry {
}
Run Code Online (Sandbox Code Playgroud)
public class ExtendedCircle extends Geometry {
}
Run Code Online (Sandbox Code Playgroud)
是否允许/良好的做法来扩展 Shape 类?