2011-11-21 77 views
0

我正在嘗試爲Java遊戲做一個isometric地圖,但我無法找到一個方法來做到這一點。我需要爲等軸測圖的每個多邊形添加一個JLabel,因此我可以爲每個位置使用paint()。我如何爲每個繪製的多邊形添加一個JLabel?我無法得到它。我已經有我的算法得出我的軸測圖的每一個位置,這樣的事情:如何在多邊形內添加JLabel?

//L is the width of the map (that will be the framw width) 
//N will be the number of COLUMN, like N*N will be the total number of positions. 

//The first position (a,b) that will be 

a=L/(2*N+1) 
b=a . tan(30º 

for (int y = 0; y < N; y++) { 
    if (y % 2 == 0) { // Se y é PAR 
     for (int x = 0; x < N; x++) { 
      Polygon p = new Polygon(); 
      p.addPoint(x * a * 2 + a, y * b); 
      p.addPoint(x * a * 2 + 2 * a, y * b + b); 
      p.addPoint(x * a * 2 + a, y * b + 2 * b); 
      p.addPoint(x * a * 2, y * b + b); 
      g.drawPolygon(p); 
     } 
    } else { // if Y is odd 
    for (int x = 0; x < N; x++) { 
     Polygon p = new Polygon(); 
     p.addPoint(x * a * 2 + 2 * a, y * b); 
     p.addPoint(x * a * 2 + 3 * a, y * b + b); 
     p.addPoint(x * a * 2 + 2 * a, y * b + 2 * b); 
     p.addPoint(x * a * 2 + a, y * b + b); 
     g.drawPolygon(p); 
    } 
    } 
} 

非常感謝提前傢伙

+0

JLabel是否必須在多邊形內?添加它到底是什麼問題?此外你在繪製你的多邊形? –

+0

發佈您的SSCCE,顯示多邊形的繪畫以及框架的大小如何影響多邊形。 – camickr

回答

2

佈局和繪圖是真的很不同。佈局不是關於確切的定位。你也可以在所需位置的Graphics對象上使用drawChars,或者將它轉換爲Graphics2D對象並使用drawString。

+0

我想在等距地圖的每個位置上使用標籤,因爲在某些位置會有代表我的遊戲英雄的圖像,你明白嗎? 在某些情況下,標籤將有英雄..另一個案件將有一座建築.. 我可以通過使用每個位置上的標籤,然後使用每個標籤的方法塗料來操縱。 感謝你們的幫助,我希望你能理解我:) – TiagoM