import ReceptorEventos;
import java.awt.*;

public class PanelEntrada extends Panel
{
  static int TXT_ANCHO = 120;
  static int TXT_ALTO = 25;
  int x,y;

  ReceptorEventos jefe;
  public TextField txtEntrada;
  Image fondo;
  
  public PanelEntrada( ReceptorEventos jefe, Image fondo )
  {
     super();
     this.jefe = jefe;
     this.fondo = fondo;
     creaControles();
     x = 0;
     y = 0;
  }

  public void creaControles()
  {
     txtEntrada = new TextField(35);

     setLayout(null);
     add(txtEntrada);
  }

  public void lineaTexto( Graphics g, String texto, boolean centrar, int sombra )
  {
     FontMetrics fm = g.getFontMetrics();
     if (centrar) x = (bounds().width - fm.stringWidth(texto))/2;
     y += fm.getHeight();
     if (sombra>0) {
        Color colorAnt = g.getColor();
        g.setColor( Color.darkGray );
        g.drawString(texto,x+sombra,y+sombra);
        g.setColor( colorAnt );
     }
     g.drawString(texto,x,y);
  }

  public boolean dibujaFondo( Graphics g )
  {
     if(fondo==null) return(true);
     int dx = bounds().width;
     int dy = bounds().height;
     int lx = fondo.getWidth(this);
     int ly = fondo.getHeight(this);
     if((lx==-1) || (ly==-1)) return(false);
     int y = 0;
     boolean resultado;
     do {
       x = 0;
       do {
         resultado = g.drawImage(fondo,x,y,this);
         x += lx;
       } while(x<dx);
       y += ly;
     } while(y<dy);
     return(resultado);
  }

  public boolean imageUpdate(Image  img, int  infoflags, int  x, int  y, int  width, int  height)
  {
     repaint();
     return(super.imageUpdate(img,infoflags,x,y,width,height));
  }

  public void paint( Graphics g )
  {
     super.paint(g);

     if(dibujaFondo(g)) {
       y = 0;
       g.setFont( new Font("TimesRoman", Font.BOLD, 48) );
       g.setColor( new Color(0,128,128) );
       lineaTexto(g,"ATENEO",true,3);

       g.setFont( new Font("TimesRoman", Font.BOLD, 14) );
       g.setColor( new Color(255,0,128) );
       lineaTexto(g,"Dpto. Informatica - UVA",false,1);

       g.setFont( new Font("Helvetica", Font.BOLD, 14) );
       g.setColor( Color.blue );
       lineaTexto(g,"",true,1);
       lineaTexto(g,"Un centro de debate y discusion donde Ud. puede",true,1);
       lineaTexto(g,"entrar a cualquiera de las tertulias existentes",true,1);
       lineaTexto(g,"o proponer la creacion de otras nuevas.",true,1);
       lineaTexto(g,"",true,1);
       lineaTexto(g,"Introduzca ahora el nombre por el que quiere ser",true,1);
       lineaTexto(g,"conocido. Al pulsar [Enter] aparecera el panel",true,1);
       lineaTexto(g,"de control, donde podra elegir la tertulia en la",true,1);
       lineaTexto(g,"que quiere participar.",true,1);
     }

     txtEntrada.reshape(
       (bounds().width-TXT_ANCHO)/2,bounds().height-2*TXT_ALTO,
       TXT_ANCHO,TXT_ALTO
     );
  }

  public boolean action( Event evt, Object what )
  {
     if(evt.target==txtEntrada) {
       String args[] = {txtEntrada.getText()};
       if(args[0].length()>0) {
         jefe.nuevoEvento(this,ReceptorEventos.INT_ALTA,args);
         return(true);
       }
     }

     return( super.action(evt,what) );
  }
}
