JavaFX canvas GraphicsContext.drawImage 巨大的滞后

标签 java canvas javafx

我正在制作一个 map 编辑器,使用 JavaFX 作为 UI,并使用自定义 Canvas 来绘制用户绘制 map 的组件。

这是我嵌入到 map 编辑器中的 Canvas 组件

public class EditorEngine extends Canvas {

    public Level level;
    public MouseHandler mouse;

    @SuppressWarnings("unchecked")
    public EditorEngine(Level level, ReadOnlyDoubleProperty widthProperty, ReadOnlyDoubleProperty heightProperty) {
        super(widthProperty.getValue(), heightProperty.getValue());

        this.level = level;
        level.engine = this;
        this.widthProperty().bind(widthProperty);
        this.heightProperty().bind(heightProperty);
        this.mouse = new MouseHandler(this);

        Duration frameDuration = Duration.millis(1000 / 42);

        @SuppressWarnings("rawtypes")
        KeyFrame frame = new KeyFrame(frameDuration, new EventHandler() {
            @Override
            public void handle(Event event) {
                update();
                render();
            }
        });

        new Timeline(60).setCycleCount(Animation.INDEFINITE);
        TimelineBuilder.create().cycleCount(Animation.INDEFINITE).keyFrames(frame).build().play();
    }

    public int tick = 0;

    public void update() {
        tick++;

        if (level != null) {
            level.update();
        }

        mouse.update();
    }

    public void render() {
        GraphicsContext g = getGraphicsContext2D();

        g.clearRect(0, 0, getWidth(), getHeight());
        g.fillRect(mouse.mx - 5, mouse.my - 5, 10, 10); //for testing and stuff

        if (level != null) {
            level.render(g);
        }

    }
}

但这不是问题发生的地方。看看我的 Level.draw 方法

public void render(GraphicsContext g) {
    for (int i = 0; i < w; i++) {
        for (int j = 0; j < h; j++) {
            if (getTile(i, j) == null) continue;

            Tileset t = Project.current.world.getTileset(tiles[j * w + i].tileset);

            if (t.sprite != null) {
                g.drawImage(t.sprite.get(), tiles[index(i, j)].sprite % t.w, tiles[index(i, j)].sprite / t.w, 16, 16, i * 16, j * 16, 16, 16);
            } else {
                System.out.println("Warning: Tileset.sprite == null!");
            }
        }
    }
}

这是错误的线路

g.drawImage(t.sprite.get(), tiles[index(i, j)].sprite % t.w, tiles[index(i, j)].sprite / t.w, 16, 16, i * 16, j * 16, 16, 16);

对两个或三个图 block 调用此方法会有点滞后,并且在任何接近 5 个以上图 block 的地方都会完全卡住应用程序。我 100% 认为这是问题所在,因为没有该行它运行 100% 正常,但一旦将其添加回来(在 Debug模式下)并保存,软件立即卡住。

有人知道为什么会发生这种情况吗?谢谢

最佳答案

没关系。这是我的一个错误,我完全忽略了这一点。 t.sprite 是我自己的 LazySprite 类,一个仅在需要时才从 HDD 加载的 Sprite 。事实证明,我在加载后忘记将其 boolean 值“initialized”设置为 true,因此它每帧都会从我的 HDD 加载图 block 集。

关于JavaFX canvas GraphicsContext.drawImage 巨大的滞后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21444287/

相关文章:

javafx-2 - 如何设置JavaFX默认皮肤

java - 无法从java应用程序访问mysql数据库

android - 我可以在 Android 2.1 的基于 Webkit 的浏览器中使用数据 URL 吗?

javascript - 我需要一个系统来发布 JavaScript 游戏的高分,但它必须很难被愚弄

c# - 在 WPF 中调整网格内的 Canvas

java - 从 Java 8u72 开始,向 ComboBox#box.getSelectionModel()#selectedItemProperty() 添加监听器会导致异常

java - 在不知道父节点的情况下删除节点 (JavaFX)

java - MapStruct @MappingTarget 生成一个空方法

java - 让 Facebook 页面谈论一段时期的值(value)?

java - 从 Git 自动部署 Grails/Java web 应用程序到 Tomcat7