Foro de elhacker.net

Programación => Java => Mensaje iniciado por: delirio en 24 Agosto 2011, 05:15 am



Título: Ayuda con sockets
Publicado por: delirio en 24 Agosto 2011, 05:15 am
Alguien podria ayudarme a como implementar sockets a este codigo, he intentado pero no me sale.....necesito implementar el cliente y servidor....h


Código
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.awt.image.*;
  4. import java.util.*;
  5. import java.io.*;
  6. import javax.imageio.*;
  7. import javax.swing.*;
  8.  
  9.  
  10. public class Main extends JFrame
  11. {
  12.  
  13.    // Ancho y alto de la ventana
  14.    public static final int ANCHO = 600;
  15.    public static final int ALTO  = 350;
  16.  
  17.    // Constantes
  18.    public final int PLAYER_VS_PLAYER = 0;
  19.    public final int PLAYER_VS_COMP   = 1;
  20.  
  21.    // Controlador de pantallas
  22.    private Pantallas pantallas;
  23.  
  24.    // Las paletas
  25.    private Paleta paleta1;
  26.    private Paleta paleta2;
  27.  
  28.    // La pelota
  29.    private Pelota pelota;
  30.  
  31.    // El marcador
  32.    private Marcador marcador;
  33.  
  34.  
  35.    /***************************************************************************
  36.      ***************************     INICIALIZAR     ***************************
  37.      ***************************************************************************/
  38.  
  39.    public static void main(String[] args) { new Main(); }
  40.  
  41.    public Main()
  42.    {
  43.        // Comprobamos que las imágenes estén en su sitio
  44.        Archivos.comprobarRutaImagenes();
  45.  
  46.        // Propiedades de la ventana
  47.        this.setTitle("Pong!");
  48.        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  49.        this.setSize(ANCHO, ALTO+20); // +20 por el borde de la ventana
  50.        this.setLocation(150,150);
  51.        this.setResizable(false);
  52.  
  53.        // Listeners del teclado
  54.        this.addKeyListener(new KeyAdapter(){
  55.            public void keyPressed(KeyEvent evt) {
  56.                formKeyPressed(evt);
  57.            }
  58.            public void keyReleased(KeyEvent evt) {
  59.                formKeyReleased(evt);
  60.            }
  61.        });
  62.  
  63.        // Creamos el controlador de pantallas
  64.        pantallas = new Pantallas();
  65.  
  66.        this.setVisible(true);
  67.    }
  68.  
  69.    private void iniGame(int tipoJuego)
  70.    {
  71.        pantallas.setFalse();
  72.        pantallas.pantallaJuego = true;
  73.  
  74.        if (tipoJuego == PLAYER_VS_PLAYER)
  75.        {
  76.            paleta1 = new Paleta(Paleta.MANEJA_PLAYER, Paleta.PLAYER1);
  77.            paleta2 = new Paleta(Paleta.MANEJA_PLAYER, Paleta.PLAYER2);
  78.        } else {
  79.            paleta1 = new Paleta(Paleta.MANEJA_PLAYER, Paleta.PLAYER1);
  80.            paleta2 = new Paleta(Paleta.MANEJA_MAQUINA, Paleta.PLAYER2);
  81.        }
  82.  
  83.        pelota = new Pelota(ANCHO/2-Pelota.anchoImagen/2, ALTO/2-Pelota.altoImagen/2);
  84.  
  85.        marcador = new Marcador();
  86.    }
  87.  
  88.  
  89.    /***************************************************************************
  90.      ***************************     LEER TECLADO    ***************************
  91.      ***************************************************************************/
  92.  
  93.    public void formKeyPressed(KeyEvent evt)
  94.    {
  95.            // Selección Player vs Player o Player vs Computer
  96.            if (evt.getKeyCode() == KeyEvent.VK_1 && pantallas.pantallaPrincipal == true)
  97.                    this.iniGame(PLAYER_VS_PLAYER);
  98.  
  99.            if (evt.getKeyCode() == KeyEvent.VK_2 && pantallas.pantallaPrincipal == true)
  100.                    this.iniGame(PLAYER_VS_COMP);
  101.  
  102.            // Mover paletas
  103.            if (pantallas.pantallaJuego == true)
  104.            {
  105.                paleta1.keyPressed(evt);
  106.                paleta2.keyPressed(evt);
  107.            }
  108.  
  109.    }
  110.  
  111.    public void formKeyReleased(KeyEvent evt)
  112.    {
  113.        // Mover paletas
  114.        if (pantallas.pantallaJuego == true)
  115.        {
  116.            paleta1.keyReleased(evt);
  117.            paleta2.keyReleased(evt);
  118.        }
  119.    }
  120.  
  121.  
  122.    /***************************************************************************
  123.      ***************************       DIBUJAR       ***************************
  124.      ***************************************************************************/
  125.  
  126.    public void dobleBuffer(Graphics2D g2)
  127.    {
  128.  
  129.        /***********************
  130.          ******  CHOCAR   ******
  131.          ***********************/
  132.        if (pantallas.pantallaJuego == true)
  133.        {
  134.            // Pelota <=> Paleta 1
  135.            if (paleta1.getRectangulo().intersects(pelota.getRectangulo())==true)
  136.            {
  137.                if (paleta1.abajo==true)
  138.                    pelota.setDireccion(Pelota.PALETA1, Pelota.DIR_PALETA_ABAJO);
  139.                if (paleta1.arriba==true)
  140.                    pelota.setDireccion(Pelota.PALETA1, Pelota.DIR_PALETA_ARRIBA);
  141.                if (paleta1.abajo==false && paleta1.arriba==false)
  142.                    pelota.setDireccion();
  143.            }
  144.  
  145.            // Pelota <=> Paleta 2
  146.            if (paleta2.getRectangulo().intersects(pelota.getRectangulo())==true)
  147.            {
  148.                if (paleta2.abajo==true)
  149.                    pelota.setDireccion(Pelota.PALETA2, Pelota.DIR_PALETA_ABAJO);
  150.                else if (paleta2.arriba==true)
  151.                    pelota.setDireccion(Pelota.PALETA2, Pelota.DIR_PALETA_ARRIBA);
  152.                else if (paleta2.abajo==false && paleta2.arriba==false)
  153.                    pelota.setDireccion();
  154.            }
  155.        }
  156.  
  157.        /***********************
  158.          ******   MOVER   ******
  159.          ***********************/  
  160.        if (pantallas.pantallaJuego == true)
  161.        {
  162.            paleta1.mover(pelota.posY);
  163.            paleta2.mover(pelota.posY);
  164.  
  165.            // Comporbamos los goles
  166.            int n = pelota.mover();
  167.  
  168.            if (n!=Pelota.NO_GOL)
  169.            {
  170.                marcador.addPunto(n);
  171.                pelota.setCentrada();      
  172.                try{ Thread.sleep(1000); } catch(Exception e) {}
  173.            }
  174.        }
  175.  
  176.        /***********************
  177.          ******  DIBUJAR  ******
  178.          ***********************/        
  179.        pantallas.dibujar(g2);
  180.        if (pantallas.pantallaJuego == true)
  181.        {
  182.            paleta1.dibujar(g2);
  183.            paleta2.dibujar(g2);
  184.            pelota.dibujar(g2);
  185.            marcador.dibujar(g2);
  186.        }
  187.  
  188.        try{ Thread.sleep(10); }catch(Exception e) {}
  189.        repaint();
  190.    }
  191.  
  192.    public void paint(Graphics g)
  193.    {
  194.        Graphics2D g2 = (Graphics2D) g;
  195.  
  196.        Image mImage = createImage(ANCHO, ALTO);
  197.        dobleBuffer((Graphics2D)mImage.getGraphics());
  198.  
  199.        g2.drawImage(mImage, 0, 20, this);
  200.    }
  201.  
  202. }
  203.