首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JavaFX画布GraphicsContext.drawImage的巨大滞后

JavaFX画布GraphicsContext.drawImage的巨大滞后
EN

Stack Overflow用户
提问于 2014-01-29 22:44:12
回答 1查看 1.3K关注 0票数 1

我正在为UI制作一个使用JavaFX的地图编辑器,并使用一个自定义画布来绘制组件,用户将在其中绘制映射。

这是我要嵌入到地图编辑器中的画布组件。

代码语言:javascript
复制
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方法

代码语言:javascript
复制
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!");
            }
        }
    }
}

这是有毛病的线路

代码语言:javascript
复制
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);

对两个或三个瓷砖进行调用会有一点滞后,而且在接近5+的任何地方,tiles都会完全冻结应用程序。我100%认为这是问题,因为它运行100%良好没有该行,但一旦它添加回(在调试模式)和保存,软件立即冻结。

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-01-30 03:08:12

不要紧。这是我的一个错误,我完全忽略了这一点。t.sprite是我自己的LazySprite类,它是一个只有在需要时才从硬盘加载的精灵类。结果是,在加载之后,我忘了将它的布尔值“初始化”设置为true,因此它继续从我的HDD加载每个帧的tileset。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21444287

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档