如何确保OverlayLayout中的绘制顺序正确? [英] How can I ensure correct drawing order in an OverlayLayout?

查看:187
本文介绍了如何确保OverlayLayout中的绘制顺序正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JPanelOverlayLayout互相绘制两个不相关的组件.具体来说,我有一个透明组件,其中包含我自己的线条图,在此之下,我正在使用JMapViewer组件,该组件显示OpenStreetMap切片.这行得通.

I am using a JPanel with an OverlayLayout to draw two unrelated components on top of each other. Specifically, I have a transparent component which contains my own line drawings, and beneath that, I am using a JMapViewer component, which displays OpenStreetMap tiles. This works.

除非JMapViewer异步加载图块.在这种情况下,加载完成后它会自行调用repaint(),并将其绘制到我的线层上.我的线层不知道这一点,因为它不知道JMapViewer选择何时重新绘制自身.

Except when the JMapViewer loads tiles asynchronously. In that case, it calls repaint() on itself when the loading is finished, and it draws itself over my line layer. My line layer cannot know this, since it cannot know when the JMapViewer chooses to repaint itself.

现在,我本应期望JPanel在重新绘制地图图层时重新绘制我的线图层,因为它知道正确的绘制顺序并且知道必须重新绘制我的线图层.但这并没有做到.

Now I would have expected the JPanel to repaint my line layer when the map layer repaints, since it knows the correct drawing order and knows that my line layer has to be repainted. But it doesn't do this.

您将如何解决?

推荐答案

JMapViewer 实现TileLoaderListener,并在加载完成后简单地调用repaint().您应该能够更新地图,然后将事件转发到您的图层,如下所示.

JMapViewer implements TileLoaderListener and simply calls repaint() when loading is finished. You should be able to update the map and then forward the event to your layer, as suggested below.

MyViewer map = new MyViewer();
...
private class MyViewer extends JMapViewer {

    @Override
    public void tileLoadingFinished(Tile tile, boolean success) {
        super.tileLoadingFinished(tile, success);
        lineLayer.repaint(); 
    }
}

这篇关于如何确保OverlayLayout中的绘制顺序正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆