着色网格

时间:2018-02-23 04:47:50

标签: java grid

所以我试图为网格着色,使其看起来像一面旗帜。 "标志的上半部分"是红色的,而#34;标志的底部是#34;是白色的。到目前为止,我的代码如下:

public class FlagMaker {
    public static void drawFlag(MyGrid grid, int countryCode) {
        int width = grid.getWd();
        int height = grid.getHt();
        if (countryCode == 1) {
            for (int rowNum = 0; rowNum < height; rowNum++) {
                for (int colNum = 0; colNum < width; colNum++) {
                    grid.setColor(rowNum, colNum, Color.RED);
                }
            }
            for (int rowNum = 0; rowNum > height ; rowNum++) {
                for (int colNum = 0; colNum > width; colNum++)
                grid.setColor(rowNum/2, colNum, Color.WHITE);

            }
        }
    }
}

但是,这段代码只会将整个标志的颜色变为红色,而我仍然坚持如何让标志的下半部分为白色。谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

public class FlagMaker {
    public static void drawFlag(MyGrid grid, int countryCode) {
        int width = grid.getWd();
        int height = grid.getHt();
        if (countryCode == 1) {
            for (int rowNum = 0; rowNum < height/2; rowNum++) {
                for (int colNum = 0; colNum < width; colNum++) {
                    grid.setColor(rowNum, colNum, Color.RED);
                }
            }
            for (int rowNum = height/2; rowNum < height ; rowNum++) {
                for (int colNum = 0; colNum < width; colNum++) {
                    grid.setColor(rowNum/2, colNum, Color.WHITE);
                }
            }
        }
    }
}