2013-03-27 29 views
0

我试图从ArrayList上画一个图标的网格到我的canvas之后,每隔10个图标之后,下一个图标将显示在一个新的行上,但似乎无法让它正常工作。第一图标的开始X和Y位置为100,100:画一个图标的网格

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; 
     } 
    } 

这将在一个水平行显示的每个图标,但无法弄清楚如何后获得第11和第10的每从新行开始。

回答

1

你只是忘记添加“换行符”,当它检测到它是第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 
}