Reputation: 177
I'm trying to use paint() to draw rectangles to the screen. If there is a 1 in my array at a location, then on the screen it will be a blue rectangle. If there is a 0 in my array at a location, then on the screen it will be a black rectangle. I create this array by accessing a .bmp file and reading the lines. These lines (essentially Strings) are then turned into character arrays with .toCharArray() which are turned into integer arrays. So this finalized array is filled with 1 and 0 integers. I then go into paint(), call the function getBits() which creates the array, and store it in numArray, which is a 2D array. For debugging purposes, I called getBits() in main() and printed out the array, which came out to:
1000000000
1101000000
1111000000
0000000000
1001000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
This is the correct output of the array if it were in an x-y coordinate system. ^
However, when I call getBits() in paint() and store it in numArray then proceed to execute my conditionals to check whether it's a 1 or a 0, it always chooses 0. It seems like there's an error of some sorts and everything is somehow changed to 0. But I know that the array contains 1s because of the debugging in main() which printed out the 1s and 0s in the above sample output.
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("");
}
}
The weird thing is that I can fix this IF I put numArray in global scope and initialize every single position myself. The problem is that I don't want to do that for my program because I want to use any bitmap.
This is what my output should look like and what it actually looks like:
[
So my question is... why does my main() function see numArray differently than paint() does? And how can I fix this?
Upvotes: 0
Views: 70
Reputation: 347244
Once I provide my own bitmap.bmp
file, the code seems to print just fine
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("");
// }
// }
}
There are a bunch of possibilities, but because you're ignoring the Exception
, it's hard to know which ones you've run up against
You should know that applets run in a very tight security sandbox, so your applet may not even be able to read the file at all
And my test file...
0000000000
1111111111
1010101010
0101010101
1100110011
0000000000
1111111111
1010101010
0101010101
1100110011
0000000000
1111111111
1010101010
0101010101
1100110011
0000000000
1111111111
1010101010
0101010101
1100110011
Upvotes: 1