# Button Class

## Class Button

```java
class Button{
  ///INSTANCE VARIABLES
  float x,y; //position
  float w,h; //size
  boolean selected; //is the button selected / on? true/false
  color selectedColor, defaultColor, currentColor;
  String label; 

  ///CONSTRUCTORS - no return type declared - match Class-name
  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;
    selected = false;
    selectedColor = color( 280, 100, 100); ///must be HSB color
    defaultColor = color( 280, 70, 70); //slightly darker?
    currentColor = defaultColor; 
  }


  ///METHODS
  void display(){
    fill( currentColor);
    rect( x, y, w, h);
    fill( 0);//black for text
    textAlign(CENTER);
    text( label, x + w/2, y + (h/2));
  }

  void clicked( int mx, int my){
    if( mx > x && mx < x + w  && my > y && my < y+h){
      //mouse has been clicked
      selected = !selected;  //toggle the value between true and false
      if( selected){
          currentColor = selectedColor;
      }else{
          currentColor = defaultColor;
      }
    }
  }



}  //end Button class
```

## Main Tab Code - Using Buttons

```java
Button btn1;   //data-type, variable-name   //null
int x;    //data-type, variable-name  //0

///Initialize things - variables
void setup(){
  size( 600, 600);
  colorMode(HSB, 360, 100, 100);
  x= 10; //initialize the variable
  btn1 = new Button( 10, 10, 100, 100, "Press" );  //initialize by calling a Button constructor
}

//makes it a frame-based application
void draw( ){
  btn1.display(); //have the button instance call it's display( ) method
}

//add code for mouseClicked
void mouseClicked( ){
  btn1.clicked( mouseX, mouseY);
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://kdoore.gitbook.io/cs1335-java-and-processing/object-oriented-programming/buttons_as_objects/button-class.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
