https://processing.org/examples/constrain.html
I will not be posting any code since this appears to be an academic assignment.
You can do this.
Hint:
The rect() may be better off in the setup() since it will keep drawing over your sketch canvas if it is in draw().
