We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi, i've searched for a while and I'm not sure if i'm missing something obvious, i'm trying to print a 2d integer array as text, i did it before but I can't remember how i did and it is driving me insane.
thanks.
void setup() {
size(300, 300);
background(255);
array();
}
void array() {
int[][] arrayinit = {
{ 10, 5, 3, 55, 90 },
{ 9, 4, 2, 32, 22 },
{ 3, 8, 5, 79, 22 },
{ 3, 8, 45, 4, 2 },
{ 3, 22, 5, 44, 4 }
};
int rows = 5;
int cols = 5;
int[] sum = new int[cols];
int[] sum2 = new int[rows];
stroke(0);
for (int c=0; c<cols; c++) { // for every column
for (int r=0; r<rows; r++) { // for every row
rect(40*r, 30*c, 40, 30);
text(arrayinit[c][r], 40*r, 30*c); //dont know if this is right
sum[c] += arrayinit[r][c]; // sum up the values per column
sum2[r] += arrayinit[r][c];
}
}
println(sum[0]);
println(sum[1]);
println(sum[2]);
println(sum2[2]);
println(arrayinit[2][2]);
}
Answers
From https://Forum.Processing.org/two/discussion/23471/combining-two-pieces-of-code/p2#Item_51
I've made this utility function to join a 2D String[][] array as 1 String, ready for text(): :ar!
You are drawing both the rectangle and text. But both has white color as their fill. Even the background. So it's pretty much invisible. You could simply change your text color to solve the problem.
It will look like this:
Best regards :D
Thanks, as I thought it was pretty obvious.
Another alternate approach is to use a fixed-width font and print a single line-wrapped string. This will create tabular text on the screen quickly and easily using the built-in text system. However, the method is inflexible (i.e. space padding is required), and it is very sensitive to character width and line height.
Belatedly: Another option is a simple trick that uses Java
Arrays.deepToString()
and then reformats the output with simple search-replace rules.Output:
Related: