Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于循环网格的Java AWT图形_Java_For Loop_Awt - Fatal编程技术网

用于循环网格的Java AWT图形

用于循环网格的Java AWT图形,java,for-loop,awt,Java,For Loop,Awt,我需要一些在JavaAWT中创建网格的帮助。你会得到一定数量的列,现在让我们假设13列 for(int i = 0; i < 13; i++) { Graphics.fillRect((i * 15), 10, 10, 10); } 这将使所有立方体彼此相邻。这不是我想要的。我需要的y位置下降15像素,每次4个立方体已经绘制 结果将是 XXXX XXXX XXXX X 谢谢你的时间 只需使用doublefor()循环即可 for(int i = 0; i < 4; i+

我需要一些在JavaAWT中创建网格的帮助。你会得到一定数量的列,现在让我们假设13列

for(int i = 0; i < 13; i++)
{
    Graphics.fillRect((i * 15), 10, 10, 10);
} 
这将使所有立方体彼此相邻。这不是我想要的。我需要的y位置下降15像素,每次4个立方体已经绘制

结果将是

XXXX
XXXX
XXXX
X

谢谢你的时间

只需使用double
for()
循环即可

for(int i = 0; i < 4; i++){
    for(int j = 0; j < 4; j++) {
        Graphics.fillRect(i * 15, j * 15, 10, 10);
    }
} 
for(int i=0;i<4;i++){
对于(int j=0;j<4;j++){
Graphics.fillRect(i*15,j*15,10,10);
}
} 
这将创建一个4x4的矩形网格。如果要将其限制为一个类似4x4的网格,该网格在最后一行中只有一个矩形,这似乎是您想要的,请使用
j<((i==3)?1:4)
作为第二个for循环中的终止条件,或者使用简单的If条件,例如:

for(int i = 0; i < 4; i++){
    for(int j = 0; j < 4; j++) {
        Graphics.fillRect(i * 15, j * 15, 10, 10);
        if(i == 3) break;
    }
} 
for(int i=0;i<4;i++){
对于(int j=0;j<4;j++){
Graphics.fillRect(i*15,j*15,10,10);
如果(i==3)中断;
}
} 
编辑:通用解决方案
intnrow、nCol、nLastCol;
//初始化值
for(int i=0;i
您需要设置列数并跟踪正在绘制的列,以便可以增加行数。另一方面,我也不会在循环中放入一个神奇的数字,而是将其作为一个变量(只是良好的编程实践)。大概是这样的:

int numRectangles = 13;
int numColumns = 4;//set this to however many columns you need
int currentRow = 1;//start at one, not zero
int tempRow = 1;
for(int i = 0; i < numRectangles; i++)
{
    Graphics.fillRect((i * 15), (tempRow * 10), 10, 10);
    currentRow++;
    if(currentRow > numColumns) {
        currentRow = 1;
        tempRow++;
    }
} 
int numRectangles=13;
int numColumns=4//将此设置为所需的列数
int currentRow=1//从一开始,不是零开始
int tempRow=1;
对于(int i=0;inumColumns){
currentRow=1;
tempRow++;
}
} 

基本上,您必须增加
x
,每次达到极限时,您将x恢复为零,并增加
y

// These should be constants, defined at the class level
public static final int NUM_RECTANGLES = 13;
public static final int NUM_COLUMNS = 4;

// And your loop
int col = 0;
int row = 0;
for(int i = 0; i < NUM_RECTANGLES; i++)
{
    Graphics.fillRect((col * 15), 10 + (row * 15), 10, 10);
    col++;
    if( col == NUM_COLUMNS ) {
        col = 0;
        row++;
    }
}
//这些应该是常量,在类级别定义
公共静态最终int NUM_矩形=13;
公共静态final int NUM_COLUMNS=4;
//还有你的循环
int col=0;
int行=0;
对于(int i=0;i
如您所见,在每个矩形之后,增加列。如果达到列的限制(记住从0开始),则移动到下一行。这意味着该列再次为0,并且该行增加


当然,如果你的
13
4
实际上是参数,请忽略我关于常数的评论。

…?是的,对不起,我会编辑postWell,这是我的问题,这将始终提供一个均匀的网格。而且永远不会画出像13这样的东西。应该是if
if(i==3)break因为从未到达
i==4
。我已经相应地编辑了我的文章。这只有在你知道有多少行的情况下才有效。如果您知道列数而不知道行数,那么如果不进行一些调整,这将无法工作。我已编辑了我的帖子,以包含一个通用解决方案。@KatsuKokushi我刚刚修复了它,对此表示抱歉。
int numRectangles = 13;
int numColumns = 4;//set this to however many columns you need
int currentRow = 1;//start at one, not zero
int tempRow = 1;
for(int i = 0; i < numRectangles; i++)
{
    Graphics.fillRect((i * 15), (tempRow * 10), 10, 10);
    currentRow++;
    if(currentRow > numColumns) {
        currentRow = 1;
        tempRow++;
    }
} 
// These should be constants, defined at the class level
public static final int NUM_RECTANGLES = 13;
public static final int NUM_COLUMNS = 4;

// And your loop
int col = 0;
int row = 0;
for(int i = 0; i < NUM_RECTANGLES; i++)
{
    Graphics.fillRect((col * 15), 10 + (row * 15), 10, 10);
    col++;
    if( col == NUM_COLUMNS ) {
        col = 0;
        row++;
    }
}