Move item through an ArrayList
in
Programming Questions
•
1 year ago
Hi,
I have an array list that gets populated when you add objects to the screen. I want to be able to select one object and move it up or down through the array list so that the object is moved to the foreground or background (changing the z-index, more or less). I found an old forum post and modified the code to make something work.
The code below sort of does what I want, but not really. In the if(key=='q') section it moves the selected item to the back and moves the 0 item to the front. I don't want the 0 item moved to the front, I want every following item in the index to move up one.
In the if(key=='w') section it moves the selected item to the front and removes the highest indexed item. Don't want that either.
Thanks in advance.
- void keyPressed () {
- if (key == 't') {
- shapeMove.add(new ShapeMove("tree.gif", 100+btn*50, 100, 150, 150));
- btn++;
- }
- if (key == 'q') {
- int ff = 0; // First false index
- ShapeMove sf = (ShapeMove)shapeMove.get(0); // image w/ false index
- for (int i = 0; i < shapeMove.size(); i++) {
- ShapeMove s = (ShapeMove)shapeMove.get(i);
- if (s.selected) {
- // Replace it with the current photo with false
- shapeMove.set(i, sf);
- // Put the selected image in place of the first false photo (and move this index)
- shapeMove.set(ff++, s);
- // Next photo is the next to swap (might also advance ff up to the next unselected photo)
- sf = (ShapeMove)shapeMove.get(ff);
- }
- }
- }
- if (key == 'w') {
- int ff = shapeMove.size()-1; // First false index
- ShapeMove sf = (ShapeMove)shapeMove.get(0); // photo with false
- for (int i = shapeMove.size()-1; i >= 0; i--) {
- ShapeMove s = (ShapeMove)shapeMove.get(i);
- if (s.selected)
- {
- // Replace it with the current photo with false
- shapeMove.set(i, sf);
- // Put the selected image in place of the first false photo (and move this index)
- shapeMove.set(ff--, s);
- // Next photo is the next to swap (might also advance ff up to the next unselected photo)
- sf = (ShapeMove)shapeMove.get(ff);
- }
- }
- }
1