button when clicked turn off other buttons
in
Programming Questions
•
8 months ago
Hello, I have written these buttons that apply some filters to the image, since I can not have two filters active at a time, is it possible to disable the other when a new one is clicked, I cant figure it out by myself so you need your help.
Thanks in advance.
- PImage img;
- Button b1 = new Button(1, "Pixelation");
- Button b2 = new Button(2, "Noise");
- void setup()
- {
- size(500, 340);
- textAlign(CENTER, CENTER);
- img = loadImage("image.jpg");
- }
- void draw() {
- background (255);
- image(img, 0, 0);
- b1.display();
- b2.display();
- }
- void mouseClicked() {
- b1.overButton();
- b2.overButton();
- }
- class Button {
- boolean selected = false;
- int nr, x, y, w, h;
- String name;
- Button (int inNr, String inName) {
- name = inName;
- nr = inNr;
- w = 70;
- h = 20;
- x = inNr*(w+10)-w;
- y = 340-(h+10);
- }
- void display() {
- if (selected) fill(90);
- else fill(150);
- rect(x, y, w, h);
- fill(0);
- text(name, x+(w/2), y+(h/2));
- if (nr == 1 && selected) {
- pixelation();
- }
- else if (nr == 2 && selected) {
- noize();
- }
- }
- void overButton() {
- if (mouseX > x && mouseX < x+w && mouseY > y && mouseY < y+h)
- selected = !selected;
- }
- void pixelation() {
- int size = 10;
- for (int y = 0; y < img.height; y = y + size) {
- for (int x = 0; x < img.width; x = x + size) {
- fill(img.get(x, y));
- stroke(img.get(x, y));
- rect(x, y, size, size);
- }
- }
- }
- void noize() {
- for (int y = 0; y < img.height; y = y + 1) {
- for (int x = 0; x < img.width; x = x + 1) {
- int nX = x + int(random(10));
- color getColor = img.get(nX, y);
- set(x, y, getColor);
- }
- }
- }
- }
1