We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi!
I have created this sketch where the mouse rolls over four squares. The text tells whenever you are over a square. But what if I want to tell when the mouse is over no squares at all? When the mouse is outside.
When the mouse is outside all the squares the ”overImg” boolean goes false. I need something that says something like:
when all the objects overImg is false - > print OUTSIDE SQUARES
Heres the sketch:
int cols = 2; // number of columns
int rows = 2; // number of rows
int sq = 110;
float avst = 10;
int translateValue = 60;
int no;
int square;
// 1) deklarera
Screen[][] scr;
void setup() {
size(400, 400);
float dist = sq+avst;
float offsetW = (width - (cols * (sq+avst)))/2-(avst/2)+(sq/2);
float offsetH = (height - (rows * (sq+avst)))/2-(avst/2)+(sq/2);
scr = new Screen[cols][rows];
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
no++;
scr[i][j] = new Screen(i*dist+avst+offsetW, j*dist+avst+offsetH, sq, sq, color(random(255), 0, 0), no);
}
}
}
void draw() {
background(255);
for (int i = 0; i < scr.length; i++) {
for (int j = 0; j < scr[i].length; j++) {
scr[i][j].display();
scr[i][j].mouseGet(mouseX, mouseY);
}
}
fill(0);
textAlign(CENTER);
text("OVER SQUARE NUMBER:\n"+ square, width/2, 50);
}
class Screen {
PVector loc;
float w;
float h;
float xLeft, xRight, yUp, yDown;
boolean overImg = false;
color col;
int sqNo;
// x, y, bredd, höjd
Screen(float x_, float y_, float w_, float h_, color col_, int sqNo_) {
loc = new PVector(x_, y_);
w = w_;
h = h_;
xLeft = loc.x-(sq/2);
xRight = loc.x+(sq/2);
yUp = loc.y-(sq/2);
yDown = loc.y+(sq/2);
col = col_;
sqNo = sqNo_;
}
void display() {
fill(0);
rectMode(CENTER);
pushMatrix();
translate(loc.x, loc.y);
fill(col);
rect(0, 0, w, h);
fill(255);
text("SQUARE: " + sqNo, 0, 0);
popMatrix();
println(overImg);
println(sqNo);
}
void mouseGet(int mx, int my) {
if ((mx > (xLeft)) && (mx < (xRight) &&
(my > (yUp) && (my < (yDown))))) {
overImg = true;
square = sqNo;
} else {
overImg = false;
}
}
}
Answers
You are doing a lot of work trying to do this with classes. Would adapting something like https://processing.org/examples/mousefunctions.html be easier?
Well.my sketch in the thread is only an example. I got a bigger sketch with a lot of other things so I need to do it with classes. Any solution of sending value from objects back to main sketch
Something like this?
Thanks a lot!! I didnt knew you could call also variables in a class by looping through them in draw with for example:
scr[i][j].overImg
.
operator: https://Processing.org/reference/dot.htmlI didnt really got it. Is it considered bad programming practise doing as my example above? How would you solve it then?
Or did I misunderstand you?
Thanks!
But actually in pure Java you don't usually access the variable via dot synthax, you need to declare these private and provide get() and set() methods to change them. Am I correct, @GoToLoop ?
IMO, for sketches or any simple programs, it's a waste of time and growth of cluttering creating getters & setters for every single field! :-@
Remember that Processing's API is multi-programming language.
For example, JavaScript & Python don't have any access keywords yet.
And that fact doesn't impede them to be used to build big applications everywhere! \m/
Besides, given that Processing's PDE make all our classes nested in relation to the PApplet sketch's top subclass, access keywords don't affect them! >:)