Home‎ > ‎

Processing Software: Creating Slide Controls

 
 
The sliding_LED is an example of how to create slide controls using Processing code.  Each slide moves independently and controls a separate color 0-255 and mixes the colors in the rectangle below.

Download sliding_LED.pde file from below and edit it using Processing Software.  Please note that you have to go to "Tools>Create Font..." and create the Font and modify the "f = loadFont("AgencyFB-Bold-16.vlw");" statement to add your own font.
-----------------------------------

Slides[ ] slide;
int numberofslides;
PFont f;

void setup()
{
  frame.setResizable(true);
  numberofslides = 3;
  int yspace = 30;
  int xspace = 40; 
  int slength = 255;
  size(slength + 2*xspace, 50 + yspace + numberofslides*yspace);
  slide = new Slides[numberofslides];
  int hxsize = 10;
  int hysize = 10;
  int hxstart = 0;
  int yloc;
  f = loadFont("AgencyFB-Bold-16.vlw");

  for(int i=0; i<numberofslides; i++) {
    yloc = yspace+i*yspace;
    slide[i] = new Slides(hxsize, hysize, slength, xspace, hxstart, yloc, slide);
  }
}

void draw()
{
  background(153);
 
  for(int i=0; i<numberofslides; i++) {
    slide[i].update();
    slide[i].display();
  }
  }

void mouseReleased()
{
  for(int i=0; i<numberofslides; i++) {
    slide[i].release();
  }
}

class Slides
{
  int xboxsize, yboxsize;
  int xboxpos, yboxpos;
  int slidelength;
  int xsloc, ysloc;
  int xtloc, ytloc;
  int xvalue;
  boolean over;
  boolean press;
  boolean keepWithinRange = false;
  boolean otherskeepWithinRange = false;
  String message = ""+ xvalue;
  Slides[] others;
 
  Slides(int hxsize, int hysize, int slength, int slstart, int hxstart, int sypos, Slides[] o)
  {
    xboxsize = hxsize;
    yboxsize = hysize;
    slidelength = slength;
    xsloc = slstart+xboxsize/2;
    ysloc = sypos;
    xtloc = width - (width-slength)/2 +20;
    ytloc = sypos + 5;
    xboxpos = xsloc + hxstart - xboxsize/2;
    xvalue = xboxpos-xsloc+xboxsize/2;
      println(xvalue);
    yboxpos = ysloc - yboxsize/2;
    others = o;
  }
 
  void update()
  {
     for(int i=0; i<others.length; i++) {
      if(others[i].keepWithinRange == true) {
        otherskeepWithinRange = true;
        break;
      } else {
        otherskeepWithinRange = false;
      } 
    }
   
    if(otherskeepWithinRange == false) {
      over();
      press();
    }
   
    if(press) {
      xboxpos = keepWithinRange(mouseX, xsloc-xboxsize/2, xsloc+slidelength-xboxsize/2);
      xvalue = xboxpos-xsloc+xboxsize/2;//(width-slidelength)/2+xboxsize/2;
      message = ""+ xvalue;
      println(xvalue);
   }
  }
 
  void over()
  {
    if(overHandle(xboxpos, yboxpos, xboxsize, yboxsize)) {
      over = true;
    } else {
      over = false;
    }
  }
 
  void press()
  {
    if(over && mousePressed || keepWithinRange) {
      press = true;
      keepWithinRange = true;
    } else {
      press = false;
    }
  }
 
  void release()
  {
    keepWithinRange = false;
  }
 
  void display()
  {
    //Draw slide bar
    line(xsloc, ysloc, xsloc + slidelength, ysloc);
   
    //Draw base slide
    fill(255); //No Color
    stroke(0); //Black Lin
    rect(xboxpos, yboxpos, xboxsize, yboxsize);
   
    //Draw Text
    text(message,xtloc,ytloc);
   
    //Draw Colored Rectangle
    fill(slide[0].xvalue,slide[1].xvalue,slide[2].xvalue); //Variable Color
    stroke(0); //Black Lin
    rect(width/4, height/6*4, width/2, height/6);

    //Little Green LEDs
    fill(0,255,0);
    noStroke();
    rect(xboxpos, yboxpos, xboxsize/4, yboxsize);
    rect((xboxpos+xboxsize-xboxsize/4+1), yboxpos, xboxsize/4, yboxsize);
    stroke(0);

    if(over) {
    //Little Yellow LEDs
        fill(255,255,0);
        noStroke();
        rect(xboxpos, yboxpos, xboxsize/4, yboxsize);
        rect((xboxpos+xboxsize-xboxsize/4+1), yboxpos, xboxsize/4, yboxsize);
        stroke(0);
      if(press) { 
    //Little Red LEDs
        fill(255,0,0);
        noStroke();
        rect(xboxpos, yboxpos, xboxsize/4, yboxsize);
        rect((xboxpos+xboxsize-xboxsize/4+1), yboxpos, xboxsize/4, yboxsize);
        text(message,xtloc,ytloc);
        stroke(0);
      }
    }

  }
}

boolean overHandle(int x, int y, int width, int height)
{
  if (mouseX >= x && mouseX <= x+width &&
      mouseY >= y && mouseY <= y+height) {
    return true;
  } else {
    return false;
  }
}

int keepWithinRange(int val, int minv, int maxv)
{
  return  min(max(val, minv), maxv);
}


 



ċ
Slide_Controlled_Plot.jar
(188k)
Timothy Schmidt,
Oct 23, 2012, 12:11 PM
ċ
sliding_LED.pde
(4k)
Timothy Schmidt,
Oct 20, 2011, 4:19 PM
Comments