Hello,
A Processing example:
I have done this a few different ways in my projects.
There may be something useful in there for you.
:)