main() 打印多维数组的值不同于 paint()

main() prints out values of multidimensional array differently from paint()

我正在尝试使用 paint() 在屏幕上绘制矩形。如果我的数组中的某个位置有一个 1,那么在屏幕上它将是一个蓝色矩形。如果我的数组中的某个位置有一个 0,那么在屏幕上它将是一个黑色矩形。我通过访问 .bmp 文件并读取行来创建此数组。然后使用 .toCharArray() 将这些行(本质上是字符串)转换为字符数组,然后转换为整数数组。所以这个最终确定的数组填充了 1 和 0 整数。然后我进入 paint(),调用函数 getBits() 创建数组,并将其存储在 numArray,这是一个二维数组。出于调试目的,我在 main() 中调用了 getBits() 并打印出数组,结果为:

1000000000
1101000000
1111000000
0000000000
1001000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000

如果它在 x-y 坐标系中,这是数组的正确输出。 ^

然而,当我在 paint() 中调用 getBits() 并将其存储在 numArray 中,然后继续执行我的条件来检查它是 1 还是 0 时,它总是选择 0。似乎有一个错误有些种类,一切都以某种方式更改为 0。但我知道该数组包含 1,因为 main() 中的调试在上面的示例输出中打印出了 1 和 0。

public class bitmaps extends JApplet{
public void init(int[][] numArray){
    getContentPane().setBackground(Color.red);
}

//This function reads from a bitmap file and stores the characters (0s and 1s) into arrayLists
public static int[][] getBits(){
    File bitmap;
    Scanner reader;
    int[][] numArray = new int[20][10];

    try{
        bitmap = new File("C:/Users/kingsman142/Desktop/Projects/bitmap.bmp");
        reader = new Scanner(bitmap);

        int row = 0;
        int column = 0;

        String readStrings = "";

        //While there is more stuff in the file
        while(reader.hasNextLine()){
            readStrings = reader.nextLine();

            //Run through each line, grab strings, turn into char arrays, turn those into integers and add them to numArray
            for(column = 0; column < readStrings.toCharArray().length; column++){
                numArray[row][column] = Character.getNumericValue(readStrings.toCharArray()[column]);
            }

            //Assign all other values that haven't been assigned yet to 0
            for(column = column; column < 10; column++){
                numArray[row][column] = 0;
            }
            row++;
        }

        reader.close();
    } catch(Exception e){

    }

    //return all of the 1s and 0s
    return numArray;
}

public void paint(Graphics g){
    super.paint(g);
    g.setColor(Color.black);

    int[][] numArray = getBits();

    int row = 0;
    int column = 0;

    for(row = 0; row < 20; row++){
        for(column = 0; column < 10; column++){
            //If it's a 0, make it a blue rectangle
            //If it's a 1, make it a black rectangle
            //Else, make it a yellow rectangle (never had this problem yet)
            if(numArray[row][column] == 1){
                g.setColor(Color.blue);
            } else if(numArray[row][column] == 0){
                g.setColor(Color.black);
            } else{
                g.setColor(Color.yellow);
            }

            //Draw the rectangle
            g.fillRect(column*10, row*10, 10, 10);
        }
    }
}

public static void main(String[] args){
    int[][] numArray = getBits();

    //Print out the array (output of this is in the question)
    for(int row = 0; row < 20; row++){
        for(int column = 0; column < 10; column++){
            System.out.print(String.valueOf(numArray[row][column]) + " ");
        }
        System.out.println("");
    }
}

奇怪的是,如果我将 numArray 放在全局范围内并自己初始化每个位置,我就可以解决这个问题。问题是我不想为我的程序这样做,因为我想使用任何位图。

这是我的输出应该是什么样子以及它实际是什么样子:

[

所以我的问题是...为什么我的 main() 函数对 numArray 的看法与 paint() 不同?我该如何解决这个问题?

一旦我提供了我自己的 bitmap.bmp 文件,代码似乎打印得很好

import java.awt.Color;
import java.awt.Graphics;
import java.io.File;
import java.util.Scanner;
import javax.swing.JApplet;

public class bitmaps extends JApplet {

    public void init(int[][] numArray) {
        getContentPane().setBackground(Color.red);
    }

//This function reads from a bitmap file and stores the characters (0s and 1s) into arrayLists
    public static int[][] getBits() {
        File bitmap;
        Scanner reader;
        int[][] numArray = new int[20][10];

        try {
            bitmap = new File("bitmap.bmp");
            reader = new Scanner(bitmap);

            int row = 0;
            int column = 0;

            String readStrings = "";

            //While there is more stuff in the file
            while (reader.hasNextLine()) {
                readStrings = reader.nextLine();

                //Run through each line, grab strings, turn into char arrays, turn those into integers and add them to numArray
                for (column = 0; column < readStrings.toCharArray().length; column++) {
                    numArray[row][column] = Character.getNumericValue(readStrings.toCharArray()[column]);
                }

                //Assign all other values that haven't been assigned yet to 0
                for (column = column; column < 10; column++) {
                    numArray[row][column] = 0;
                }
                row++;
            }

            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        //return all of the 1s and 0s
        return numArray;
    }

    public void paint(Graphics g) {
        super.paint(g);
        g.setColor(Color.black);

        int[][] numArray = getBits();

        int row = 0;
        int column = 0;

        for (row = 0; row < 20; row++) {
            for (column = 0; column < 10; column++) {
                //If it's a 0, make it a blue rectangle
                //If it's a 1, make it a black rectangle
                //Else, make it a yellow rectangle (never had this problem yet)
                if (numArray[row][column] == 1) {
                    g.setColor(Color.blue);
                } else if (numArray[row][column] == 0) {
                    g.setColor(Color.black);
                } else {
                    g.setColor(Color.yellow);
                }

                //Draw the rectangle
                g.fillRect(column * 10, row * 10, 10, 10);
            }
        }
    }

//  public static void main(String[] args) {
//      int[][] numArray = getBits();
//
//      //Print out the array (output of this is in the question)
//      for (int row = 0; row < 20; row++) {
//          for (int column = 0; column < 10; column++) {
//              System.out.print(String.valueOf(numArray[row][column]) + " ");
//          }
//          System.out.println("");
//      }
//  }
}

有很多可能性,但是因为您忽略了 Exception,所以很难知道您 运行 反对哪些

你应该知道小程序 运行 在一个非常严密的安全沙箱中,所以你的小程序甚至可能根本无法读取文件

还有我的测试文件...

0000000000
1111111111
1010101010
0101010101
1100110011
0000000000
1111111111
1010101010
0101010101
1100110011
0000000000
1111111111
1010101010
0101010101
1100110011
0000000000
1111111111
1010101010
0101010101
1100110011