【问题标题】:main() prints out values of multidimensional array differently from paint()main() 打印出与paint() 不同的多维数组的值
【发布时间】:2016-01-24 23:48:28
【问题描述】:

我正在尝试使用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() 不同?我该如何解决这个问题?

【问题讨论】:

  • 小程序不运行主方法。如果我是你,我会删除该方法,而是专注于 init() 方法。在init() 中读取位一次,而不是在paint 中。此外,您不应该覆盖paint方法,也不应该直接在applet中绘制,而应该覆盖显示在applet中的JPanel的paintComponent方法。
  • 你是什么意思他们不运行主要方法?我只是使用 main() 进行调试,实际上并没有影响小程序。
  • 另外,为什么你的 catch 块是空的——那是在要求灾难。另外,您不应该阅读文件,而是阅读资源......这里有太多问题......
  • 我的猜测是您在从小程序上下文读取文件时遇到问题,但您忽略了任何可能生成的异常。尝试在catch 子句中添加e.printStackTrace(),看看是否有任何内容打印到控制台。否则,您的代码会执行您似乎希望它执行的操作
  • @Bob main 与您的 applet 在不同的上下文中运行,后者在更严格的安全沙箱中运行。当我创建自己的bitmap.bmp 版本时,您的绘制代码运行良好

标签: java arrays multidimensional-array paint main


【解决方案1】:

一旦我提供了我自己的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

【讨论】:

  • 你从哪里得到这个小程序查看器?我刚刚使用了一个 html 文件并将我的小程序插入其中。
  • appletview.exe 在 JDK/bin 目录中。 Netbeans 将默认执行它,但也会自动生成 HTML
  • 很抱歉打扰您,但这可能是我的问题的解决方案。我可以在命令提示符下使用 appletviewer.exe 运行我的程序吗?如果有,怎么做?
  • 从命令行尝试appletviewer -? 以获得选项列表
  • 非常感谢@MadProgrammer!它一直是浏览器。显然浏览器不再支持小程序?所以当我使用 IE 运行我的程序时,它不会做我想要它做的事情。很抱歉因为这个简单的事情打扰您,但我非常感谢您提供快速、快速和有用的信息。
猜你喜欢
  • 2020-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多