gpt4 book ai didi

javafx 形状、绑定(bind)坐标值、性能问题、如何从场景中删除

转载 作者:行者123 更新时间:2023-12-04 15:07:41 26 4
gpt4 key购买 nike

这是我预期应用程序的简化版本。在 JavaFX 15.0.1 上运行此代码,您将看到一个显示一堆蓝色线条和点的窗口,以及一个重新加载按钮。旋转鼠标滚轮可放大和缩小图片。
问题是:
单击“重新加载”按钮约 20 次,然后旋转鼠标滚轮。观察应用程序如何卡住几秒钟,有时 cpu 事件会达到 90%。如果没有任何可疑之处,请单击以进行更多重新加载。
所以我相信问题是如何从场景中删除所有的形状和绑定(bind),这一定是性能下降的原因,对吧?我做了一个pane.getChildren().clear();我在这里错过了什么吗?

package application;

import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javafx.application.Application;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;

public class Main extends Application {

private DoubleProperty zoom = new SimpleDoubleProperty(1.0);
private int w = 800;
private int h = 500;

@Override
public void start(Stage stage) {
try {
Button reload = new Button("reload");
Pane pane = new Pane();
pane.setManaged(false);

reload.setOnAction(event -> {
pane.getChildren().clear();
List<Displacement> disList = Stream.generate(Displacement::new).limit(1200).collect(Collectors.toList());;
disList.forEach(dis -> pane.getChildren().add(new DisplacementLine(dis, w, h, pane)));
disList.forEach(dis -> pane.getChildren().add(new DisplacementCircle(dis, w, h, pane)));
});

VBox vbox = new VBox(reload, pane);
Pane root = new Pane();
Scene scene = new Scene(root);
root.getChildren().add(vbox);
stage.setScene(scene);
stage.setWidth(1000);
stage.setHeight(600);
stage.show();

pane.prefWidthProperty().bind(zoom.multiply(w));
pane.prefHeightProperty().bind(zoom.multiply(h));
pane.layoutXProperty().bind(root.widthProperty().subtract(pane.prefWidthProperty()).divide(2.0));
pane.layoutYProperty().bind(root.heightProperty().subtract(pane.prefHeightProperty()).divide(2.0));

pane.setOnScroll(scrollEvent -> {
double delta = 0.1;
double f = scrollEvent.getDeltaY() > 0 ? 1+delta : 1/(1+delta);
zoom.set(zoom.get() * f);
});

reload.fire();

} catch(Exception e) {
e.printStackTrace();
}
}


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

class DisplacementLine extends Line {

final int w, h;
final double x1, y1, x2, y2;

public DisplacementLine(Displacement dis, int w, int h, Pane pane) {
this.w = w;
this.h = h;
x1 = dis.x;
y1 = dis.y;
x2 = dis.x + dis.u;
y2 = dis.y + dis.v;
startXProperty().bind(pane.prefWidthProperty().multiply(x1).divide(w));
startYProperty().bind(pane.prefHeightProperty().multiply(y1).divide(h));
endXProperty().bind(pane.prefWidthProperty().multiply(x2).divide(w));
endYProperty().bind(pane.prefHeightProperty().multiply(y2).divide(h));
setStroke(Color.BLUE);
setVisible(dis.valid);
}
}

class DisplacementCircle extends Circle {

final int w, h;
final double x, y;

public DisplacementCircle(Displacement dis, int w, int h, Pane pane) {
this.x = dis.x;
this.y = dis.y;
this.w = w;
this.h = h;
centerXProperty().bind(pane.prefWidthProperty().multiply(x).divide(w));
centerYProperty().bind(pane.prefHeightProperty().multiply(y).divide(h));
setRadius(1.5);
setFill(Color.BLUE);
setVisible(dis.valid);
}
}

class Displacement {

final static Random rand = new Random();
final double x, y, u, v;
final boolean valid;

public Displacement() {
x = rand.nextDouble() * 1000;
y = rand.nextDouble() * 600;
u = rand.nextDouble() * 30;
v = rand.nextDouble() * 15;
valid = rand.nextBoolean();
}
}
app window

最佳答案

基于 geometrical 的评论我把这个变化放在一起。这看起来很不错,我可以通过这种方式拥有不可变的对象。
在这里,我引入了函数来监听 Pane 宽度/高度属性的变化,然后设置每个形状的坐标,因此不会创建数千个绑定(bind)。
将使其适应我的最终项目

public class Main extends Application {

private DoubleProperty zoom = new SimpleDoubleProperty(1.0);
private int w = 800;
private int h = 500;

@Override
public void start(Stage stage) {
try {
Button reload = new Button("reload");
Pane pane = new Pane();
pane.setManaged(false);

Consumer<Number> setX = value -> {
double pw = value.doubleValue();
for (Node node : pane.getChildren()) {
if (node instanceof DisplacementLine) {
DisplacementLine dl = (DisplacementLine) node;
dl.setStartX(pw * dl.x1 / w);
dl.setEndX(pw * dl.x2 / w);
}
if (node instanceof DisplacementCircle) {
DisplacementCircle dc = (DisplacementCircle) node;
dc.setCenterX(pw * dc.x / w);
}
}
};
Consumer<Number> setY = value -> {
double ph = value.doubleValue();
for (Node node : pane.getChildren()) {
if (node instanceof DisplacementLine) {
DisplacementLine dl = (DisplacementLine) node;
dl.setStartY(ph * dl.y1 / h);
dl.setEndY(ph * dl.y2 / h);
}
if (node instanceof DisplacementCircle) {
DisplacementCircle dc = (DisplacementCircle) node;
dc.setCenterY(ph * dc.y / h);
}
}
};
pane.prefWidthProperty().addListener((obs, oldval, newval) -> setX.accept(newval));
pane.prefHeightProperty().addListener((obs, oldval, newval) -> setY.accept(newval));

reload.setOnAction(event -> {
pane.getChildren().clear();
List<Displacement> disList = Stream.generate(Displacement::new).limit(1200).collect(Collectors.toList());;
disList.forEach(dis -> pane.getChildren().add(new DisplacementLine(dis, pane)));
disList.forEach(dis -> pane.getChildren().add(new DisplacementCircle(dis, pane)));
setX.accept(pane.getPrefWidth());
setY.accept(pane.getPrefHeight());
});

VBox vbox = new VBox(reload, pane);
Pane root = new Pane();
Scene scene = new Scene(root);
root.getChildren().add(vbox);
stage.setScene(scene);
stage.setWidth(1000);
stage.setHeight(600);
stage.show();

pane.prefWidthProperty().bind(zoom.multiply(w));
pane.prefHeightProperty().bind(zoom.multiply(h));
pane.layoutXProperty().bind(root.widthProperty().subtract(pane.prefWidthProperty()).divide(2.0));
pane.layoutYProperty().bind(root.heightProperty().subtract(pane.prefHeightProperty()).divide(2.0));

pane.setOnScroll(scrollEvent -> {
double delta = 0.1;
double f = scrollEvent.getDeltaY() > 0 ? 1+delta : 1/(1+delta);
zoom.set(zoom.get() * f);
});

reload.fire();

} catch(Exception e) {
e.printStackTrace();
}
}


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

class DisplacementLine extends Line {

final double x1, y1, x2, y2;

public DisplacementLine(Displacement dis, Pane pane) {
x1 = dis.x;
y1 = dis.y;
x2 = dis.x + dis.u;
y2 = dis.y + dis.v;
setStroke(Color.BLUE);
setVisible(dis.valid);
}
}

class DisplacementCircle extends Circle {

final double x, y;

public DisplacementCircle(Displacement dis, Pane pane) {
this.x = dis.x;
this.y = dis.y;
setRadius(1.5);
setFill(Color.BLUE);
setVisible(dis.valid);
}
}

class Displacement {

final static Random rand = new Random();
final double x, y, u, v;
final boolean valid;

public Displacement() {
x = rand.nextDouble() * 1000;
y = rand.nextDouble() * 600;
u = rand.nextDouble() * 30;
v = rand.nextDouble() * 15;
valid = rand.nextBoolean();
}
}

关于javafx 形状、绑定(bind)坐标值、性能问题、如何从场景中删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65766334/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com