Is there a way to share information (like ints) between classes?

I'm writing a game/simulation in where you're going to diagnose a patient and the patients disease is a new and random one each time so you have to push different buttons and get different clues and stuff. I want a class with information about the buttons and one class for each disease. I'll share everything I've got so far, so you can try to run the code and try it out. Ok, to the question: In the "button"-class I have a button called "blodprøve" that displays a list of things you can test for and if you click for example "CRP", you mark that and you draw blood, testing for the CRP-level and then when you hit OK, you get the CRP-levels. And in pseudocode I have written (this is a simplification):

void mouse(){ //this is run in "void mouseClicked" outside of the button-class
If(CRP is clicked){
CRP = 1;
}
if(OK is clicked){
OK == 1;
}
}
void display(){
if(CRP == 1){
mark CRP with a checkmark so the user knows it has been checked off;
}
if(OK == 1){
make the text and checkmarks disappear;
}
}

And I want to write in the console/diabetes-class: (btw, the console class is not a disease, it's the levels of a normal person)

If(CRP ==1){
if(OK ==1{
add the CRP-levels specified in this class to the console-string so they can be displayed;
}
}

But I am afraid I have to do it like this:

void display(){
if (CRP==!){
display checkmark;
diabetes.crp
console.crp
}
}
//in diabetes-class:
void crp(){
CRP = 1;
}

void display(){
if(CRP==1){
if(OK ==1){
add CRP-leves
}

The problem here is that I have to introduce each int in every class and I have to create a separate void for each int in every class, and all of a sudden I'm knees deep in work that seemed very simple to start with....

Ok, I hope you guys understood all of that, now here's the code:

Buttons buttons;
Console console;
Diabetes diabetes;
float S = random(1, 2);
int Sykdom = int(S);

void setup() {
  size(450, 400);
  diabetes = new Diabetes();
  buttons = new Buttons();
  console = new Console();
}

void draw() {
  background(0);
  switch(Sykdom) {
  case 1: 
    console.print();
    break;
  case 2:
    diabetes.print();
    break;
  }
  buttons.display();
  buttons.update();
}

void mouseClicked() {
  switch(Sykdom) {
  case 1: 
    console.mouse();
    console.scroll();
    break;
  case 2:
    diabetes.mouse();
    diabetes.scroll();
    break;
  }
  buttons.blodprove();
}


class Buttons {
  String blodprave ="";
  String ok = "";
  int bp = 0;
  int CRP = 0;
  int HB = 0;
  int MCV = 0;
  int JERN = 0;
  int FERRITIN = 0;
  int CK = 0;
  int TROPONINT = 0;
  int LPK = 0;
  int INR = 0;
  int HDL = 0;
  int LDL = 0;
  int OK = 0;

  void display() {
    fill(200);
    rectMode(CORNER);
    rect(0, 200, width, height);
    fill(233);
    rectMode(CENTER);
    rect(424, 250, 50, 100);
    rect(424, 350, 50, 99);

    rect(300, 270, 199, 60);
    rect(100, 330, 200, 60);
    rect(300, 330, 199, 60);
  }

  void update() {
    fill(128, 191, 255);
    if (mousePressed) {
      if (mouseX<40&&mouseX>0&&mouseY<240&&mouseY>200) {
        fill(98, 169, 255);
      } else {
        fill(128, 191, 255);
      }
    }
    rect(20, 220, 40, 40);
    fill(0);
    text("BT", 15, 225);
    fill(128, 191, 255);

    if (mousePressed) {
      if (mouseX<80&&mouseX>40&&mouseY<240&&mouseY>200) {
        fill(98, 169, 255);
      } else {
        fill(128, 191, 255);
      }
    }
    rect(60, 220, 40, 40);
    fill(0);
    text("BG", 55, 225);
    fill(128, 191, 255);

    if (mousePressed) {
      if (mouseX<120&&mouseX>80&&mouseY<240&&mouseY>200) {
        fill(98, 169, 255);
      } else {
        fill(128, 191, 255);
      }
    }
    rect(100, 220, 40, 40);
    fill(0);
    stroke(100);
    text("Puls", 90, 225);
    fill(128, 191, 255);

    if (mousePressed) {
      if (mouseX<160&&mouseX>120&&mouseY<240&&mouseY>200) {
        fill(98, 169, 255);
      } else {
        fill(128, 191, 255);
      }
    }
    rect(140, 220, 40, 40);
    fill(0);
    stroke(100);
    text("Resp", 128, 225);
    fill(128, 191, 255);

    if (mousePressed) {
      if (mouseX<200&&mouseX>160&&mouseY<240&&mouseY>200) {
        fill(98, 169, 255);
      } else {
        fill(128, 191, 255);
      }
    }
    rect(180, 220, 40, 40);
    fill(0);
    stroke(100);
    text("Temp", 165, 225);
    fill(128, 191, 255);


    if (mousePressed) {
      if (mouseX<240&&mouseX>200&&mouseY<240&&mouseY>200) {
        fill(98, 169, 255);
      } else {
        fill(128, 191, 255);
      }
    }
    rect(220, 220, 40, 40);
    fill(0);
    stroke(100);
    text("CRP", 210, 225);
    fill(128, 191, 255);

    fill(233);
    if (mousePressed) {
      if (mouseX<300&&mouseX>0&&mouseY<305&&mouseY>242) {
        fill(200);
      } else {
        fill(233);
      }
    }
    rect(100, 270, 200, 60);
    fill(0);
    stroke(100);
    text("Blodprøve", 50, 270);
    fill(128, 191, 255);


    fill(200);
    if (mousePressed) {
      if (mouseX>400&&mouseX<width&&mouseY>187&&mouseY<210) {
        fill(150);
      } else {
        fill(200);
      }
    }
    rect(424, 195, 50, 20);


    fill(200);
    if (mousePressed) {
      if (mouseX>400&&mouseX<width&&mouseY>167&&mouseY<187) {
        fill(150);
      } else {
        fill(200);
      }
    }
    rect(424, 175, 50, 20);

    fill(200, 200, 255);
    text(blodprave,330,20);
    fill(100, 255, 100);
    text(ok, 340, 183);

    stroke(200, 240, 200);
    if (CRP==1) {
      line(320, 12, 325, 18);
      line(325, 18, 330, 8);
    }
    if (HB==1) {
      line(320, 27, 325, 33);
      line(325, 33, 330, 23);
    }
    if (MCV==1) {
      line(320, 40, 325, 46);
      line(325, 46, 330, 36);
    }
    if (JERN==1) {
      line(320, 54, 325, 60);
      line(325, 60, 330, 50);
    }
    if (FERRITIN==1) {
      line(320, 68, 325, 73);
      line(325, 73, 330, 63);
    }
    if (CK==1) {
      line(320, 83, 325, 88);
      line(325, 88, 330, 78);
    }
    if (TROPONINT==1) {
      line(320, 96, 325, 102);
      line(325, 102, 330, 92);
    }
    if (LPK==1) {
      line(320, 112, 325, 118);
      line(325, 118, 330, 108);
    }
    if (INR==1) {
      line(320, 126, 325, 132);
      line(325, 132, 330, 122);
    }
    if (HDL==1) {
      line(320, 140, 325, 146);
      line(325, 146, 330, 136);
    }
    if (LDL==1) {
      line(320, 153, 325, 159);
      line(325, 159, 330, 149);
    }
    stroke(100);
  }

  void blodprove() {
    if (bp==0) {
      if (mouseX<300&&mouseX>0&&mouseY<305&&mouseY>242) {
        blodprave += "CRP\nHb\nMCV\nJern\nFerritin\nCK\nTroponin T\nLPK\nINR\nHDL\nLDL\n";
        ok += "OK";
        bp = 1;
      }
    }

    if (bp==1) {
      if (mouseX<360&&mouseX>325&&mouseY<26&&mouseY>13) {
        CRP=1;
      }
      if (mouseX<360&&mouseX>325&&mouseY<38&&mouseY>27) {
        HB=1;
      }
      if (mouseX<360&&mouseX>325&&mouseY<52&&mouseY>41) {
        MCV=1;
      }
      if (mouseX<360&&mouseX>325&&mouseY<66&&mouseY>55) {
        JERN=1;
      }
      if (mouseX<380&&mouseX>325&&mouseY<80&&mouseY>68) {
        FERRITIN=1;
      }
      if (mouseX<410&&mouseX>325&&mouseY<93&&mouseY>83) {
        CK=1;
      }
      if (mouseX<400&&mouseX>325&&mouseY<110&&mouseY>97) {
        TROPONINT=1;
      }
      if (mouseX<360&&mouseX>325&&mouseY<123&&mouseY>111) {
        LPK=1;
      }
      if (mouseX<360&&mouseX>325&&mouseY<136&&mouseY>125) {
        INR=1;
      }
      if (mouseX<360&&mouseX>325&&mouseY<150&&mouseY>138) {
        HDL=1;
      }
      if (mouseX<360&&mouseX>325&&mouseY<164&&mouseY>153) {
        LDL=1;
      }
      if (mouseX<360&&mouseX>325&&mouseY<187&&mouseY>178) {
        blodprave = "";
        ok = "";
        bp = 0;
        CRP = 0;
        HB = 0;
        MCV = 0;
        JERN = 0;
        FERRITIN = 0;
        CK = 0;
        TROPONINT = 0;
        LPK = 0;
        INR = 0;
        HDL = 0;
        LDL = 0;
        OK = 0;
      }
    }
  }
}



class Diabetes {
  String text1 = "";
  int X=0;
  int Y=0;
  int A=0;
  void mouse() {
    if (mouseX<40&&mouseX>0&&mouseY<240&&mouseY>200) {
      text1 += "BT: 140/90\n";
    }
    if (mouseX<80&&mouseX>40&&mouseY<240&&mouseY>200) {
      text1 += "BG: 10\n";
    }
    if (mouseX<120&&mouseX>80&&mouseY<240&&mouseY>200) {
      text1 += "Puls: 80\n";
    }
    if (mouseX<160&&mouseX>120&&mouseY<240&&mouseY>200) {
      text1 += "Resp: 15\n";
    }
    if (mouseX<200&&mouseX>160&&mouseY<240&&mouseY>200) {
      text1 += "Temp: 37\n";
    }
    if (mouseX<240&&mouseX>200&&mouseY<240&&mouseY>200) {
      text1 += "CRP: 10\n";
    }
    if (mouseX>400&&mouseX<width&&mouseY>187&&mouseY<210) {
      A+=1;
      X -=14;
    }
    if (mouseX>400&&mouseX<width&&mouseY>167&&mouseY<187) {
      A+=1;
      X +=14;
    }
  }
  void print() {
    fill(255);
    pushMatrix();
    translate(0, X);
    text(text1, 10, 13);
    popMatrix();
  }
  void scroll() {
    Y +=1;
    if (A==0) {
      if (Y>14) {
        X -=14;
      }
    }
    A=0;
  }
}



class Console {
  String text1 = "";
  int X=0;
  int Y=0;
  int A=0;
  int bp = 0;
  void mouse() {
    if (mouseX<40&&mouseX>0&&mouseY<240&&mouseY>200) {
      text1 += "BT: 140/90\n";
    }
    if (mouseX<80&&mouseX>40&&mouseY<240&&mouseY>200) {
      text1 += "BG: 6.3\n";
    }
    if (mouseX<120&&mouseX>80&&mouseY<240&&mouseY>200) {
      text1 += "Puls: 80\n";
    }
    if (mouseX<160&&mouseX>120&&mouseY<240&&mouseY>200) {
      text1 += "Resp: 15\n";
    }
    if (mouseX<200&&mouseX>160&&mouseY<240&&mouseY>200) {
      text1 += "Temp: 37\n";
    }
    if (mouseX<240&&mouseX>200&&mouseY<240&&mouseY>200) {
      text1 += "CRP: 10\n";
    }

    if (mouseX>400&&mouseX<width&&mouseY>187&&mouseY<210) {
      A+=1;
      X -=14;
    }
    if (mouseX>400&&mouseX<width&&mouseY>167&&mouseY<187) {
      A+=1;
      X +=14;
    }

  }
  void print() {
    fill(255);
    pushMatrix();
    translate(0, X);
    text(text1, 10, 13);
    popMatrix();
  }
  void scroll() {
    Y +=1;
    if (A==0) {
      if (Y>14) {
        X -=14;
      }
    }
    A=0;
  }
  void bpcheck() {
    bp += 1;
  }
}
Tagged:

Answers

  • I really have no clue what you want.

    However, I advice to get some more structure in your program. You might want to use guido for a GUI: https://github.com/fjenett/Guido

    The nice thing about that GUI library is that it is easier to change in style compared to the other GUI libraries for processing.

    For the rest, can't you make a class for the blood property, for example:

    class BloodPropery {
    
       int id;
       String name;
       float normalLevel;
       float minDangerLevel;
       float maxDangerLevel;
    
    }
    

    And then create a instance for every property you want?

    BloodPropery MCV = new BloodPropery ...

    And then create a person that can store properties, for example:

    class Person {
    
        HashMap<Integer, Float> bloodValues = new HashMap<Integer, Float>();    
    
        void setBloodProperty(BloodPropery  type, float value) {
            bloodValues.put(type.id, value);     
        }
    
        float getBloodProperyValue(BloodPropery  type) {
           Float val = bloodValues .get(type.id);
           if (val == null) return -1;
           return val; 
        }
    }
    

    That way it might also become a bit more clear to us. Hope it helps.

  • I'm not very good at coding, so I didn't really understand the guido thing... But I'll try harder to understand and rewrite my code so it's more understandable...

    Basically what I'm asking is this: can I introduse int CRP = 0; in one class and then access it like this if(CRP==0){ in another class?

    I tried to google it and I found that some people use "global int" but I don't know if that's what I'm looking for or if that's something else entirely...

    It might be extra difficult to understand my code considering some of it is in Norwegian... I'm terribly sorry for the inconvenience but I really don't know how else to write the code...

    The code you wrote looks amazing and I hope I can use that, but I don't know because I don't fully understand what you did and how to use it...

    Give me some time to look at it and I'll try to wrap my head around it!

  • Answer ✓

    you can make it static.

    class Buttons {
       static int CRP = 0;
    }
    

    and then access it like:

    if(Buttons.CRP == 0)

    Or you access it from an instance:

    Buttons buttons = new Buttons();
    
    if(buttons.CRP == 0)
    

    Or you define it outside the class (in the top of your document for example).

    int CRP = 0;
    
    void setup() { ...
    

    Long story short. I suggest buying "Processing: A Programming Handbook for Visual Designers"
    https://processing.org/books/

    It's a really good book. Programming is more fun when you know it better. And trying to do a project without having much programming knowledge can be extreme hard.

  • Thank you, I'll try that and I am going to check out that book as well :) I already find Processing to be a lot of fun, so I bet it's worth a read.

  • Speaking also for having a structure in your code, it is better if you don't start the variables' name with capital letter. Usually we declare classes with capital first letter and all the variable and functions with small first letter. That way you can easily spot the classes from all the other stuff.

Sign In or Register to comment.