I've made this program to test it:
You can compile it and run it. With left and right you change columns and up and down you change rows. With 1 you move the active row 1 step left, with 2 one step right. With 3 you move active column one step up, with 4 one step down.
As you see when moving the elements left or up the first and last element becomes the same. If you move right or down all elements become the same.
I need to do this to make an animation with an image.
When moving up i think the problem could be on this line:
Since its the last element that is wrong. It's like tempArray[index1] would have the same value as array[index1] which gets changed right in start. I think the problems with moving down or right would be fixed if I knew what was wrong here.
Replies appriciated, been many hours about this.
[ June 15, 2006: Message edited by: Jon Mattson ]
I don't understand what it is supposed to do, so can't tell what is going wrong.
BTW: Use System.out.println(); instead of System.out.println(""); and your many if statements might be better off as a switch.
Is this intentional?
I am getting this sort of printout:-
I presume that is what you wanted.
Active row: 0
Active column: 0
If you press 1 while 0 is the active row this is what i want to happen:
And this is what happens:
If I press 2 when 0 is the active row, this is what I want to happen:
And this is what happens
I hope this makes it a bit clearer
[ June 15, 2006: Message edited by: Jon Mattson ]
Are you using J2SE5.0? If so you can get rid of the bits about length of String by using the printf method, but it won't work in J2SE1.4.
You would also do well to have a look at the more complicated if statements, like this one:-Try to get them into a simpler format, likeGo through the entire app like that and simplify things. Then try it again. Get rid of the dead wood, then you will be able to see what is going on and get the "for" loops sorted out.
[ June 15, 2006: Message edited by: Campbell Ritchie ]
you really need to change the elements - could you just access the elements in a certain order?
something like this - it just changes to order/display of the columns
I still don't understand quite what the idea of the whole thing is, but it seems to be altering the order of the printouts from the arrays.
There is no need to parse numbers; the KeyEvent.VK_1 sort of event is expressed as an int, so it can be used directly for a switch statement.
There are much easier ways to get a rotating index for an array than the multiple if statements. In fact there is a very simple way to enter an offset value and get 0 1 2 3 4, or 1 2 3 4 0 or 2 3 4 0 1, etc from a for loop.
It is something really basic and simple, first-week stuff, which I haven't told you.
But I really wonder why my way doesn't work.
-First I make a copy of the original array:
-Then I pick values from the temp array to the original array. If i want a column to move one step up I do this:
That code is assigning the values of tempArray[i+1]["THE ACTIVE COLUMN"] to array[i]["THE ACTIVE COLUMN"]. That should move all elements in the column (except the one on the top) one step up and i think it works too. But the problem seems to be on the line
which is excecuted when (i+1 < rows) is not true which means it excecutes when the it's the last iteration of the loop. This line is supposed to take the element on the top of the column and place it on the bottom. It takes the value tempArray["THE ACTIVE COLUMN"] (which is the element on the top of the column from the original order) and places it in array[rows-1]["THE ACTIVE COLUMN"] (which is according to my logic the last element in the column in the array with the new order). What i think this line does in reality is that it takes the value from the top of the column in the new order and places it in the last element in the column. But I don't get why it does that.
For me it would be entirely logical for this to work but it doesn't. Could someone explain why?
I hope you understand what I've written since english ain't my native language.
I'll still try to explain for what i need this.
I want to animate an image(a 16x16 tile for my game) of a waterfall. First I load the image to the program. Then I read it's pixeldata and place it in a 1D array. That 1D array I then convert to a 3D array which contains the alpha, red, green, blue values for each pixel. When the image is in this form I want to move the pixels from ONE column one pixel down. Then I change it back to 1D array and to a picture. I repeat this with other columns and it looks like water is falling. I want to have a method which can move the rows or columns in any direction (up, down, left, right) so I can animate other things too. If there's a faster way to move the pixels please let me know.
[ June 18, 2006: Message edited by: Jon Mattson ]
And as for what you do to get a counter, you ought to have been taught that in your first few weeks of Java programming. You set up an offset, whenever you push the -> key you increase the offset by 1, and whenever you push the <- key you reduce the offset by 1. But make sure your offset never goes less than 0.
To go through the array indices to get 0 1 2 3 4 is easy. To get 2 3 4 0 1 is easy too, providing you remember which of the five basic arithmetic operators you use.
I will give you a hint: +-* and / won't work.
I'll give you another hint. Compile and run an app like this:-
That will give you a square looking like this:-
00 01 02 03 04 Offset: 0
11 12 13 14 10 Offset: 1
22 23 24 20 21 Offset: 2
33 34 30 31 32 Offset: 3
44 40 41 42 43 Offset: 4
Try altering the app for different sizes of square. Try altering it to move the rows down. Then alter it until you get the sort of offsets you want for your game array.