Code Wed Apr 8 v1

During the class demo, the button's colors didn't show correctly. It was a classic error, it would be nice if I could say I'd planned to make the error, but it was an honest error. So a good lesson to be learned:

The problem was that the colors that were initialized in the constructor were local variables, since I included the dataType: color. So, rather than initializing the class instance variables, I created new local variables: and the instance variables used their default colors of 0,0,0:

//Incorrect code in the constructor
 Button( float x, float y, float w, float h, String label  ){
    this.x = x;
    this.y = y;
    this.w = w;
    this.h = h;
    this.label = label;
    
    //the problem is that I created new local variables
    //rather than initializing the class instance variables
    color selectedColor = color( 280, 100, 100); //purple
    color defaultColor = color( 280, 80,70);//dull, dark version
    color currentColor = defaultColor;
    
    selected = false; //button starts in off state
  } //end constructor

//Correct code in the constructor
Button( float x, float y, float w, float h, String label  ){
    this.x = x;
    this.y = y;
    this.w = w;
    this.h = h;
    this.label = label;
    
    //Correct code:  remove dataType:  color 
    //so that I'm now setting values for instance variables
   selectedColor = color( 280, 100, 100); //purple
   defaultColor = color( 280, 80,70);//dull, dark version
   currentColor = defaultColor;
    
    selected = false; //button starts in off state
  } //end constructor

Full Code: Main Tab

Class Button - version 1

Last updated

Was this helpful?