Problem passing arguments to class?

Here is my class. I have it in a separate tab.

class button{
  int buttonX, buttonY, textSize, buttonWidth, buttonHeight;
  String buttonText;
  boolean isActive=false;
  button(int x, int y, int size, String t){
    buttonX = x;
    buttonY = y;
    textSize = size;
    buttonText = t;
    buttonWidth = (int) (textWidth(buttonText)+textWidth(buttonText)/4);
    buttonHeight = (int) (textSize+textSize/4);
  }

  int width(){
    return buttonWidth;
  }

  int height(){
    return buttonHeight;
  }

  String message(){
    return buttonText;
  }

  int xloc(){
    return buttonX;
  }

  int yloc(){
    return buttonY;
  }

  void changeSize(int x){
    textSize = x;
  }

  void build(){
    noStroke();
    if(mouseX >= buttonX && mouseX<= buttonX+buttonWidth && mouseY >=buttonY && mouseY <= buttonY+buttonHeight){
      isActive = true;
    }else{
      isActive = false;
    }
    if(isActive){
      fill(255);
    }else{
      fill(200);
    }
    rect(buttonX, buttonY, buttonWidth, buttonHeight, buttonHeight/4);
    fill(0);
    textAlign(CENTER,CENTER);
    textSize(textSize);
    text(buttonText,buttonX+buttonWidth/2, buttonY+buttonHeight/3/*fill*/);
  }
}

The class seems to be working fine. The problem is when I call it.

If I don't have this line constantly updating:

thisone = new button(50,200,60,"Ryan Sheets");

it doesn't build my button correctly.

This code

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

  background(0);
  thisone = new button(50,200,60,"Ryan Sheets");
}

void draw(){
  thisone.build();
}

displays this:

Screen Shot 2015-12-05 at 1.07.07 PM

while this code:

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

  background(0);
}

void draw(){
  thisone = new button(50,200,60,"Ryan Sheets");
  thisone.build();
}

displays this:

Screen Shot 2015-12-05 at 1.07.29 PM

What is the problem here and how can I fix it?

Answers

  • Quick update: I realize that I shouldn't have used int width() and int height() as my getters in the class, but that is not the problem, and I have checked it with them changed to getWidth() and getHeight()

  • You didn't answer my question. I know how background and draw work.

  • I know how background() and draw() work.

    So why your background() is inside setup() instead of draw()?! :>

  • edited December 2015

    For exactly the reason that is stated in the link you just uploaded from the reference. Read it yourself. It says:

    The background() function sets the color used for the background of the Processing window. The default background is light gray. This function is typically used within draw() to clear the display window at the beginning of each frame, but it can be used inside setup() to set the background on the first frame of animation or if the backgound need only be set once."

    I only wanted to set it for one frame at the beginning.

  • edited December 2015

    ... if the backgound need only be set once.

  • IT DOESN'T NEED TO BE SET MORE THAN ONCE! I have an issue with the button. This is not a large project. I'm just making a button. That is all. I do not need to draw the background more than once. I do not need for things to be removed from the screen. Nothing is moving. Placing background() inside draw() will NOT fix my issue.

  • edited December 2015

    OK... although it's recommended to use background() at the beginning of draw(). But it's your choice!

    Anyways, know that textWidth() is recalculated after textSize()! L-)

  • I don't understand what you mean by your last comment. How is textWidth() recalculated after textSize()? Please elaborate.

  • edited December 2015

    textWidth() returns the characters' width for current textFont() + its current textSize().
    If either changes, it's expected for textWidth() to return a different value when invoked again! :-B

  • I understand that, but how is that affecting my problem?

  • Answer ✓

    You invoke and cache textWidth() inside constructor.
    Then invoke textSize() inside build() method.
    Of course the cached value doesn't correspond to current textSize() now.

  • edited December 2015 Answer ✓

    The sketch from this thread below: https://forum.Processing.org/two/discussion/comment/25959/#Comment_25959
    I only cache textWidth() in txtW after textFont(), which is already size = 15:

    textFont(createFont("DejaVu Sans", 15));
    textAlign(LEFT, BASELINE);
    txtW = ADJUST + textWidth(TXT);
    

    P.S.: I do change textSize() within draw(). However, I re-calculate coordinates relying on txtW.

    textSize(15);
    text(TXT, width - txtW, height - 12);
    
  • Ok. I understand now what the problem was. Is the most efficient way of fixing it to put textSize() in the constructor before I set the width?

  • Answer ✓

    Most likely yes, before your textWidth() calculations. *-:)

  • Thank you very much. Problem solved.

Sign In or Register to comment.