Question about classes

edited October 2013 in Questions about Code

I'm trying to create a code where if you click on the screen an object will change, using classes, but I keep getting the error message "Cannot make a static reference to the non-static method redness() from the type Assignment5_Question1_Attempt4.Stress.

here's the code for my stress class:

class Stress
{
  float stressX = width/2;
  float stressY = height/3;

  Stress(float _x, float _y)
  {
    stressX = _x;
    stressY = _y;
  }

  void redness()
  {
    fill(255, 0, 0);
      ellipse(stressX, stressY + 7, 45, 50);  //head

      //eyes
      fill(255);
      ellipse(stressX - 8, stressY + 2, 14, 14);  //left eye
      fill(0, 0, 255);
      ellipse(stressX - 5, stressY + 3, 2, 2);  //left pupil
      fill(255);
      ellipse(stressX + 8, stressY + 2, 14, 14);  //right eye
      fill(0, 0, 255);
      ellipse(stressX + 5, stressY + 3, 2, 2);  //right pupil

      //mouth
      fill(255);
      ellipse(stressX, stressY + 15, 20, 10);

      //hair
      stroke(222, 152, 2);  
      line(stressX-1, stressY-18, stressX-3, stressY-25);
      line(stressX-1, stressY-18, stressX+1, stressY-25);
      line(stressX-3, stressY-18, stressX-5, stressY-25);
      line(stressX-5, stressY-18, stressX-7, stressY-25);
      line(stressX-7, stressY-18, stressX-9, stressY-25);
      line(stressX-9, stressY-17, stressX-11, stressY-24);
      line(stressX-11, stressY-14, stressX-13, stressY-21);
      line(stressX-13, stressY-14, stressX-15, stressY-21);
      line(stressX-15, stressY-14, stressX-17, stressY-21);
      line(stressX+1, stressY-18, stressX+3, stressY-25);
      line(stressX+1, stressY-18, stressX-1, stressY-25);
      line(stressX+3, stressY-18, stressX+5, stressY-25);
      line(stressX+5, stressY-18, stressX+7, stressY-25);
      line(stressX+7, stressY-18, stressX+9, stressY-25);
      line(stressX+9, stressY-17, stressX+11, stressY-24);
      line(stressX+11, stressY-14, stressX+13, stressY-21);
      line(stressX+13, stressY-14, stressX+15, stressY-21);
      line(stressX+15, stressY-14, stressX+17, stressY-21);

      stroke(0);
      line(stressX, stressY - 18, stressX-2, stressY - 25);
      line(stressX, stressY - 18, stressX, stressY-25);
      line(stressX-2, stressY-18, stressX-4, stressY-25);
      line(stressX-4, stressY-18, stressX-6, stressY-25);
      line(stressX-6, stressY-18, stressX-8, stressY-25);
      line(stressX-8, stressY-17, stressX-10, stressY-24);
      line(stressX-10, stressY-14, stressX-12, stressY-21);
      line(stressX-12, stressY-14, stressX-14, stressY-21);
      line(stressX-14, stressY-14, stressX-16, stressY-21);
      line(stressX, stressY - 18, stressX+2, stressY-25);
      line(stressX+2, stressY-18, stressX+4, stressY-25);
      line(stressX+4, stressY-18, stressX+6, stressY-25);
      line(stressX+6, stressY-18, stressX+8, stressY-25);
      line(stressX+8, stressY-17, stressX+10, stressY-24);
      line(stressX+10, stressY-14, stressX+12, stressY-21);
      line(stressX+12, stressY-14, stressX+14, stressY-21);
      line(stressX+14, stressY-14, stressX+16, stressY-21);
  }
}

the code for my person class:

class Person
{
  float headX = width/2;
  float headY = height/3;

  Person(float x, float y)
  {
    headX = x;
    headY = y;
  }

  void display()
  {
    fill(252, 246, 189);
    stroke(0);
    ellipse(headX, headY + 7, 45, 50);  //head

    //eyes
    fill(255);
    ellipse(headX - 5, headY + 2, 7, 7);  //left eye
    fill(0, 0, 255);
    ellipse(headX - 5, headY + 3, 2, 2);  //left pupil
    fill(255);
    ellipse(headX +5, headY + 2, 7, 7);  //right eye
    fill(0, 0, 255);
    ellipse(headX + 5, headY + 3, 2, 2);  //right pupil

    //nose
    stroke(0); 
    line(headX, headY+9, headX - 2, headY + 12);
    line(headX - 2, headY +12, headX, headY + 12);

    //mouth
    line(headX - 5, headY +20, headX - 3, headY + 18);
    line(headX - 3, headY + 18, headX - 1, headY +20);
    line(headX - 1, headY +20, headX +1, headY+18);
    line(headX + 1, headY+18, headX+3, headY+20);

    //hair
    stroke(222, 152, 2);  
    line(headX-1, headY-18, headX-3, headY-10);
    line(headX-1, headY-18, headX+1, headY-10);
    line(headX-3, headY-18, headX-5, headY-10);
    line(headX-5, headY-18, headX-7, headY-10);
    line(headX-7, headY-18, headX-9, headY-10);
    line(headX-9, headY-17, headX-11, headY-9);
    line(headX-11, headY-14, headX-13, headY-8);
    line(headX-13, headY-14, headX-15, headY-8);
    line(headX-15, headY-14, headX-17, headY-8);
    line(headX+1, headY-18, headX+3, headY-10);
    line(headX+1, headY-18, headX-1, headY-10);
    line(headX+3, headY-18, headX+5, headY-10);
    line(headX+5, headY-18, headX+7, headY-10);
    line(headX+7, headY-18, headX+9, headY-10);
    line(headX+9, headY-17, headX+11, headY-9);
    line(headX+11, headY-14, headX+13, headY-8);
    line(headX+13, headY-14, headX+15, headY-8);
    line(headX+15, headY-14, headX+17, headY-8);

    stroke(0);
    line(headX, headY - 18, headX-2, headY-10);
    line(headX, headY - 18, headX, headY-10);
    line(headX-2, headY-18, headX-4, headY-10);
    line(headX-4, headY-18, headX-6, headY-10);
    line(headX-6, headY-18, headX-8, headY-10);
    line(headX-8, headY-17, headX-10, headY-8);
    line(headX-10, headY-14, headX-12, headY-9);
    line(headX-12, headY-14, headX-14, headY-8);
    line(headX-14, headY-14, headX-16, headY-8);
    line(headX, headY - 18, headX+2, headY-10);
    line(headX+2, headY-18, headX+4, headY-10);
    line(headX+4, headY-18, headX+6, headY-10);
    line(headX+6, headY-18, headX+8, headY-10);
    line(headX+8, headY-17, headX+10, headY-8);
    line(headX+10, headY-14, headX+12, headY-9);
    line(headX+12, headY-14, headX+14, headY-8);
    line(headX+14, headY-14, headX+16, headY-8);
  }
}

and finally my general code:

Person myPerson;
Stress myStress;
float headX = width/2;
float headY = height/3;
float stressX = width/2;
float stressY = height/3;

void setup()
{
  size(500,500);

  myPerson = new Person(headX,headY);
  myStress = new Stress(stressX,stressY);
}

void draw()
{
  if (mousePressed)
  {
    if (mouseY <= height/2)
    {
      println("Stressed Out");
      Stress.redness();
    }
    else
    {
      println("Relaxed");
      Person.display();
    }
  }
}

Answers

  • edited October 2013 Answer ✓

    When you post code here; highlight it and hit CTRL+K! [-(
    Method redness() isn't static. It means it gotta be invoked thru' an instantiated object's reference from its class! :-&
    You got that reference stored in variable myStress. So, just use that instead of the class name! 3:-O

    Person myPerson; 
    Stress myStress; 
    
    // Wrong! width & height are 0 before setup()!!!
    float headX = width/2; 
    float headY = height/3; 
    float stressX = width/2; 
    float stressY = height/3;
    
    void setup() { 
      size(500, 500);
    
      myPerson = new Person(headX, headY); 
      myStress = new Stress(stressX, stressY);
    }
    
    void draw() { 
      if (mousePressed) { 
        if (mouseY <= height/2) { 
          println("Stressed Out"); 
          //Stress.redness();  // Gotta use object's reference instead!!!
          myStress.redness();
        } 
        else { 
          println("Relaxed"); 
          //Person.display();  // Gotta use object's reference instead!!!
          myPerson.display();
        }
      }
    }
    
  • Sorry about the format, it was my first post. Thanks a lot though, that was really helpful

Sign In or Register to comment.