Quantcast
Channel: Library Questions - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 2896

how to change color button and print the button's label

$
0
0

i have a problem with my codes, i just the button once, but i t print like 3 or more character at the same time. how can i click once and print one character?

import controlP5.*;
ControlP5 cp5;
Println console;
color currentcolor;
RectButton rect1, rect2;
boolean locked = false;
Textarea myTextarea;
int label = 1;

void setup() {
  //set up window
  size(1100, 670);
  color baseColor = color(102, 102, 102);
  currentcolor = baseColor;

  int x = 30;
  int y = 500;
  int size = 50;
  color buttoncolor = color(153, 102, 102);
  color highlight = color(102, 51, 51);
  String label = "A";
  rect1 = new RectButton(x, y, size, buttoncolor, highlight, label);
  // Define and create rectangle button #2
  x = 90;
  y = 500;
  size = 50;
  buttoncolor = color(153, 153, 153);
  highlight = color(102, 102, 102);
 label = "B";
  rect2 = new RectButton(x, y, size, buttoncolor, highlight, label);

  cp5 = new ControlP5(this);
  cp5.enableShortcuts();
  //frameRate(1);
  myTextarea = cp5.addTextarea("txt")
                  .setPosition(50, 50)
                  .setSize(300, 200)
                  .setFont(createFont("", 12))
                  .setLineHeight(12)
                  .setColor(color(255))
                 .setColorBackground(color(000, 145))
                  .setColorForeground(color(255, 111));
   console = cp5.addConsole(myTextarea);
}
void draw() {
  background(currentcolor);
  stroke(255);
  update(mouseX, mouseY);
  rect1.display();
  rect2.display();
}
void update(int x, int y) {
  if(locked == false) {
    rect1.update();
    rect2.update();
  } else {
    locked = false;
  }

  if(mousePressed) {
    if(rect1.pressed()) {            //ON button
      currentcolor = rect1.basecolor;
      print("H");
    } else if(rect2.pressed()) {    //OFF button
      currentcolor = rect2.basecolor;
      //print("L");
    }
  }
}
class Button {
  int x, y;
  int size;
  color basecolor, highlightcolor;
  color currentcolor;
  boolean over = false;
  boolean pressed = false;
  String label = "A";
  void update()
  {
    if(over()) {
      currentcolor = highlightcolor;
    } else {
      currentcolor = basecolor;
    }
  }
  boolean pressed()
  {
    if(over) {
      locked = true;
      return true;
    } else {
      locked = false;
      return false;
    }
  }
  boolean over()
  {
    return true;
  }
  void display()
  {
  }
}
class RectButton extends Button {
  RectButton(int ix, int iy, int isize, color icolor, color ihighlight, String ilabel)
  {
    x = ix;
    y = iy;
    size = isize;
    basecolor = icolor;
    highlightcolor = ihighlight;
    currentcolor = basecolor;
    label = ilabel;
  }
  boolean over()
  {
    if( overRect(x, y, size, size) ) {
      over = true;
      return true;
    } else {
      over = false;
      return false;
    }
  }
  void display()
  {
    stroke(255);
    fill(currentcolor);
    rect(x, y, size, size);
  }
}
boolean overRect(int x, int y, int width, int height) {
  if (mouseX >= x && mouseX <= x+width &&
      mouseY >= y && mouseY <= y+height) {
    return true;
  } else {
    return false;
  }
}

Viewing all articles
Browse latest Browse all 2896

Trending Articles