You need a queue. Which is a class that allows you to shove things into an array at the end, and pop off and remove things from the front. Thus giving you First in First out system. I code in C++ , but Java is similiar enough you can read the code and make the proper syntax corretions. Note: I would symbolically change your graph to a NxN array of x,y points. So the above graph would go from 0,0 to 2,2. It would also be easier if you mirror imaged the number along the diagonal. For example
1----2----3
| ||
4----5----6
| | |
7----8----9
For any size N. This arrangement is the more normal way of reading arrays in code. But if your original way of drawing is necessary flip the x,y in the equation above.. us y co-ords to go left/right and x to go up/down in the code. The code might be a little longish for the forums. So if you want just PM and i could email you some code I have already.