Send value from all object

PerPer
edited January 2016 in Questions about Code

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;
    }
  }
}
Tagged:

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?

  • PerPer
    edited January 2016

    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

  • edited January 2016 Answer ✓

    Something like this?

     void checkScreenOverImg() {
      int countTrue = 0;
      for (int i = 0; i < cols; i++) {
       for (int j = 0; j < rows; j++) {
        if (scr[i][j].overImg) countTrue++;
       }
      }
      if (countTrue == 0) println("OUTSIDE SQUARES");
     }
    
  • PerPer
    edited January 2016

    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

    int cols = 2; 
    int rows = 2;
    int sq = 110; 
    float avst = 10; 
    int translateValue = 60;
    int no; 
    int square;
    
    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);
      boolean countTrue = true;
      for (int i = 0; i < cols; i++) {
        for (int j = 0; j < rows; j++) {
          scr[i][j].display();
          scr[i][j].mouseGet(mouseX, mouseY);
          if (scr[i][j].overImg) {
            countTrue = false;
            println(scr[i][j].sqNo + " is " + scr[i][j].overImg);
          }
        }
      }
    
      fill(0);
      textAlign(CENTER);
      if (countTrue == false) {
        text("OVER SQUARE NUMBER:\n"+ square, width/2, 50);
      } else {
        text("OVER SQUARE NUMBER:\n"+ "OUTSIDE SQUARES", width/2, 50);
      }
    }
    
    
    class Screen {
      PVector loc;
      float w;
      float h;
      float xLeft, xRight, yUp, yDown;
      boolean overImg = false;
      color col;
      int sqNo;
    
      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();
      }
    
      void mouseGet(int mx, int my) {
        if ((mx > (xLeft)) && (mx < (xRight) && 
          (my > (yUp) && (my < (yDown))))) {
          overImg = true;
          square = sqNo;
        } else {
          overImg = false;
        }
      }
    }
    
  • edited January 2016

    I didn't knew you could call also variables in a class by looping through them.

    • Actually we can't! Variable scr[][] belongs to the "sketch", not to class Screen.
    • Since scr[][] refers to a 2D array of Screen objects, we can access all of its members via the dot . operator: https://Processing.org/reference/dot.html
    • Therefore, it's an indirect access. :-\"
    • Although an instance of Screen can directly access "sketch"'s scr[][] field, given the former is an inner class of the latter, it's considered bad programming practice to do so. :-@
  • I 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!

  • edited January 2016
    • I'm suspicious you misunderstood me. You haven't committed the "bad" practice in your program.
    • That is, you aren't accessing variable sketch's scr[][] inside your Screen class.
    • That was just an addendum warning... 8-|
    • Rather that's happening inside sketch's draw() method. Which is very right! O:-)
  • 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 ?

  • edited January 2016

    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! >:)

  • edited January 2016
    // forum.Processing.org/two/discussion/14449/send-value-from-all-object
    // GoToLoop (2016-Jan-14)
    
    private final InnerClass innerObj = new InnerClass();
    private int sketchField = 10;
    
    void setup() {
      println(innerObj.innerField);    // private field access.
      println(innerObj.sumFields());   // private method access.
      println(innerObj.checkEquals()); // protected method access.
      exit();
    }
    
    private class InnerClass {
      private int innerField = 20;
    
      private int sumFields() {
        // Directly accessing the foreign sketchField is bad practice!
        // It is done merely to illustrate that private keyword is moot here:
        return innerField + sketchField;
      }
    
      protected boolean checkEquals() {
        // Again bad practice to directly access foreign innerObj field here:
        return this == innerObj;
      }
    }
    
Sign In or Register to comment.