java - Swapping elements in an ArrayList -
i'm trying make slider puzzle kind of game click 1 image, click , swap positions. reason works correctly first time it, when go swap image second time , every time after selects different element 1 clicked on. appreachated, thank you.
public class test extends application { int click1 = -1, click2 = -1; public static void main(string[] args) { application.launch(args); } @override public void start(stage primarystage) throws exception { //create gridpane gridpane pane = new gridpane(); pane.setalignment(pos.center); pane.sethgap(5); pane.setvgap(5); //create arraylist , add imagelist arraylist arraylist<imageview>imagelist = new arraylist<imageview>(); (int = 0; < 9; i++) { imagelist.add(new imageview ((i) +".jpg")); } addimages(imagelist, pane); //add onclick listeners each image imagelist.get(0).setonmouseclicked(e->{ swap(0, imagelist, pane); }); imagelist.get(1).setonmouseclicked(e->{ swap(1, imagelist, pane); }); imagelist.get(2).setonmouseclicked(e->{ swap(2, imagelist, pane); }); imagelist.get(3).setonmouseclicked(e->{ swap(3, imagelist, pane); }); imagelist.get(4).setonmouseclicked(e->{ swap(4, imagelist, pane); }); imagelist.get(5).setonmouseclicked(e->{ swap(5, imagelist, pane); }); imagelist.get(6).setonmouseclicked(e->{ swap(6, imagelist, pane); }); imagelist.get(7).setonmouseclicked(e->{ swap(7, imagelist, pane); }); imagelist.get(8).setonmouseclicked(e->{ swap(8, imagelist, pane); }); //display scene scene scene = new scene(pane, 650, 650); primarystage.setscene(scene); primarystage.settitle("test"); primarystage.show(); } private void swap(int lastclick, arraylist<imageview> imagelist, gridpane pane) { if (click1 == -1) { click1 = lastclick; system.out.println(imagelist.get(click1).getimage().impl_geturl()+ " "); imagelist.get(click1).setscalex(1.02); imagelist.get(click1).setscaley(1.02); } else { click2 = lastclick; system.out.println(imagelist.get(click2).getimage().impl_geturl()+ " "); //swap indexes in arraylist collections.swap(imagelist, click2, click1); pane.getchildren().removeall(imagelist); addimages(imagelist, pane); //reset next swap imagelist.get(click1).setscalex(1.0); imagelist.get(click1).setscaley(1.0); imagelist.get(click2).setscalex(1.0); imagelist.get(click2).setscaley(1.0); click1 = -1; click2 = -1; } } private void addimages(arraylist<imageview> imagelist, gridpane pane) { //add imagelist gridpane int = 0; while (i < 9) { (int j = 0; j <= 2; j++) { (int k = 0; k <= 2; k++) { pane.add(imagelist.get(i), k, j); i++; } } } }
}
you need update mouseclick listener once update new element. ends happening stale state. item has moved reports initial position rather actual position, resulting in items moving around shouldn't be.
basic state.
0, 1, 2, 3, 4, 5, 6, 7, 8
assume click 3 , 5. request swap image 5 image 3's slot, , image 3 image 5's slot.
0, 1, 2, 5, 4, 3, 6, 7, 8
this looks right now, let's try swap. let's swap image 1 5.
0, 3, 2, 5, 4, 1, 6, 7, 8
what happened?! 1 , 5 clicked, 3 , 1 swapped places instead!
the move on back-end worked worked designed. image 5 , image 1 clicked, resulting in operation "swap slot 5 slot 1, instead of intended slot 3 slot 1. issue image in slot 5 image 3.
the rectification update mouse click listeners know afterwards, can report new, updated positions.
fix be:
imagelist.get(click1).setonmouseclicked(e->{ swap(click1, imagelist, pane); }); imagelist.get(click2).setonmouseclicked(e->{ swap(click2, imagelist, pane); });
this tells swapped items "this moved, , on should report new moved position."
Comments
Post a Comment