main()以不同于paint()的方式打印出多维数组的值

时间:2015-10-25 23:22:32

标签: java arrays multidimensional-array paint main

我正在尝试使用paint()在屏幕上绘制矩形。如果我的阵列中某个位置有1,那么在屏幕上它将是一个蓝色矩形。如果我的阵列中某个位置有0,那么在屏幕上它将是一个黑色矩形。我通过访问.bmp文件并读取行来创建此数组。然后将这些行(实际上是字符串)转换为带有.toCharArray()的字符数组,这些数组将转换为整数数组。所以这个最终的数组用1和0整数填充。然后我进入paint(),调用函数getBits()创建数组,并将其存储在numArray中,这是一个2D数组。出于调试目的,我在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.看起来有些错误但是我知道数组包含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放在全局范围内并自己初始化每一个位置。问题是我不想为我的程序这样做,因为我想使用任何位图。

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

[expected output]

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

1 个答案:

答案 0 :(得分:1)

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

Display

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,很难知道你遇到了哪些

您应该知道applet在非常严格的安全沙箱中运行,因此您的applet甚至可能根本无法读取该文件

我的测试文件......

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