首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >网格停止在10 x 50而不是50 x 50

网格停止在10 x 50而不是50 x 50
EN

Stack Overflow用户
提问于 2020-02-09 02:59:18
回答 2查看 31关注 0票数 0

我试图使用绘图面板生成一个50x50的网格,但由于某种原因,它停在了50x10。我正在使用Intellij,如果这是相关的。

代码语言:javascript
复制
protected void paintComponent(Graphics g){
    super.paintComponent(g);

    int theChangingX = 0;
    int theChangingY = 0;

    for (int row = 0; row <= 2500 + 1; row++){
        g.setColor(Color.red);
        g.drawRect(theChangingX,theChangingY, pixelsize, pixelsize );
        theChangingX+=10;
        for (int col = 0; col<=2500 + 1; col++ ){
            if (theChangingX ==2500){
                theChangingY+=10;
                theChangingX =-10;
            }
        }
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-02-09 03:08:12

我认为你把代码弄得太复杂了,我会这样做的:

代码语言:javascript
复制
 protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(Color.red);
    for (int col = 0; col < 50; col++) {
        for (int row = 0; row < 50; row++) {
            g.drawRect(row * cellSize, col * cellSize, cellSize, cellSize);
        }
    }
}
票数 3
EN

Stack Overflow用户

发布于 2020-02-09 03:09:48

尝尝这个

代码语言:javascript
复制
protected void paintComponent(Graphics g){
    super.paintComponent(g);

    int theChangingX = 0;
    int theChangingY = 0;

    for (int row = 0; row < 50; row++){
        g.setColor(Color.red);
        theChangingX = row*10;

        for (int col = 0; col < 50 ;col++ ){
            theChangingY = col*10;
            g.drawRect(theChangingX,theChangingY, pixelsize, pixelsize );
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60130160

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档