(Java)将1x1像素转换为3x3像素

时间:2014-11-17 22:04:28

标签: java

是否需要我编写一个程序来读取包含40,000个数字的数据文件并使用这些数字,在大小为200x200的图形窗口中显示图像。

现在问题让我用600x600屏幕替换之前的200x200显示器,其中每个数字变成3x3像素块。

//Setting the size of the graphics window
final int WINDOW_DIMENSION = 200;
EasyGraphics generate = new EasyGraphics(WINDOW_DIMENSION,WINDOW_DIMENSION);

//Constructing the arrays
    char[][] firstArray = new char[WINDOW_DIMENSION][WINDOW_DIMENSION];
    char[] secondArray = scan.toCharArray();

    //Using "for" loop to generate 2D array
    int line = WINDOW_DIMENSION;
    int column = -1;

    for (int i = 0; i < WINDOW_DIMENSION*WINDOW_DIMENSION; i++) {
        column++;
        if (i%WINDOW_DIMENSION == 0) {
            line--;
            column = 0;
        }
        firstArray[column][line]=secondArray[i];
    }

    //Plotting the colours for respective range of numbers
    for (int a = 0; a < WINDOW_DIMENSION; a++) {
        for (int b = 0; b < WINDOW_DIMENSION; b++) {
            if (firstArray[a][b]=='0' || firstArray[a][b]=='1' || firstArray[a][b]=='2' || firstArray[a][b]=='3') {
                generate.setColor(66,167,243);
                generate.plot(a,b);
            } 

            else if (firstArray[a][b]=='4' || firstArray[a][b]=='5') {
                generate.setColor(0,255,77);
                generate.plot(a,b);
            } 

            else if (firstArray[a][b]=='6' || firstArray[a][b]=='7') {
                generate.setColor(51,183,91);
                generate.plot(a,b);
            } 

            else if (firstArray[a][b]=='8' || firstArray[a][b]=='9') {
                generate.setColor(102,51,0);
                generate.plot(a,b);
            }
        }
    }
}

有关如何解决此问题的任何解决方案?

2 个答案:

答案 0 :(得分:1)

如果没有关于generateplot的更多信息,我们只能给出“通用”答案:无论generate.plot(a,b)做什么,都必须做3到3次。因此,用

替换所有对generate.plot(a,b);的调用就足够了
for (int da=0; da<3; da++) {
    for (int db=0; db<3; db++) {
        generate.plot(a*3+da,b*3+db);
    }
}

编辑:用上面的代码替换每个generate.plot(a,b)

在:

if (firstArray[a][b]=='0' || firstArray[a][b]=='1' || 
    firstArray[a][b]=='2' || firstArray[a][b]=='3') {
    generate.setColor(66,167,243);
    generate.plot(a,b);
} 

后:

if (firstArray[a][b]=='0' || firstArray[a][b]=='1' || 
    firstArray[a][b]=='2' || firstArray[a][b]=='3') {
    generate.setColor(66,167,243);
    for (int da=0; da<3; da++) {
        for (int db=0; db<3; db++) {
            generate.plot(a*3+da,b*3+db);
        }
    }
} 

答案 1 :(得分:0)

    //Setting the size of the graphics window
    final int WINDOW_DIMENSION = 200;
    EasyGraphics generate = new EasyGraphics(WINDOW_DIMENSION*3,WINDOW_DIMENSION*3);

    //Constructing the arrays
    char[][] firstArray = new char[WINDOW_DIMENSION][WINDOW_DIMENSION];
    char[] secondArray = scan.toCharArray();

    //Using "for" loop to generate 2D array
    int line = WINDOW_DIMENSION;
    int column = -1;

    for (int i = 0; i < WINDOW_DIMENSION*WINDOW_DIMENSION; i++) {
        column++;
        if (i%WINDOW_DIMENSION == 0) {
            line--;
            column = 0;
        }

        firstArray[column][line]=secondArray[i];
    }

    //Plotting the colours for respective range of numbers
    for (int a = 0; a < WINDOW_DIMENSION; a++) {
        for (int b = 0; b < WINDOW_DIMENSION; b++) {
            if (firstArray[a][b]=='0' || firstArray[a][b]=='1' || firstArray[a][b]=='2' || firstArray[a][b]=='3') {
                generate.setColor(66,167,243);
                for (int da=0; da<3; da++) {
                    for (int db=0; db<3; db++) {
                      generate.plot(a*3+da,b*3+db);
                    }
                }
            } 

            else if (firstArray[a][b]=='4' || firstArray[a][b]=='5') {
                generate.setColor(0,255,77);
                for (int da=0; da<3; da++) {
                    for (int db=0; db<3; db++) {
                      generate.plot(a*3+da,b*3+db);
                    }
                }
            } 

            else if (firstArray[a][b]=='6' || firstArray[a][b]=='7') {
                generate.setColor(51,183,91);
                for (int da=0; da<3; da++) {
                    for (int db=0; db<3; db++) {
                      generate.plot(a*3+da,b*3+db);
                    }
                }
            } 

            else if (firstArray[a][b]=='8' || firstArray[a][b]=='9') {
                generate.setColor(102,51,0);
                for (int da=0; da<3; da++) {
                    for (int db=0; db<3; db++) {
                      generate.plot(a*3+da,b*3+db);
                    }
                }
            } 
        }
    }
}