How to use this code with mousePressed?

Just put all that code into the mousePressed() method from draw()!