我正在为UI制作一个使用JavaFX的地图编辑器,并使用一个自定义画布来绘制组件,用户将在其中绘制映射。
这是我要嵌入到地图编辑器中的画布组件。
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);对两个或三个瓷砖进行调用会有一点滞后,而且在接近5+的任何地方,tiles都会完全冻结应用程序。我100%认为这是问题,因为它运行100%良好没有该行,但一旦它添加回(在调试模式)和保存,软件立即冻结。
有人知道为什么会发生这种事吗?谢谢
发布于 2014-01-30 03:08:12
不要紧。这是我的一个错误,我完全忽略了这一点。t.sprite是我自己的LazySprite类,它是一个只有在需要时才从硬盘加载的精灵类。结果是,在加载之后,我忘了将它的布尔值“初始化”设置为true,因此它继续从我的HDD加载每个帧的tileset。
https://stackoverflow.com/questions/21444287
复制相似问题