Java 绘制一个图标网格

Java 绘制一个图标网格,java,canvas,for-loop,grid,Java,Canvas,For Loop,Grid,我正在尝试将ArrayList中的图标网格绘制到我的canvas上,每隔10个图标,下一个图标就会显示在新行上,但似乎无法使其正常工作。第一个图标的起始X和Y位置为100100: int x = 32; // Dimensions of icons int y = x; for (int pos = 0; pos < icons.getIcon().size(); pos++) { if(pos % 10 == 0) {

我正在尝试将
ArrayList
中的图标网格绘制到我的
canvas
上,每隔10个图标,下一个图标就会显示在新行上,但似乎无法使其正常工作。第一个图标的起始X和Y位置为100100:

int x = 32; // Dimensions of icons
int y = x;

for (int pos = 0; pos < icons.getIcon().size(); pos++)
    {
        if(pos % 10 == 0)
        {

            icons.getIcon().get(pos).paintIcon(canvas, graphics, posX, posY);
        }
        else
        {
            icons.getIcon().get(pos).paintIcon(canvas, graphics, posX, posY);
            posX += x + 10;
        }
    }
int x=32;//图标的尺寸
int y=x;
对于(int pos=0;pos

这将在水平行中显示每个图标,但无法计算如何获得第11个和第10个图标,以便在新的行上开始。

当检测到它是第11个图标时,您只是忘记添加“换行符”。诸如此类:

int x = 32; // Dimensions of icons
int y = x;
int posX = 100;
int posY = 100;

for (int pos = 0; pos < icons.getIcon().size(); pos++) {
    if(pos % 10 == 0) {
        posY += y + 10;
        posX = 100; // Returns posX back to the left-most position
        icons.getIcon().get(pos).paintIcon(canvas, graphics, posX, posY);
    } else {
        icons.getIcon().get(pos).paintIcon(canvas, graphics, posX, posY);
    }
    posX += x + 10; // Do that out of the if, so that posX is incremented either way
}
int x=32;//图标的尺寸
int y=x;
int posX=100;
int-posY=100;
对于(int pos=0;pos