My buttons wont work

edited April 2017 in Library Questions

Hi, I need to make a clock for a project. The buttons on the clock have to come from a button class, which I have made. However, only the last button in my code is clickable. Everything works using the keyboard, but not the mouse. I hope you can help me, since none of my teachers are available for questions. Thanks!

**Main:**

boolean pressedLeft=false; //setting up boolean to know if the mouse is being clicked or not
boolean turnOnAlarm=false;
color buttoncolor;
Button but, but1, but2, but3, but4;
int alarmh=00;
int alarmm=00;
int alarms=00;

Minim minim; //minim audioplayer
AudioPlayer player;

void setup()
{
  size(600, 400);

  textAlign(CENTER, CENTER);
  textSize(72);
  minim = new Minim(this); //asking minim to use elements from the folder
  player = minim.loadFile("Old-fashioned-alarm-clock.mp3");

  buttoncolor = color(51, 153, 255);
  but = new Button(50, 330, 50, buttoncolor);
  but1 = new Button(160, 330, 50, buttoncolor);
  but2 = new Button(275, 330, 50, buttoncolor);
  but3 = new Button(390, 330, 50, buttoncolor);
  buttoncolor = color(255, 255, 51);
  but4 = new Button(50, 175, 50, buttoncolor);
}

void mousePressed()
{
  if (but.pressed())
  {
    if (turnOnAlarm==false)
    {turnOnAlarm=true;}
    else if (turnOnAlarm==true)
    {turnOnAlarm=false;}
  } else if (but1.pressed())
  {
    changeAlarmNumberH();
  } else if (but2.pressed())
  {
    changeAlarmNumberM();
  } else if (but3.pressed())
  {
    changeAlarmNumberS();
  } else if (but4.pressed())
  {
    turnOnLights();
  }
}
void keyPressed()
{
  if (keyCode==ENTER)
  {
    if (turnOnAlarm==false)
    {turnOnAlarm=true;}
    else if (turnOnAlarm==true)
    {turnOnAlarm=false;}
  } else if (keyCode==LEFT)
  { 
    changeAlarmNumberH();
  } else if (keyCode==DOWN)
  {
    changeAlarmNumberM();
  } else if (keyCode==RIGHT)
  {
    changeAlarmNumberS();
  } else if (keyCode==UP)
  {
    turnOnLights();
  }
}

void changeAlarmNumberH()
{
  alarmh++;
  if (alarmh>23)
  {
    alarmh = 0;
  }
}
void changeAlarmNumberM()
{
  alarmm++;
  if (alarmm>60)
  {
    alarmm = 0;
  }
}
void changeAlarmNumberS()
{
  alarms++;
  if (alarms>60)
  {
    alarms = 0;
  }
}
void turnOnLights()
{
  if (millis() <= 5000) {
    background(255);
  } else {
    background(0);
  }
}

void draw() //drawing the clock
{
  background(0);

  String nowHour = nf(hour(), 2); //limits amount of digits
  String nowMinute = nf(minute(), 2);
  String nowSecond = nf(second(), 2);
  String alarmHour = nf(alarmh, 2);
  String alarmMinute = nf(alarmm, 2);
  String alarmSecond = nf(alarms, 2);

  fill(0, 129, 255);
  text(nowHour + ":" + nowMinute + ":" + nowSecond, (width*0.5), (height*0.3)); //running clock
  fill(51, 153, 255);
  text(alarmHour + ":" + alarmMinute + ":" + alarmSecond, (width*0.5), (height*0.7)); //alarm

  but.display();
  but1.display();
  but2.display();
  but3.display();
  but4.display();

  if (turnOnAlarm==true)
  {
    if (hour()==alarmh)
    {
      if (minute()==alarmm)
      {
        if (second()==alarms)
        {
          player.play();
          {
          }
        }
      }
    }
  }
}

**Button class:**

class Button
{
  int x, y;
  int size;
  color buttoncolor;
  boolean pressed = false;  

  Button(int ix, int iy, int isize, color icolor)
  {
    x = ix;
    y = iy;
    size = isize;
    buttoncolor = icolor;
  }

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

  void display()
  {
    fill(buttoncolor);
    rect(x, y, size, size);
  }
}
Tagged:

Answers

  • This code:

    if (turnOnAlarm==false)
        {turnOnAlarm=true;}
        else if (turnOnAlarm==true)
        {turnOnAlarm=false;}
    

    can be simplified like this

    turnOnAlarm=!turnOnAlarm;

    I have modified your code to fix the problem with the light (it wasn't working) and the problem with the buttons. You have to use the button's field (size) instead of width and height.

    Noe I removed all references to minim so to be able to test your code. You will need to add it back yourself.

    Kf

    boolean pressedLeft=false; //setting up boolean to know if the mouse is being clicked or not
    boolean turnOnAlarm=false;
    color buttoncolor;
    Button but, but1, but2, but3, but4;
    int alarmh=00;
    int alarmm=00;
    int alarms=00;
    
    
    void setup()
    {
      size(600, 400);
    
      textAlign(CENTER, CENTER);
      textSize(72);
    
    
      buttoncolor = color(51, 153, 255);
      but = new Button(50, 330, 50, buttoncolor);
      but1 = new Button(160, 330, 50, buttoncolor);
      but2 = new Button(275, 330, 50, buttoncolor);
      but3 = new Button(390, 330, 50, buttoncolor);
      buttoncolor = color(255, 255, 51);
      but4 = new Button(50, 175, 50, buttoncolor);
    }
    
    void mouseReleased()
    {
      if (but.pressed())
      {
        turnOnAlarm=!turnOnAlarm;
      } else if (but1.pressed())
      {
        changeAlarmNumberH();
      } else if (but2.pressed())
      {
        changeAlarmNumberM();
      } else if (but3.pressed())
      {
        changeAlarmNumberS();
      } else if (but4.pressed())
      {
        turnOnLights();
      }
    }
    void keyPressed()
    {
      if (keyCode==ENTER)
      {
        if (turnOnAlarm==false)
        {
          turnOnAlarm=true;
        } else if (turnOnAlarm==true)
        {
          turnOnAlarm=false;
        }
      } else if (keyCode==LEFT)
      { 
        changeAlarmNumberH();
      } else if (keyCode==DOWN)
      {
        changeAlarmNumberM();
      } else if (keyCode==RIGHT)
      {
        changeAlarmNumberS();
      } else if (keyCode==UP)
      {
        turnOnLights();
      }
    }
    
    void changeAlarmNumberH()
    {
      alarmh++;
      if (alarmh>23)
      {
        alarmh = 0;
      }
    }
    void changeAlarmNumberM()
    {
      alarmm++;
      if (alarmm>60)
      {
        alarmm = 0;
      }
    }
    void changeAlarmNumberS()
    {
      alarms++;
      if (alarms>60)
      {
        alarms = 0;
      }
    }
    void turnOnLights()
    {
      targetTime=millis()+5000;
    }
    
    int targetTime=0;
    void draw() //drawing the clock
    {
      if (millis() <= targetTime) {
        background(255);
      } else {
        background(0);
      }
    
      String nowHour = nf(hour(), 2); //limits amount of digits
      String nowMinute = nf(minute(), 2);
      String nowSecond = nf(second(), 2);
      String alarmHour = nf(alarmh, 2);
      String alarmMinute = nf(alarmm, 2);
      String alarmSecond = nf(alarms, 2);
    
      fill(0, 129, 255);
      text(nowHour + ":" + nowMinute + ":" + nowSecond, (width*0.5), (height*0.3)); //running clock
      fill(51, 153, 255);
      text(alarmHour + ":" + alarmMinute + ":" + alarmSecond, (width*0.5), (height*0.7)); //alarm
    
      but.display();
      but1.display();
      but2.display();
      but3.display();
      but4.display();
    
      if (turnOnAlarm==true)
      {
        if (hour()==alarmh)
        {
          if (minute()==alarmm)
          {
            if (second()==alarms)
            {
    
              {
              }
            }
          }
        }
      }
    }
    
    
    
    class Button
    {
      int x, y;
      int size;
      color buttoncolor;
      boolean pressed = false;  
    
      Button(int ix, int iy, int isize, color icolor)
      {
        x = ix;
        y = iy;
        size = isize;
        buttoncolor = icolor;
      }
    
      boolean pressed()
      {
        println("Pressed " + frameCount);
        return (mouseX >= x && mouseX <= x+size &&
          mouseY >= y && mouseY <= y+size) ;
      }
    
      void display()
      {
        fill(buttoncolor);
        rect(x, y, size, size);
      }
    }
    
Sign In or Register to comment.