Rectangle button

hi,

as you say you re a beginner, i have to remind there are libraries to do this in a quite easy way

however, you re close to a functionning code , it may be better for learning to finish this

the test you do in mousePressed() are a little bit mixed:

dist() will calculate distance between mouse and the xy position you give to it

so if mouse is close to a button position value will be low, and high if far away
(you can add println(d1); to check what values it give)

why do you compare that ‘d’ value with a position?

the closest button is button1 if d1 is the smallest value you get no?

if it s still too complicate, i recomand to process only first button, then manage others
you can have a look on how it s done in this processing example

well this example is a little bit complex:

but you can look how it test if the mouse is over the rectangle:

  // Test if the cursor is over the box 
  if (mouseX > bx-boxSize && mouseX < bx+boxSize && 
      mouseY > by-boxSize && mouseY < by+boxSize) {
3 Likes