Loading...
Logo
Processing Forum
Can anyone tell me whether it is possible/how to create a shadow around the circumference or even entirety of a PImage in Processing.org? I want to make it look as if my buttons are being pressed (the buttons being the PImages..). If anyone has any other ideas how this effect could be achieved I'd greatly appreciate it,

Thanks

Replies(1)

Your buttons, your style. Here's some sample buttons though. Personally I like the middle button best. It's simply and easy.

Copy code
  1. PImage myButton;
  2. int num_buttons = 5;
  3. Button[] buttons;

  4. class Button{
  5.   int x,y,w,h,j;
  6.   Button(int i){
  7.     j = i;
  8.     x = 20 + i * (myButton.width+20);
  9.     y = (height-myButton.width)/2;
  10.     w = myButton.width;
  11.     h = myButton.height;
  12.   }
  13.   boolean isPressed(){
  14.     return( mousePressed&&mouseX>x&&mouseX<x+w&&mouseY>y&&mouseY<y+h );
  15.   }
  16.   void draw(){
  17.     switch( j ){
  18.       case 0:
  19.         pushStyle();
  20.         if( isPressed() ) tint(128);
  21.         image( myButton, x, y );
  22.         popStyle();
  23.         break;
  24.         
  25.       case 1:
  26.         pushStyle();
  27.         image( myButton, x, y );
  28.         if( isPressed() ){
  29.           noStroke();
  30.           fill( random(255),random(255),random(255), 128 );
  31.           rect( x-10, y-10, w+20, h+20 );
  32.         }
  33.         popStyle();
  34.         break;
  35.         
  36.       case 2:
  37.         pushStyle();
  38.           noStroke();
  39.           fill( 128 );
  40.         if( isPressed() ){
  41.           rect( x+2, y+2, w, h );
  42.         } else {
  43.           rect( x+6, y+6, w, h );
  44.         }
  45.         image( myButton, x, y );
  46.         popStyle();
  47.         break;
  48.         
  49.       case 3:
  50.         pushStyle();
  51.         if( !isPressed() ){
  52.         image( myButton, x, y );
  53.         } else {
  54.           pushMatrix();
  55.           translate(x+w/2,y+h/2);
  56.           rotate(map(millis()%5000,0,5000,0,TWO_PI));
  57.           image( myButton, -w/2, -h/2 );
  58.           popMatrix();
  59.         }
  60.         popStyle();
  61.         break;
  62.         
  63.       case 4:
  64.         pushStyle();
  65.         if( isPressed() ){ fill(255,0,0); stroke(255,255,0); rect(x-10,y-10,w+20,h+20); }
  66.         image( myButton, x, y );
  67.         popStyle();
  68.         break;
  69.         
  70.         
  71.     }    
  72.   }
  73. }  

  74. void setup(){
  75.   // I don't have a button image so I'm gonna make one!
  76.   PGraphics pg = createGraphics( 50, 50, P2D );
  77.   pg.beginDraw();
  78.   pg.background(200);
  79.   pg.fill(200,0,0);
  80.   pg.ellipse(pg.width/2,pg.height/2,pg.width-8,pg.height-8);
  81.   pg.noFill();
  82.   pg.rect(0,0,pg.width-1,pg.height-1);
  83.   pg.endDraw();
  84.   myButton = pg.get(); 
  85.   size( 20 + num_buttons * (myButton.width + 20 ), 80 + myButton.height );
  86.   buttons = new Button[num_buttons];
  87.   for( int i=0; i< num_buttons; i++ ){
  88.     buttons[i] = new Button(i);
  89.   } 
  90. }

  91. void draw(){
  92.   background(196);
  93.   for( int i=0; i< num_buttons; i++ ){
  94.     buttons[i].draw();
  95.   }   
  96. }
Code is easily extensible for anyone else who wants to add a button style.