Ejemplo Mensaje con Alert en JavaScript


Domingo, 2 de Noviembre de 2008
HTML:
  1. <title>Ejemplo Mensaje en JavaScript</title>
  2.  
  3. <script language="javascript">
  4.  
  5. function mess( ) {
  6. var mensaje = formulario_mensaje.mensajetxt.value;
  7.  
  8.     alert(mensaje);
  9.  
  10. }
  11.  
  12. </script>
  13. </head>
  14.  
  15.  
  16.  
  17. <form name="formulario_mensaje">
  18.   <p><b>Introduzca su mensaje</b></p>
  19.   <p>
  20.     <input type="text" name="mensajetxt">
  21.     <br>
  22.     <input value="ok" type="button" onClick="mess();">
  23.  
  24.  
  25.     </p>
  26. </form>
  27. </body>
  28.  
  29. </html>

mini Paint Applet


Sábado, 25 de Octubre de 2008

JAVA:
  1. import java.awt.*;
  2. import java.applet.*;
  3.  
  4. public class MyApp extends Applet
  5. {
  6.     BorderLayout esquemaOrdena;
  7.     Panel panHerr, panCol;
  8.     Button btnBlanco, btnNegro, btnGris, btnRojo, btnAzul, btnRosa, btnNaranja, btnVerde;
  9.     Button btnLapiz, btnAerografo, btnOvalo, btnOvaloRell, btnRec, btnRecRell, btnLinea, btnBorrar;
  10.     Label lblEste, lblOeste, lblCol;
  11.    
  12.     Graphics g = getGraphics();
  13.    
  14.     Point inicioLinea = new Point(0,0);
  15.            
  16.     String strHerr = "Lapiz", strCol = "Negro";
  17.     int diaCirculo = 5, x1, x2,y1, y2, x3, x4, y3, y4, xx, yy;
  18.    
  19.     public void init()
  20.     {
  21.         //--- METODO DE ORDENACION ---//
  22.         esquemaOrdena = new BorderLayout(0,0);
  23.         setLayout(esquemaOrdena);
  24.        
  25.         lblEste = new Label();
  26.         lblOeste = new Label();
  27.         lblEste.setBackground(Color.gray);
  28.         lblOeste.setBackground(Color.gray);
  29.        
  30.         add("West", lblOeste);
  31.         add("East", lblEste);
  32.        
  33.         /*--- PANEL ---*/
  34.         panHerr = new Panel();
  35.         panCol = new Panel();
  36.         panHerr.setBackground(Color.gray);
  37.         panCol.setBackground(Color.gray);
  38.        
  39.         add("North", panHerr);
  40.         add("South", panCol);
  41.  
  42.         /*--- HERRAMIENTAS ---*/
  43.         setForeground(Color.blue);
  44.         btnLapiz = new Button("Lapiz");
  45.         btnAerografo = new Button("Aerografo");
  46.         btnLinea = new Button("Linea");
  47.         btnOvalo = new Button("Ovalo");
  48.         btnOvaloRell = new Button("Ovalo Relleno");
  49.         btnRec = new Button("Rectangulo");
  50.         btnRecRell = new Button("Rectangulo Relleno");
  51.         btnBorrar = new Button("Borrar");
  52.        
  53.         panHerr.add(btnLapiz);
  54.         panHerr.add(btnAerografo);
  55.         panHerr.add(btnLinea);
  56.         panHerr.add(btnOvalo);
  57.         panHerr.add(btnOvaloRell);
  58.         panHerr.add(btnRec);
  59.         panHerr.add(btnRecRell);
  60.         panHerr.add(btnBorrar);
  61.        
  62.         /*--- COLORES ---*/
  63.        
  64.         lblCol =new Label("COLORES");
  65.         lblCol.setForeground(Color.black);
  66.         btnBlanco = new Button(" B ");
  67.         btnBlanco.setBackground(Color.white);
  68.         btnBlanco.setForeground(Color.white);
  69.         btnNegro = new Button(" N ");
  70.         btnNegro.setBackground(Color.black);
  71.         btnNegro.setForeground(Color.black);
  72.         btnGris = new Button(" G ");
  73.         btnGris.setBackground(Color.gray);
  74.         btnGris.setForeground(Color.gray);
  75.         btnRojo = new Button(" R ");
  76.         btnRojo.setBackground(Color.red);
  77.         btnRojo.setForeground(Color.red);
  78.         btnAzul = new Button(" A ");
  79.         btnAzul.setBackground(Color.blue);
  80.         btnAzul.setForeground(Color.blue);
  81.         btnRosa = new Button(" P ");
  82.         btnRosa.setBackground(Color.pink);
  83.         btnRosa.setForeground(Color.pink);
  84.         btnNaranja = new Button(" O ");
  85.         btnNaranja.setBackground(Color.orange);
  86.         btnNaranja.setForeground(Color.orange);
  87.         btnVerde = new Button(" V ");
  88.         btnVerde.setBackground(Color.green);
  89.         btnVerde.setForeground(Color.green);
  90.        
  91.         panCol.add(lblCol);
  92.         panCol.add(btnBlanco);
  93.         panCol.add(btnNegro);
  94.         panCol.add(btnGris);
  95.         panCol.add(btnRojo);
  96.         panCol.add(btnAzul);
  97.         panCol.add(btnRosa);
  98.         panCol.add(btnNaranja);
  99.         panCol.add(btnVerde);
  100.        
  101.    
  102.     }
  103.    
  104.     public boolean action( Event evt, Object obj)
  105.     {
  106.         if(evt.target instanceof Button)
  107.         {
  108.             /*--- HERRAMIENTAS ---*/
  109.             if("Lapiz".equals(obj))
  110.             {
  111.                 strHerr = "Lapiz";
  112.             }
  113.             if("Aerografo".equals(obj))
  114.             {
  115.                 strHerr = "Aerografo";
  116.             }
  117.             if("Linea".equals(obj))
  118.             {
  119.                 strHerr = "Linea";
  120.             }
  121.             if("Ovalo".equals(obj))
  122.             {
  123.                 strHerr = "Ovalo";
  124.             }
  125.             if("Ovalo Relleno".equals(obj))
  126.             {
  127.                 strHerr = "Ovalo Relleno";
  128.             }
  129.             if("Rectangulo".equals(obj))
  130.             {
  131.                 strHerr = "Rectangulo";
  132.             }
  133.             if("Rectangulo Relleno".equals(obj))
  134.             {
  135.                 strHerr = "Rectangulo Relleno";
  136.             }
  137.             if("Borrar".equals(obj))
  138.             {
  139.                 repaint();
  140.             }
  141.            
  142.             /*--- COLORES ---*/
  143.             if(" B ".equals(obj))
  144.             {
  145.                 strCol = "Blanco";
  146.             }
  147.             if(" N ".equals(obj))
  148.             {
  149.                 strCol = "Negro";
  150.             }
  151.             if(" G ".equals(obj))
  152.             {
  153.                 strCol = "Gris";
  154.             }
  155.             if(" R ".equals(obj))
  156.             {
  157.                 strCol = "Rojo";
  158.             }
  159.             if(" A ".equals(obj))
  160.             {
  161.                 strCol = "Azul";
  162.             }
  163.             if(" P ".equals(obj))
  164.             {
  165.                 strCol = "Rosa";
  166.             }
  167.             if(" O ".equals(obj))
  168.             {
  169.                 strCol = "Naranja";
  170.             }
  171.             if(" V ".equals(obj))
  172.             {
  173.                 strCol = "Verde";
  174.             }
  175.         }
  176.         return true;
  177.     }
  178.  
  179.     /*---- EVENTO PRESIONAR MOUSE ----*/
  180.     public boolean mouseDown(Event e, int x, int y)
  181.     {
  182.         x1 = x;
  183.         y1 = y;
  184.        
  185.         inicioLinea.move(x,y);
  186.         return true;
  187.     }
  188.  
  189.     /*---- EVENTO ARRASTRAR MOUSE ----*/
  190.     public boolean mouseDrag(Event e, int x, int y)
  191.     {
  192.         g = getGraphics();
  193.        
  194.         if(strCol == "Blanco")
  195.         {
  196.             g.setColor(Color.white);
  197.         }
  198.         if(strCol == "Negro")
  199.         {
  200.             g.setColor(Color.black);
  201.         }
  202.         if(strCol == "Gris")
  203.         {
  204.             g.setColor(Color.gray);
  205.         }
  206.         if(strCol == "Rojo")
  207.         {
  208.             g.setColor(Color.red);
  209.         }
  210.         if(strCol == "Azul")
  211.         {
  212.             g.setColor(Color.blue);
  213.         }
  214.         if(strCol == "Rosa")
  215.         {
  216.             g.setColor(Color.pink);
  217.         }
  218.         if(strCol == "Naranja")
  219.         {
  220.             g.setColor(Color.orange);
  221.         }
  222.         if(strCol == "Verde")
  223.         {
  224.             g.setColor(Color.green);
  225.         }
  226.         //--- CONDICION HERRAMIENTA ---*/
  227.         if(strHerr == "Lapiz")
  228.         {
  229.             g.drawLine(inicioLinea.x, inicioLinea.y, x, y);
  230.         }
  231.                
  232.         if(strHerr == "Aerografo")
  233.         {
  234.             g.fillOval(x - (diaCirculo/2), y - (diaCirculo/2), diaCirculo, diaCirculo);
  235.         }
  236.        
  237.         inicioLinea.move(x,y);
  238.         return true;
  239.     }
  240.    
  241.     /*---- EVENTO LEVANTAR MOUSE ----*/
  242.     public boolean mouseUp(Event e, int x, int y)
  243.     {
  244.         x2 = x;
  245.         y2 = y;
  246.         x3 = x1/2;
  247.         x4 = (x2-x1)/2;
  248.         y3 = y2/2;
  249.         y4 = (y2-y1)/2;
  250.        
  251.         /* --- CONDICIONES DE FIGURAS ---*/
  252.         if(strHerr == "Linea")
  253.         {
  254.             g.drawLine(x1, y1, x2, y2);
  255.         }
  256.         if(x2<x1 && y2<y1)
  257.         {
  258.             xx = x1;
  259.             x1 = x2;
  260.             x2 = xx;
  261.             yy = y1;
  262.             y1 = y2;
  263.             y2 = yy;
  264.         }
  265.         if(x2<x1 && y2>y1)
  266.         {
  267.             xx = x1;
  268.             x1 = x2;
  269.             x2 = xx;
  270.         }
  271.         if(x2>x1 && y2<y1)
  272.         {
  273.             yy = y1;
  274.             y1 = y2;
  275.             y2 = yy;
  276.         }
  277.            
  278.         if(strHerr == "Ovalo")
  279.         {
  280.             g.drawOval(x1, y1, x2-x1, y2-y1);
  281.         }
  282.        
  283.         if(strHerr == "Ovalo Relleno")
  284.         {
  285.             g.fillOval(x1, y1, x2-x1, y2-y1);
  286.         }
  287.        
  288.         if(strHerr == "Rectangulo")
  289.         {
  290.             g.drawRect(x1, y1, x2-x1, y2-y1);
  291.             g.drawRect(x1, y1, x3, y4);
  292.         }
  293.        
  294.         if(strHerr == "Rectangulo Relleno")
  295.         {
  296.             g.fillRect(x1, y1, x2-x1, y2-y1);
  297.         }
  298.         return true;
  299.     }
  300.    
  301.     public static void main (String args[]) {
  302.        
  303.         new MyApp();
  304.        
  305.     }
  306.    
  307.    
  308. } // fin del metodo

Para ejecutar el Applet solo crea este documento HTML

HTML:
  1.     <HEAD>
  2.         <TITLE>MyApplet Example1</TITLE>
  3.     </HEAD>
  4.     <BODY>
  5.         <H1>MyApplet</H1>
  6.         <HR>
  7.         <P>
  8.             <APPLET CODE="MyApp.class" WIDTH="300" HEIGHT="300">
  9.             </APPLET>
  10.         </P>
  11.         <HR>
  12.     </BODY>
  13. </HTML>

Uso de Sockets UDP en JAVA


Jueves, 23 de Octubre de 2008

La programación de comunicaciones con UDP se basa en las clases DatagramPacket y DatagramSocket que permiten crear un socket UDP y, posteriormente, enviar y recibir datagramas UDP sobre él.

Recuerde que para poder recibir a través de un socket UDP se necesita que el socket escuche en un puerto. Para ello se crea un socket UDP mediante la orden:

DatagramSocket ds = new DatagramSocket(12345);

Con esto cualquier datagrama que llegue al puer