gpt4 book ai didi

java - 绘制图标网格

转载 作者:行者123 更新时间:2023-12-01 14:43:57 24 4
gpt4 key购买 nike

我正在尝试将 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 个图标以开始新行。

最佳答案

当它检测到这是第 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
}

关于java - 绘制图标网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15656894/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com