Guest User

Untitled

a guest
May 28th, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.44 KB | None | 0 0
  1. package pelotaCS;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.io.*;
  6. import java.net.*;
  7. import javax.swing.*;
  8.  
  9. public class PelotaNetServer extends JFrame {
  10.     private Socket conexion = null;
  11.     private DataOutputStream out = null;
  12.     private DataInputStream input = null;
  13.     public PelotaNetServer(){
  14.         super("Pelota - Server");
  15.         this.setSize(500,400);
  16.         this.setLocation(100,40);
  17.         //this.setLocationRelativeTo(null);
  18.         this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  19.         //instanzio un objeto para esperar la conexion de un client.
  20.         ThreadConexion esperoConexion = new ThreadConexion(this);
  21.         this.setVisible(true);
  22.     }//fin construcor
  23.    
  24. class ThreadConexion implements Runnable{
  25.     private PelotaNetServer ventana;
  26.     private Thread me;
  27.     public ThreadConexion (PelotaNetServer ventana){
  28.         this.ventana = ventana;
  29.         me = new Thread (this);
  30.         me.start();
  31.     }
  32.  
  33.     public void run(){
  34.         try{
  35.             ServerSocket server = new ServerSocket(6789);
  36.             ventana.setConexion(server.accept());
  37.             server.close();
  38.         }
  39.         catch(Exception e){
  40.             JOptionPane.showMessageDialog(null, e.toString());
  41.             System.exit(-1);
  42.         }
  43.     } //fin metodo run thread
  44.    
  45. }
  46. public void setConexion(Socket conexion){
  47.     this.conexion = conexion;
  48.     try{
  49.         out = new DataOutputStream(conexion.getOutputStream());
  50.         input = new DataInputStream(conexion.getInputStream());
  51.         }
  52.     catch(Exception e){
  53.         JOptionPane.showMessageDialog(null, e.toString());
  54.         System.exit(-1);
  55.     }
  56.     //inicio la animacion de un objeto de la clase PanelAnimacion
  57.     PanelAnimacion pan = new PanelAnimacion(this,this.getSize());
  58.     this.add(pan);
  59. }  
  60.  
  61. class PanelAnimacion extends JPanel implements ActionListener{
  62.     private PelotaNetServer ventana ;
  63.     private Dimension dimension;
  64.     private Image bufferVirtuale;
  65.     private Graphics offScreen;
  66.     private Timer tim = null;
  67.     private int xPelota = 0;
  68.     private int yPelota = 0;
  69.     private int diametroPelota = 40;
  70.     private int movimiento = 3;
  71.     private int timerDelay = 10;
  72.     private boolean derecha,abajo,pelotaPresente,llegadaComunicada ;
  73.  
  74.     public PanelAnimacion(PelotaNetServer ventana,Dimension dimension){    
  75.         super();
  76.         this.ventana = ventana;
  77.         this.setSize(dimension);
  78.         derecha = true;
  79.         abajo = true;
  80.         pelotaPresente = true;
  81.         llegadaComunicada = false;
  82.         tim = new Timer(timerDelay,this);
  83.         tim.start();
  84.     }
  85.     public void update(Graphics g){
  86.         paint(g);
  87.     }
  88.     public void paint(Graphics g){
  89.         super.paint(g);
  90.         bufferVirtuale = createImage(getWidth(),getHeight());
  91.         offScreen = bufferVirtuale.getGraphics();
  92.         Graphics2D screen =(Graphics2D)g;
  93.         offScreen.setColor(new Color(254,138,22));
  94.         if (pelotaPresente){
  95.           offScreen.fillOval(xPelota,yPelota,diametroPelota,diametroPelota);
  96.         }
  97.         screen.drawImage(bufferVirtuale,0,0,this);
  98.         offScreen.dispose();
  99.     }
  100.    
  101.     public void actionPerformed(ActionEvent e){
  102.         if (pelotaPresente){
  103.             if (abajo){
  104.                 if (yPelota > (this.getHeight()-diametroPelota)){
  105.                     abajo = false;
  106.                     yPelota -= movimiento;
  107.                 }else{
  108.                     yPelota += movimiento;
  109.                 }
  110.             }else{        
  111.                 if (yPelota <=0){
  112.                     abajo = true;
  113.                     yPelota += movimiento;    
  114.                 }else{
  115.                     yPelota -= movimiento;
  116.                 }
  117.             }
  118.         if (derecha){
  119.           if((!llegadaComunicada)&&(xPelota>(this.getWidth()-diametroPelota))){
  120.                     try{
  121.                         ventana.getOutput().writeBoolean(abajo);
  122.                         ventana.getOutput().writeInt(yPelota);
  123.                         llegadaComunicada = true;
  124.                     }catch( Exception ecc){
  125.                     JOptionPane.showMessageDialog(null,ecc.toString());
  126.                     System.exit(-1);
  127.                 }
  128.             }else{
  129.                 xPelota += movimiento;
  130.                 if (xPelota > this.getWidth()){
  131.                     pelotaPresente = false;
  132.                     llegadaComunicada = false;
  133.                     }
  134.                 }
  135.             }else{
  136.                 if (xPelota <=0){
  137.                     derecha = true;
  138.                     xPelota += movimiento;
  139.                 }else{
  140.                     xPelota -= movimiento;
  141.                 }
  142.             }
  143.         }else{
  144.             boolean direccion = false;
  145.             int y=0;
  146.             try{
  147.                 direccion = ventana.getInput().readBoolean();
  148.                 y = ventana.getInput().readInt();
  149.                 abajo = direccion;
  150.                 derecha = false;
  151.                 yPelota = y;  
  152.                 xPelota = this.getWidth();
  153.                 pelotaPresente = true;
  154.                 }
  155.             catch( Exception ecc){
  156.                 JOptionPane.showMessageDialog(null,ecc.toString());
  157.                     System.exit(-1);
  158.                 }
  159.             }
  160.         repaint();
  161.         }
  162.     }
  163.  
  164.  
  165. public DataInputStream getInput(){
  166.     return input;
  167.     }
  168. public DataOutputStream getOutput(){
  169.     return out;
  170.     }
  171. }
  172. //fin class PelotaNetServer
Add Comment
Please, Sign In to add comment