They have certainly chosen a hard way to make a sokoban game by using sprites to draw the grid instead of just laying it all out in an array. It's also awful how they setup LEFT_COLLISION, RIGHT_COLLISION, etc. as constants representing all four directions but then fail to use them to simplify the code in any way. You should use those sorts of constants so that you can pass one as an argument to a method and allow that method to work for any direction and thereby requiring only a quarter as much code, but in this game every method that takes a direction constant immediately goes into an if-else chain to figure out which constant it was given, completely ruining the benefits of using the constant.

On top of that they are giving every wall its own individual ImageIcon even though they are all going to look the same. I hope that javax.swing.ImageIcon internally arranges to share images so that it's not creating dozens of copies of the wall image, but the ImageIcon javadoc makes no promises about that.

I would never trust a tutorial written like that to teach me anything. Don't consider it an example of the best way to make a sokoban game.

My first step in making any change to that game would be to completely rewrite it, but considering your problem involves only images I can limit myself to the images.

Start by adding an Image to the constructor parameters of each of your four kinds of Actor. At the beginning of initWorld you should create all the images you will need and pass the correct image to each actor you construct as you construct them. For what you want, you should pass two images to each Baggage you create, one for being in the right place and one for being out of it.

You can add a method to Baggage something like public void setInRightPlace(boolean inRightPlace) and if it is called with true it sets one image as the current image, and if it is called with false it sets the other one. And then you can modify checkBagCollision so that every time a Baggage moves you check to see if it is in the right place and call the setInRightPlace method.

This article has been dead for over six months. Start a new discussion instead.