2011-04-29 30 views
3

我正在使用JPanelOverlayLayout來繪製兩個不相關的組件。具體來說,我有一個透明的組件,其中包含我自己的線條圖,在其下面,我使用了一個顯示OpenStreetMap圖塊的JMapViewer組件。這工作。如何確保OverlayLayout中的正確繪圖順序?

除JMapViewer異步加載tile以外。在這種情況下,當加載完成時它自己調用repaint(),並且它通過我的線路層自行繪製。我的線路層無法知道這一點,因爲它無法知道JMapViewer何時選擇重新繪製自己。

現在我會預計JPanel重繪我的線路層時,地圖層重繪,因爲它知道正確的繪圖順序,並知道我的線路層必須重新繪製。但它不這樣做。

你會如何解決這個問題?

回答

2

JMapViewer實現TileLoaderListener從AWT事件線程做到這一點,在加載完成時只需調用repaint()。您應該能夠更新地圖,然後將事件轉發到您的圖層,如下所示。

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

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

這取決於JMapViewer如何重新繪製自己。如果它只是更新其內容,那麼父母可能甚至不會注意到有什麼變化。

1

我不知道這是否是最好的方法,但它是我能想到的唯一方法。您可以使用自定義的RepaintManager替換JMapComponent的RepaintManager,該自定義控件還可以管理覆蓋組件的重繪。這樣,您可以保證您的覆蓋組件總是最後重新繪製。

...通過SwingUtilities的

RepaintManager myManager = new RepaintManager() { 
    public void addDirtyRegion(JComponent c, int x, int y, int w, int h) { 
....implemnt code here to guarentee your top level component gets repainted whenever 'c' is the JMapComponent 
    } 
} 

RepaintManager.setCurrentManager(myManager); 
+0

謝謝。我現在正在處理*另一個*地圖查看器組件,因爲垃圾回收的方法不起作用(因爲它沒有偵聽器的鉤子),但是你的方法卻行。 – 2011-08-03 15:33:15

2

當加載完成後調用重繪()本身,

調用重繪()父面板代替。