At a high level it sounds like you’re looking for collision detection. Shameless self-promotion: here is a guide on collision detection:
It’s written for Processing, but all of the basic ideas still apply.
I’d also recommend that you get something simpler working first. Break your problem down into smaller steps and then take those steps on one at a time. Can you create a simpler example program that shows two hard-coded rectangles? Get that working before you try implementing it in your bigger project. Then if you get stuck, you can ask a more specific question. Good luck!