Friday 8 May 2009

Chunk 85 - Advanced Buttons 2 (Part 2)

...... Let’s start by adding the ability to change the colour of a button, and to give the button a label so that anyone using our application knows the purpose of the button.

We will need to add two more instance properties, under the ones for position and size, in order to store details of the colour and button text.

// Button Position and Size Properties
int btnTopX; // Button Top X Coordinate
int btnTopY; // Button Top Y Coordinate
int btnWidth; // Button Width
int btnHeight; // Button Height
color btnColour; // Button Colour
String btnText; // Button Label Text

We also need to change the constructor method to set default values for these new variables.
Button (int X, int Y, int W, int H)
{
this.btnTopX = X; // Set Button Top X Coordinate
this.btnTopY = Y; // Set Button Top Y Coordinate
this.btnWidth = W; // Set Button Width
this.btnHeight = H; // Set Button Height
this.btnColour = 128; // Default Colour
this.btnText = ""; // Default Button Label Text
}

In order for our application to set these values we have to create some ‘setter’ methods in the button class, which allow us to specify values for the text label and colour of the button. These setter methods appear within the class definition for a button, immediately after the constructor method.
// Setter Methods - Methods that can be used by an
// application to set instance variables within an
// instance of the button class

// Set the Button Label Text
public void setButtonLabel(String labelText)
{
this.btnText = labelText;
}

// Set the Button Colour
public void setButtonColour(int colourValue)
{
this.btnColour = colourValue;
}

Now we need to amend the drawButton() method to use the colour defined for the button, and to draw the button label if it is defined.
// Draw the button on the screen
public void drawButton()
{
// Set the button colour
fill(this.btnColour);

// Draw the button
rect(this.btnTopX, this.btnTopY, this.btnWidth, this.btnHeight);

// Draw the button label if the text is defined (not blank)
if (! this.btnText.equals(""))
{
// Select the font for the text
textFont(btnFont,12);

// Set the colour to white
fill(255);

// Calculate the horizontal centre of the button, then subtract half
// the width of the button label to find the starting position
float textX = (this.btnTopX + this.btnWidth / 2) – (textWidth(this.btnText) / 2);

// Calculate the vertical centre of the button
float textY = (this.btnTopY + this.btnHeight / 2) + 6;

// Output the text
text(this.btnText, textX, textY);
}
}

You will notice that we have replaced the fill statement in the drawButton method with a fill statement using the colour attribute variable defined for the button being drawn. We have also added a check to see if the button has a text label variable defined for it, and if it does we set the colour to white and output the label text on the button.

In order to make the button look as good as possible we should position the button label so it is centered, to do this we calculate the starting X position for the text as being the X position of the button (this.btnTopX) plus half the width of the button (this.btnWidth / 2) minus half the pixel length of the button label (textWidth(this.btnText) / 2). The starting Y position is calculated as the Y position of the button (this.btnTopY) plus half the height of the button (this.btnHeight / 2) plus half the size of the font being used for the text.

Finally we can change the setup procedure to load the font we want to use, and specify our additional instance properties for our buttons.

Button btn1;
Button btn2;
PFont btnFont;

void setup() {
size(400,400); // Set screen size
smooth(); // Anti-aliased (smooth) edges

// Load the font we want to use on the buttons
btnFont=loadFont("Verdana-Bold-12.vlw");

// Create a new button
btn1 = new Button(10,365,100,25);
btn1.setButtonColour(color(128,128,255));
btn1.setButtonLabel("On");

// Create a new button
btn2 = new Button(120,365,100,25);
btn2.setButtonColour(color(128,128,255));
btn2.setButtonLabel("Off");

}


When we run our program now we get a screen with two buttons as shown in figure 2.0

Figure 2.0 Colours and Labels





// Button Class
// Development of a button class - Stage 2 of 5
// 2009 Nigel Parker

Button btn1; // On Button
Button btn2; // Off Button
PFont btnFont;

void setup() {
size(400,400); // Set screen size
smooth(); // Anti-aliased (smooth) edges

// Load the font we want to use on the buttons
btnFont=loadFont("Verdana-Bold-12.vlw");

// Create a new button and set the colour and label
btn1 = new Button(10,365,100,25);
btn1.setButtonColour(color(128,128,255));
btn1.setButtonLabel("On");

// Create a new button and set the colour and label
btn2 = new Button(120,365,100,25);
btn2.setButtonColour(color(128,128,255));
btn2.setButtonLabel("Off");

}

void draw () {

// Clear the background and set the background colour
background(128,128,128);

// Process button one
btn1.drawButton();

// Process button two
btn2.drawButton();

}

// Definition of a Button class that will allow us to
// create and use buttons within our processing code
class Button {

// Button Position and Size Properties
int btnTopX; // Button Top X Coordinate
int btnTopY; // Button Top Y Coordinate
int btnWidth; // Button Width
int btnHeight; // Button Height
color btnColour; // Button Colour
String btnText; // Button Label Text

// The button constructor is called to create a new button
// and stores the position and size information provided
// in the instance variables of the object creaetd
Button (int X, int Y, int W, int H)
{
this.btnTopX = X; // Set Button Top X Coordinate
this.btnTopY = Y; // Set Button Top Y Coordinate
this.btnWidth = W; // Set Button Width
this.btnHeight = H; // Set Button Height
this.btnColour = 128; // Default Colour
this.btnText = ""; // Default Button Label Text
}

// Setter Methods - Methods that can be used by an
// application to set instance variables within an
// instance of the button class

// Set the Button Label Text
public void setButtonLabel(String labelText)
{
this.btnText = labelText;
}

// Set the Button Colour
public void setButtonColour(int colourValue)
{
this.btnColour = colourValue;
}

// Draw the button on the screen
public void drawButton()
{
// Set the button colour
fill(this.btnColour);

// Draw the button
rect(this.btnTopX, this.btnTopY, this.btnWidth, this.btnHeight);

// Draw the button label if the text is defined (not blank)
if (! this.btnText.equals(""))
{
// Select the font for the text
textFont(btnFont,12);

// Set the colour to white
fill(255);

// Calculate the horizontal centre of the button, then subtract half
// the width of the button label to find the starting position
float textX = (this.btnTopX + this.btnWidth / 2) - (textWidth(this.btnText) / 2);

// Calculate the vertical centre of the button
float textY = (this.btnTopY + this.btnHeight / 2) + 6;

// Output the text
text(this.btnText, textX, textY);
}
}
}

2 comments:

  1. Have you given any thought to the buttons available via the controlP5 library? Following is a link to a program using them.

    http://www.seas.harvard.edu/courses/cs171/final_project/web_pages/bosmeny-t_carroll-d/Programs/search_data_viz/applet/search_data_viz.pde

    ReplyDelete
  2. Actually that example is a bit too complicated for a simpler example look at my chunk42 blog (just click on the openid to follow)

    ReplyDelete