GUI problem
in
Programming Questions
•
8 months ago
Hello. I am trying to make simple gui(in fact I am editing "Buttons" example)
I have got array and I would like to append element by clicking circlebutton. I thought it would be simply.
But when I clik it append more than one element. How should I write that to append only one?
My code.Thanks for any reply:)
- int[] El_num = new int[0];
- CircleButton circle1, circle2, circle3;
- RectButton rect1, rect2;
- boolean locked = false;
- void setup()
- {
- background(255);
- size(400, 400);
- smooth();
- color buttoncolor = color(204);
- color highlight = color(153);
- ellipseMode(CENTER);
- // Define and create circle button
- circle1 = new CircleButton(300, 40, 24, buttoncolor, highlight);
- // Define and create circle button
- circle2 = new CircleButton(300, 70, 24, buttoncolor, highlight);
- circle3 = new CircleButton(30, 100, 100, buttoncolor, highlight);
- // Define and create rectangle button
- rect1 = new RectButton(150, 20, 100, buttoncolor, highlight);
- // Define and create rectangle button
- rect2 = new RectButton(90, 20, 50, buttoncolor, highlight);
- }
- void draw()
- {
- background(255);
- //stroke(255);
- strokeWeight(2);
- update(mouseX, mouseY);
- circle1.display();
- circle2.display();
- circle3.display();
- rect1.display();
- rect2.display();
- println(El_num.length);
- }
- void update(int x, int y)
- {
- if(locked == false) {
- circle1.update();
- circle2.update();
- circle3.update();
- rect1.update();
- rect2.update();
- }
- else {
- locked = false;
- }
- //---THERE DATA
- if(mousePressed) {
- if(circle1.pressed()) {
- El_num = append(El_num, 7);
- }
- else if(circle2.pressed()) {
- // currentcolor = circle2.basecolor;
- }
- else if(circle3.pressed()) {
- // currentcolor = circle3.basecolor;
- }
- else if(rect1.pressed()) {
- //currentcolor = rect1.basecolor;
- }
- else if(rect2.pressed()) {
- // currentcolor = rect2.basecolor;
- }
- }
- }
- class Button
- {
- int x, y;
- int size;
- color basecolor, highlightcolor;
- color currentcolor;
- boolean over = false;
- boolean pressed = false;
- 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;
- }
- 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;
- }
- }
- boolean overCircle(int x, int y, int diameter)
- {
- float disX = x - mouseX;
- float disY = y - mouseY;
- if(sqrt(sq(disX) + sq(disY)) < diameter/2 ) {
- return true;
- }
- else {
- return false;
- }
- }
- }
- class CircleButton extends Button
- {
- CircleButton(int ix, int iy, int isize, color icolor, color ihighlight)
- {
- x = ix;
- y = iy;
- size = isize;
- basecolor = icolor;
- highlightcolor = ihighlight;
- currentcolor = basecolor;
- }
- boolean over()
- {
- if( overCircle(x, y, size) ) {
- over = true;
- return true;
- }
- else {
- over = false;
- return false;
- }
- }
- void display()
- {
- strokeWeight(1);
- stroke(0);
- fill(currentcolor);
- ellipse(x, y, size, size);
- }
- }
- class RectButton extends Button
- {
- RectButton(int ix, int iy, int isize, color icolor, color ihighlight)
- {
- x = ix;
- y = iy;
- size = isize;
- basecolor = icolor;
- highlightcolor = ihighlight;
- currentcolor = basecolor;
- }
- boolean over()
- {
- if( overRect(x, y, size, size) ) {
- over = false;
- return false;
- }
- else {
- over = false;
- return false;
- }
- }
- void display()
- {
- strokeWeight(1);
- stroke(0);
- fill(currentcolor);
- rect(x, y, size, size);
- }
- }
1