Assignment 2

Map the key response on screen using processing


<back

a2.jpg
Screen shot of the application
//Assignment 2
void setup()
{
  size(450,150);
  background(#ffffff);
  strokeWeight(2);
  fill(#000000);
  rect(50,50,50,50);
  rect(150,50,50,50);
  rect(250,50,50,50);
  rect(350,50,50,50);
}

void draw()
{ 

  if(keyPressed)
  {

    switch(key)
    {
      case '1': glowSquare(1);
      break;
      case '2': glowSquare(2);
      break;
      case '3': glowSquare(3);
      break;
      case '4': glowSquare(4);
      break;
      default : exceptionHandler();
    }
  }
  if(!keyPressed)
  {
    setup();
  }

}

void glowSquare(int _key)
{

  int xCod;

  for (int i=1; i<5; i++)
  {  
    if(_key == i)
    {
      fill(#FF0000);
      xCod = (i*100)-50;

    }
    else
    {
      fill(#000000);
      xCod = (i*100)-50;
    }

    rect(xCod,50,50,50);
  }
}

void exceptionHandler()
{
  print("Error: Please press key between 1 to 4");
  setup();
}

<back

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License