This is my sketch. The idea is to take a 6 digit number entry and display as a color bar the colors according to the numbers. I have 2 queries. Why is there a white bar at the beginning, and how to clear the display and the entry to begin again without running the sketch again.
import controlP5.*;
ControlP5 cp5;
/* create an array of colors 0-9.
it take up to 6 figure number convert to string */
//int num = (int)(Math.random() * 1000000);
int num;
int potency;
int l = 6;
int[]numbers = new int[l];
color[] colornum = new color[l];
String inputText;
boolean keyPressed = false;
//color colors = new color[10];
// holds the colors needed for drawing
// color number correspondences
//red
color color0 = color(255,0,0);
//orange (102,255,255)
color color1 = color(255,128,0);
//yellow(255,255,0)
color color2 = color(255,255,0);
//green (0,255,0)
color color3 = color(0,255,0);
//blue(0.255,255)
color color4 = color(0,255,255);
//indigo(0,0,255)
color color5 = color(0,0,255);
//violet(127,0.255)
color color6 = color(127,0,255);
//pink(255,0,255)
color color7 = color(255,0,255);
//magneta(255.0,127)
color color8 = color(255,0,127);
//silver (128,128,128)
color color9 = color(128,128,128);
color[] colors = {
color0, color1, color2, color3, color4, color5, color6, color7, color8, color9
};
void setup(){
size(200,200);
background(255);
//numbers to digits.
convert();
cp5 = new ControlP5(this);
cp5.addTextfield("Enter 6 digits")
.setPosition(0, 10)
.setSize(200,30)
.setAutoClear(false)
.setFont(createFont("times",14));
//.setColor(255);
cp5.getController("Enter 6 digits").getCaptionLabel().setColor(color(148,0,211) );
//cp5.getController("Enter Number").getCaptionLabel().setSize(14).setColor(255);
cp5.addBang("clear")
.setPosition(130,10)
.setSize(60,20)
.getCaptionLabel().align(ControlP5.CENTER, ControlP5.CENTER)
;
}
void draw(){
//access array elements
//convert();
inputText = cp5.get(Textfield.class,"Enter 6 digits").getText();
text(inputText,50,70);
if (keyPressed == true){
for (int i = 0; i < l; i++){
int n = numbers[i];
colornum[i] = colors[n];
//println(n + " "+ colors[n]);
}
int sw =30;
background(255);
for (int i = 0; i < l; i++){
//potency = alpha,transparency 0-255
stroke(colornum[i]);
println(colornum);
strokeWeight(sw);
line((i+1)*sw, 30, (i+1)*sw , 200);
}
cp5.getController("Enter 6 digits").getCaptionLabel().setSize(1);
noLoop();
}
}
void convert(){
int pow;
int i;
for (i = 1; i <l+1; i++) { pow = int(pow(10, l-i));
numbers[i-1] = num/pow%10;
//println(i);
//println(pow);
};
//println(i);
// you could continue this pattern for 4,5,6 digit numbers
// dont need to print you could then use the new int values man other ways
println(numbers);
}
void keyPressed() {
if (keyCode == ENTER){
num = int(inputText);
println(num);
convert();
keyPressed = true;
}
}
public void clear() {
cp5.get(Textfield.class,"Enter 6 digits").clear();
inputText= null;
//cp5.getController("Enter Number").getCaptionLabel().setSize(14);
}
void controlEvent(ControlEvent theEvent) {
if(theEvent.isAssignableFrom(Textfield.class)) {
println("controlEvent: accessing a string from controller '"
+theEvent.getName()+"': "
+theEvent.getStringValue()
);
}
}
public void input(String theText) {
// automatically receives results from controller input
println("a textfield event for controller 'input' : "+theText);
}