We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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:
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:
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()
https://Processing.org/reference/background_.html
https://Processing.org/reference/draw_.html
You didn't answer my question. I know how background and draw work.
So why your background() is inside setup() instead of draw()?! :>
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.
... 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.
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.
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?
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.
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:
P.S.: I do change textSize() within draw(). However, I re-calculate coordinates relying on txtW.
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?
Most likely yes, before your textWidth() calculations. *-:)
Thank you very much. Problem solved.