Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pelotaCS;
- import java.awt.*;
- import java.awt.event.*;
- import java.io.*;
- import java.net.*;
- import javax.swing.*;
- public class PelotaNetServer extends JFrame {
- private Socket conexion = null;
- private DataOutputStream out = null;
- private DataInputStream input = null;
- public PelotaNetServer(){
- super("Pelota - Server");
- this.setSize(500,400);
- this.setLocation(100,40);
- //this.setLocationRelativeTo(null);
- this.setDefaultCloseOperation(EXIT_ON_CLOSE);
- //instanzio un objeto para esperar la conexion de un client.
- ThreadConexion esperoConexion = new ThreadConexion(this);
- this.setVisible(true);
- }//fin construcor
- class ThreadConexion implements Runnable{
- private PelotaNetServer ventana;
- private Thread me;
- public ThreadConexion (PelotaNetServer ventana){
- this.ventana = ventana;
- me = new Thread (this);
- me.start();
- }
- public void run(){
- try{
- ServerSocket server = new ServerSocket(6789);
- ventana.setConexion(server.accept());
- server.close();
- }
- catch(Exception e){
- JOptionPane.showMessageDialog(null, e.toString());
- System.exit(-1);
- }
- } //fin metodo run thread
- }
- public void setConexion(Socket conexion){
- this.conexion = conexion;
- try{
- out = new DataOutputStream(conexion.getOutputStream());
- input = new DataInputStream(conexion.getInputStream());
- }
- catch(Exception e){
- JOptionPane.showMessageDialog(null, e.toString());
- System.exit(-1);
- }
- //inicio la animacion de un objeto de la clase PanelAnimacion
- PanelAnimacion pan = new PanelAnimacion(this,this.getSize());
- this.add(pan);
- }
- class PanelAnimacion extends JPanel implements ActionListener{
- private PelotaNetServer ventana ;
- private Dimension dimension;
- private Image bufferVirtuale;
- private Graphics offScreen;
- private Timer tim = null;
- private int xPelota = 0;
- private int yPelota = 0;
- private int diametroPelota = 40;
- private int movimiento = 3;
- private int timerDelay = 10;
- private boolean derecha,abajo,pelotaPresente,llegadaComunicada ;
- public PanelAnimacion(PelotaNetServer ventana,Dimension dimension){
- super();
- this.ventana = ventana;
- this.setSize(dimension);
- derecha = true;
- abajo = true;
- pelotaPresente = true;
- llegadaComunicada = false;
- tim = new Timer(timerDelay,this);
- tim.start();
- }
- public void update(Graphics g){
- paint(g);
- }
- public void paint(Graphics g){
- super.paint(g);
- bufferVirtuale = createImage(getWidth(),getHeight());
- offScreen = bufferVirtuale.getGraphics();
- Graphics2D screen =(Graphics2D)g;
- offScreen.setColor(new Color(254,138,22));
- if (pelotaPresente){
- offScreen.fillOval(xPelota,yPelota,diametroPelota,diametroPelota);
- }
- screen.drawImage(bufferVirtuale,0,0,this);
- offScreen.dispose();
- }
- public void actionPerformed(ActionEvent e){
- if (pelotaPresente){
- if (abajo){
- if (yPelota > (this.getHeight()-diametroPelota)){
- abajo = false;
- yPelota -= movimiento;
- }else{
- yPelota += movimiento;
- }
- }else{
- if (yPelota <=0){
- abajo = true;
- yPelota += movimiento;
- }else{
- yPelota -= movimiento;
- }
- }
- if (derecha){
- if((!llegadaComunicada)&&(xPelota>(this.getWidth()-diametroPelota))){
- try{
- ventana.getOutput().writeBoolean(abajo);
- ventana.getOutput().writeInt(yPelota);
- llegadaComunicada = true;
- }catch( Exception ecc){
- JOptionPane.showMessageDialog(null,ecc.toString());
- System.exit(-1);
- }
- }else{
- xPelota += movimiento;
- if (xPelota > this.getWidth()){
- pelotaPresente = false;
- llegadaComunicada = false;
- }
- }
- }else{
- if (xPelota <=0){
- derecha = true;
- xPelota += movimiento;
- }else{
- xPelota -= movimiento;
- }
- }
- }else{
- boolean direccion = false;
- int y=0;
- try{
- direccion = ventana.getInput().readBoolean();
- y = ventana.getInput().readInt();
- abajo = direccion;
- derecha = false;
- yPelota = y;
- xPelota = this.getWidth();
- pelotaPresente = true;
- }
- catch( Exception ecc){
- JOptionPane.showMessageDialog(null,ecc.toString());
- System.exit(-1);
- }
- }
- repaint();
- }
- }
- public DataInputStream getInput(){
- return input;
- }
- public DataOutputStream getOutput(){
- return out;
- }
- }
- //fin class PelotaNetServer
Add Comment
Please, Sign In to add comment