...
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
...
Can you describe how the images that are to go in the array are created from the original image?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.I still can't get it to work.
...
When you get errors, please copy and paste the full text here.all my attempts resulted in an error
...
Last edited by pajfilms; August 8th, 2011 at 05:08 PM.
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.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:
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]
Last edited by Norm; August 8th, 2011 at 05:25 PM.
...
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
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];
Last edited by Norm; August 8th, 2011 at 05:42 PM.
...
Last edited by pajfilms; December 25th, 2012 at 10:23 AM. Reason: I wrote "letters" instead of "errors"
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.