I put together some code that's supposed to do what you desire. Here's the java file: