   import javax.microedition.lcdui.*;
   import javax.microedition.media.*;
   import javax.microedition.media.control.*;	
	import java.io.IOException;
	import java.io.InputStream;
   import javax.microedition.media.*; 


    public class ClassPantalla extends Canvas implements Runnable  {
      private Image IMG1, IMG0; 
      private Graphics IMGg1; 
      private int[] RGB;
      private int W,H;
		private int color;
		private Player player=null;
      private VideoControl videoControl=null;
    
       public ClassPantalla() { 
         try { 
			    player=Manager.createPlayer("capture://video");
             player.realize();
	   		 videoControl=(VideoControl)(player.getControl("VideoControl"));
				 if(videoControl!=null){
					videoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO,this);
					videoControl. setDisplayLocation(5,15);
				}
			}catch(IOException ioe){ }catch(MediaException me){}catch(SecurityException se){	}
		  
         IMG1 = Image.createImage(128,128);								

			if((player!=null)){
				try{ player.start();
					  videoControl.setVisible(true);
				}
				catch(MediaException me){}
		 	  	catch(SecurityException se){}
			}		
      }  
   
	 public void run() {	 
      takeSnapshot();
 	}

	
 	public void keyPressed(int keyCode){
	 if(getGameAction(keyCode)==FIRE){
		 Thread t=new Thread(this);
		 t.start();
		}
	 }

	public void takeSnapshot(){
			if(player!=null){
				try{
					byte[] pngImage=videoControl.getSnapshot(null);
					System.err.println("png length:"+pngImage.length);
					IMG1=Image.createImage(pngImage,0,pngImage.length);
					color=Procesar();
					repaint();
					int r=((0xff0000&color)>>16);
				   int g=((0x00ff00&color)>>8);
				   int b=(0x0000ff&color); 
					int col=-1;
					int umb=60;
					if((2*r-g-b)>umb) col=0;
					if((2*g-r-b)>umb/2) col=1; //ojo
 					if((2*b-r-g)>umb) col=2;
					String[] colores={"rojo.wav", "verde.wav", "azul.wav"};
					if(col>=0) try {
	   	 		 InputStream in=getClass().getResourceAsStream(colores[col]);
					 Player p= Manager.createPlayer(in,"audio/x-wav");
					 p.start();
	 				} catch(Exception e){}
 
					
					
				}catch(MediaException me){System.err.println("err take");	}
			}
	}
	
	
       public int PromMascara(int[] M) {
         int[][] A;
         int f=0,c=0, r=0, g=0,aux,n=0;
			int b=0; 
 		
         A=new int[W][H];
         for(int i=0;i<W*H;i++) {
				if(    ( (c-W/2)<30 && (W/2-c)<30 ) && ( (H/2-f)<30 && (f-H/2)<30 ) ){
				  aux=0xffffff&M[i];
				  A[c][f]=(int)(aux); 
				  r=r+((0xff0000&aux)>>16);
				  g=g+((0x00ff00&aux)>>8);
				  b=b+(0x0000ff&aux);  //OJO AGREGAR A TIPS!!!!! SIEMPRE UTILIZAR () con operador &
				  n++;
            }		
				else A[c][f]=0x0000ff;         
				c=c+1;
            if(c==W) {c=0;f=f+1;} 
         } 
         for(f=0;f<H;f++) for(c=0;c<W;c++) M[c+W*f]=A[c][f];
		
		   r=(int)(r/n);
			g=(int)(g/n);			
 		   b=(int)(b/n);  
	      return (int)((0xffffff)&((r<<16)|(g<<8)|(b)));
      }
   

       public int Procesar() {
         W=IMG1.getWidth();
         H=IMG1.getHeight();
         RGB=new int[W*H];
         IMG1.getRGB(RGB,0,W,0,0,W,H);
 
         return PromMascara(RGB); 
      } 
    
       protected void paint(Graphics g) {
			g.setColor(0x000000);//color fondo
         g.fillRect(0,0,getWidth(),getHeight()); 
         g.setColor(0x00ffff);//color letra		
			g.drawString("Color:",5,0,g.TOP|g.LEFT);
			g.setColor(color);//color fondo
         g.fillRect(5+30,0,5+125,14);
      }
   }