如何在多边形内添加JLabel?

时间:2011-11-21 00:24:43

标签: java map jlabel isometric

我正在尝试为Java上的游戏制作isometric地图,但我找不到办法来做到这一点。我需要为等轴测图的每个多边形添加JLabel,因此我可以对每个位置使用paint()。如何为我绘制的每个多边形添加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);
    }
  }
}

非常感谢你们

1 个答案:

答案 0 :(得分:2)

布局和绘图真的很不一样。布局与精确定位无关。您也可以在想要的位置使用Graphics对象上的drawChars,或者将其转换为Graphics2D对象并使用drawString。

相关问题