...
Printable View
...
Can you describe how the images that are to go in the array are created from the original image?Quote:
to split an image into an array of images
What are the pixel boundaries for each image?
Can you write a loop that changes the x and/or y pixel values for each image that is to be created?
The code sample shows you how to create a buffered image and put it in an array and get a Graphics object for drawing on it.
Then use the drawImage() method to draw that part of the source image into the next image you want to save in the array.
...
Are you saying your problem is moving elements from a two dim array to a one dim array?
Take a piece of paper, draw the 2 dim array as you have above. Mark each element in the two dim array with its row, col position: 0,0 thru 2,5. Then draw the one dim array and mark on it the row, col values for its souce in the 2 dim array. For example a = 0,0 b = 0,1 ... g = 1,0 etc
Looking at those numbers should allow you to copy the contents from the 2 dim to the one dim
The trick could be to use nested loops going over the rows and columns for the input and by having another index for the output array that you manually increment.
...
Make a nested loop for the two dimensions, i for first and j for second.
Have another index for the target array:
.... here the nested for loops
targetArray[idx++] = sourceArray[i][j]; // Copy element and increment the index to one dim
}
}
...
Please post your code for copying the 2 dim array to a 1 dim array.Quote:
I still can't get it to work.
...
When you get errors, please copy and paste the full text here.Quote:
all my attempts resulted in an error
...
have you worked out the x,y positions for the output and compared it to the desired?
a starts at 0,0 and goes to 0
e starts at 1,0 and goes to 1 this should be 4
i starts at 2,0 and goes to 2 this should be 8
b starts at 0,1 and goes to 3 this should be 1
There is a relationship above between the x,y and the z. You need to find the expression that generates the z value (1 dim index) from the x,y 2 dim indexes.
...
No that was wrong. I deleted it just after I posted it.Quote:
I did reverse the indexes
Are you sure the elements are in the order you show? And not like this?
a d g
b e h
c f i
Here is code for 2 dim to 1 dim:
Code :
int[][] twoDim = {{1,2,3},{4,5,6}, {7,8,9}}; // Input 2 dim array int[] oneDim = new int[9]; // Output to 1 dim array int ix = 0; // index to the one dim array for (int i=0; i < twoDim.length; i++) { for (int j=0; j < twoDim[i].length; j++) { oneDim[ix++] = twoDim[i][j]; // copy from 2 dim to 1 dim } } System.out.println("oneDim=" + Arrays.toString(oneDim)); // oneDim=[1, 2, 3, 4, 5, 6, 7, 8, 9]
...
Is it working?
If you take your image and draw a grid on it and label the images with letters, where is the image b relative to image a, on the same row or in the same column?
Here I can get output similar to when you get it in the wrong order
Code :
int[][] twoDim = {{1,2,3},{4,5,6}, {7,8,9}}; int[] oneDim = new int[9]; for (int i=0; i < twoDim.length; i++) { for (int j=0; j < twoDim[i].length; j++) { oneDim[j*3+i] = twoDim[i][j]; // NB: hardcoded 3 here } } System.out.println("oneDim=" + Arrays.toString(oneDim)); // oneDim=[1, 2, 3, 4, 5, 6, 7, 8, 9] using oneDim[i*3+j] = twoDim[i][j]; // oneDim=[1, 4, 7, 2, 5, 8, 3, 6, 9] using oneDim[j*3+i] = twoDim[i][j];
...
Look at the end of my last post. One version of the code ordered the output the same as you show.
By column vs by row.