【发布时间】:2014-11-13 05:16:42
【问题描述】:
我需要在图像标签上绘制 10 个多边形的大约 10 组坐标。这是我迄今为止编写的代码:
但是由于我不能单独调用paintComponent,所以我在实例化JLabel 时调用它,这会导致问题。最后,我只是在图像上绘制了最后一个多边形,因为每次创建新的 jLabel 时。有人可以指出如何改进,以便我可以在同一个 JLabel 上绘制多个多边形。
private void setMapImage()
{
Queries queries = new Queries(dbConnection, connection);
List<ArrayList<Integer>> allGeo = queries.getBuilding();
for(int i = 0; i < allGeo.size(); i++)
{
int[] xPoly = queries.separateCoordinates(allGeo.get(i),0);
int[] yPoly = queries.separateCoordinates(allGeo.get(i),1);
poly = new Polygon(xPoly, yPoly, xPoly.length);
poly = new Polygon(xPoly, yPoly, xPoly.length);
background=new JLabel(new ImageIcon("image.JPG"),SwingConstants.LEFT)
{
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.YELLOW);
g.drawPolygon(poly);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(820, 580);
}
};
}
background.setVerticalAlignment(SwingConstants.TOP);
frame.add(background);
background.setLayout(new FlowLayout());
frame.setVisible(true);
}
【问题讨论】:
-
我不确定为什么要专门在 JLabel 上绘画。您是否考虑过使用玻璃板。这是Java tutorial,以防您想查看它。