That's strange! Detect mouse inside a box?

I remember that I did that in a Periodic Table program before, my Dad accidentally deleted the D: drive in my computer and everything inside got lost.

This is my code:

boolean mouseIsBetween(float x1,float y1,float x2,float y2){
  if(mouseX >= x1 && mouseX <= x2 && mouseY >= y1 && mouseY <= y2){
    return true;
  }else{
    return false;
  }
}

It must detect if the mouse is between x1,y2 to x2,y2, but it do not return true.

Sorry if there is any grammar mistake.

1 Like

the function looks ok

maybe you call the function not correctly

remember that the values you have to pass to the function are NOT the same as with rect(), because with

  • mouseIsBetween you want absolute positions x,y and x,y
  • with rect you have x,y and the sizeX,sizeY
2 Likes

working example with your function


boolean boolEnteringText=true; // entering text yes/no
String export=""; // current text input
String result = ""; // list of text on the right  
boolean showBlinkingLine=true;  // show blinking cursor yes/no

void setup() {
  size(800, 800);
  background(0);
}

void draw() {
  background(0);
  text(export + blinkingLine(), 22, 22);
  text(result, width-112, 22);

  noFill();
  stroke(220); 
  rect(210, 4, 61, 27);
  fill(255);
  text("submit", 222, 22);
}

//---------------------------------------------------------------------------


void keyPressed() {
  if (boolEnteringText == true && key==BACKSPACE) {
    // delete last char
    if (export.length()>0) {
      export=export.substring(0, export.length()-1);
    }
  } else if  (boolEnteringText == true && (key==RETURN||key==ENTER)) {
    // submit
    submit();
  }//else if
  else if (boolEnteringText == true && key != CODED) {
    // add char 
    export += key;
  }
}

void mousePressed() {
  if (mouseIsBetween(210, 4, 280, 4+27)) {
    submit();
  }
}

boolean mouseIsBetween(float x1, float y1, float x2, float y2) {
  if (mouseX >= x1 && mouseX <= x2 && mouseY >= y1 && mouseY <= y2) {
    return true;
  } else {
    return false;
  }
}

String blinkingLine() {
  // toggle showBlinkingLine
  if (frameCount%17==0) 
    showBlinkingLine = ! showBlinkingLine;

  // return line or nothing     
  if (showBlinkingLine) 
    return "|";
  else return "";
}

void submit() {
  // submit
  result+=export+"\n";
  export="";
  showBlinkingLine=true;
}
//
2 Likes

Studio.ProcessingTogether.com/sp/pad/export/ro.9eDRvB4LRmLrr
Studio.ProcessingTogether.com/sp/pad/export/ro.9vAkI4JDuLs8s
2 Likes

Tested everything, don’t work… :confused:

This is my code:
https://cutt.ly/cfpBITG

Can you guys help me change it?

Thanks for the helps…

Hi @HumanoidX,

I took your example and applied a simple case when you press the bin image. You can reply the code to the other images.
I noticed that the FunBlackboard.pde was missing the draw() function.
Your code for the mouseIsBetween() method is correct assuming that you pass the correct coordinates.

I would recommend you to also look at the following example for better understanding :slight_smile: (https://processing.org/examples/button.html)

import processing.sound.*;

float oldx;
float oldy;
float newx;
float newy;
boolean drewCurve = false;

PImage bin;
PImage style;
PImage rgb;
PImage a;

boolean binClicked = false;

void setup(){
  size(900,440);
  background(0);
  
  bin = loadImage("bin.png");
  rgb = loadImage("rgb.png");
  a = loadImage("a.png");
  style = loadImage("style.png");
  //drawPalette();
}

void draw(){
  background(0); 
  drawPalette();
//If you have clicked the bin a text message appears. If you click again the text message will dissapear
  if(binClicked){
    fill(255);
    text("clicked the bin",width/2, height/2);
  }

}

void mousePressed(){ 
//The bin is located between corrdinates (0,0) and (40,40)
  if(mouseX >= 0 && mouseX <= 40 && mouseY >= 0 && mouseY <= 40){
    binClicked = !binClicked; //alter the boolean state
  }
}

Hope it helps!

3 Likes

Thanks Miguel, It worked. :smiling_face_with_three_hearts: Looks like I can’t use the return function to output data. But it worked so I will not care about it. :stuck_out_tongue:

Hi @HumanoidX

Glad it worked
But you can use the mouseIsBetween function still.
Replace

void mousePressed(){ 
//The bin is located between corrdinates (0,0) and (40,40)
  if(mouseX >= 0 && mouseX <= 40 && mouseY >= 0 && mouseY <= 40){
    binClicked = !binClicked; //alter the boolean state
  }
}

by

void mousePressed(){ 
//The bin is located between corrdinates (0,0) and (40,40)
 if(mouseIsBetween(0,0,40,40)){
    binClicked = !binClicked; //alter the boolean state
  }
}
2 Likes

Guess what, don’t work anymore when I switch to the boolean…return code.

I have to use the very very loooooong code after all. :exploding_head:

When using your code and substituting for what I have posted it worked for me.

Make sure that you are calling the functions in the proper place. The mousePressed() method is an event that detects when the mouse is being pressed. Only there I check if the bin was clicked.

1 Like

Never mind, it somehow worked again. :confused:
But it worked anyway… :smile:

1 Like