【发布时间】: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