Processing ControlP5 example 2: Style and setValue

Updated code in 2017 to work with the version 2.2.6 of ControlP5.

In this example you’ll see how to style the GUI that you’ve made with the ControlP5 library (see first article : Processing ControlP5 example 1: user interface for the basics.)

controlP5 styling interface elements

Download the zip-file ( with all the examples ) : controlP5-examples.zip

This example is pretty straight forward. You give some style the controlP5 object with the ‘ControlFont’ and ‘setColor….’ methods.

In the controlEvent function we use the values of some control elements to control the value of others. The setValue method is useful when you want to monitor variables in a visual way.

  • Slider1 controls Slider2.
  • Bang1 sets Toggle1 to 1 (on/true).
  • Button1 sets Toggle1 to 0 (off/false).
  • Numberbox1 controls Knob1.
// ControlP5 Example 2 : Style UI elements and setValue
// ControlP5 by Andreas Schlegel : 
// http://www.sojamo.de/libraries/controlP5/

import controlP5.*;
ControlP5 controlP5;

void setup() {
 size(300,260);
 smooth();
 
 controlP5 = new ControlP5(this);
 
 // change the default font to Verdana
 PFont p = createFont("Verdana",10); 
 ControlFont font = new ControlFont(p);
 
 // change the original colors
 controlP5.setColorForeground(0xffaa0000);
 controlP5.setColorBackground(0xff660000);
 controlP5.setFont(font);
 controlP5.setColorActive(0xffff0000);
 
 // add the elments ( see example 1 for the parameters )
 controlP5.addBang("bang1")
 .setPosition(10,10)
 .setSize(20,20); 
 controlP5.addButton("button1")
 .setValue(1)
 .setPosition(70,10)
 .setSize(60,20); 
 controlP5.addToggle("toggle1")
 .setValue(false)
 .setPosition(170,10)
 .setSize(20,20); 
 controlP5.addSlider("slider1")
 .setRange(0,255)
 .setValue(128)
 .setPosition(10,80)
 .setSize(10,100)
 .setColorValue(0xffff88ff)
 .setColorLabel(0xffdddddd);
 
 Slider s = controlP5.addSlider("slider2")
 .setRange(0,255)
 .setValue(128)
 .setPosition(70,80)
 .setSize(100,10);
 // change sliderMode of the Slider object. The default is Slider.FIX
 s.setSliderMode(Slider.FLEXIBLE); 
 
 controlP5.addKnob("knob1")
 .setRange(0,360)
 .setValue(0)
 .setPosition(70,120)
 .setSize(50,50);
 
 Numberbox n = controlP5.addNumberbox("numberbox1")
 .setValue(50)
 .setPosition(170,120)
 .setSize(60,14);
 // change Multiplier of the Numberbox ( default is 1 )
 n.setMultiplier(30);
}
 
void draw() { 
 background(0); // background black
}

void controlEvent(ControlEvent theEvent) {
 
 
 if(theEvent.isController()) { 
 
 print("control event from : "+theEvent.getController().getName());
 println(", value : "+theEvent.getController().getValue());
 
 // clicking on bang1 sets toggle1 value to 1 (true) 
 if(theEvent.getController().getName()=="bang1") {
 controlP5.getController("toggle1").setValue(1); 
 }
 
 // clicking on button1 sets toggle1 value to 0 (false)
 if(theEvent.getController().getName()=="button1") {
 controlP5.getController("toggle1").setValue(0); 
 }
 
 // dragging slider1 changes the value of slider2
 if(theEvent.getController().getName()=="slider1") {
 float value = theEvent.getController().getValue();
 // prevent warnings at start
 if(controlP5.getController("slider2") != null) {
 controlP5.getController("slider2").setValue(value);
 }
 }
 
 
 // changing the value of numberbox1 turns knob1
 if(theEvent.getController().getName()=="numberbox1") {
 controlP5.getController("knob1").setValue(theEvent.getController().getValue());
 }
 
 
 } 
 
}

8 thoughts on “Processing ControlP5 example 2: Style and setValue”

  1. ¿Can you add image in background for one listBox.?

    Now I have this: setBackgroundColor(color(100,0,0));

    But I want a image and not a background color.

    Sorry for my bad english.Thanks

  2. Hi, how do i make differents sizes of fonts for my buttoms? I set only size but i can’t do it for other buttoms in the same scketch. All buttoms are the same size that the first buttom. Thanks!

  3. hello sir
    i want to send the number to the arduino through processing gui and i have used the text field for the entering the numbers (integers) how to send these numbers to the arduino port the code which i have wrote i am pasting here please guide me

    **removed code**

  4. Sorry, I don’t have time to answer questions about integrating technologies together. Try to make baby steps. First see if you can send information to Arduino without the GUI, then start using the GUI.

  5. You can load an image on a controlP5 screen, as background, and then generate controllers on top of it.
    def setup():
    global img
    size(900, 600)

    global cp5
    cp5 = ControlP5(this)
    img = loadImage(“rbtarm.jpg”)

    (cp5.addKnob(‘m2’)
    .setPosition(120,40)
    .setRadius(40)
    .setRange(102,736)
    .setValue(580)
    )
    The Knob(‘m2’) is placed on the image and work as it’s supposed to.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.