旋转数组,同时保持Java中组件的方向

问题描述 投票:1回答:1

对于我正在从事的项目,我必须阅读一个字体精灵表(如下所示,将font.png转换成一个索引数组。

enter image description here

[读取文件并将其转换为数组时,我使用下面的代码:

    BufferedImage img = ImageIO.read(new File("D:\\Downloads\\font.png"));
    int[][] font = new int[8][760];
    for(int i = 0; i < img.getWidth(); i++) {
        for(int j = 0; j < img.getHeight(); j++) {
            font[j][i] = (img.getRGB(i, j) != - 1) ? 1:0;
        }
    }
    System.out.println(Arrays.deepToString(font).replace("], ", "]\n").replace("[[", "[").replace("]]", "]"));

哪个给我一个8x760 int [] [],其中每个黑色像素为1,每个白色像素为0(图像中的字符为空格,!,“,#)enter image description here

为了将阵列旋转为760x8阵列,我尝试了以下代码:

    int[][] rFont = new int[760][8];
    for(int a = 0; a < 95; a++) {
        for(int i = 0; i < 8; i++) {
            for(int j = 0; j < 8; j++) {
                rFont[i + 8*a][j] = font[j][i + 8*a];
            }
        }
    }
    System.out.println(Arrays.deepToString(rFont).replace("], ", "]\n").replace("[[", "[").replace("]]", "]"));

此代码可以完美地旋转数组,除非字体中的每个字母也旋转了90度。如下面的图片所示,它清楚地表明“!”也旋转。enter image description here

我在代码中哪里出了错,无法保持字符的方向?

java arrays matrix bufferedimage
1个回答
0
投票

替换

        for(int j = 0; j < 8; j++) {
            rFont[i + 8*a][j] = font[j][i + 8*a];
        }

with

                for (int j = 7; j >= 0; j--) {
                    rFont[i + 8 * a][7 - j] = font[j][i + 8 * a];
                }
© www.soinside.com 2019 - 2024. All rights reserved.