Advertisement
LegendDario

PallaNetClient

Jan 23rd, 2019
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.21 KB | None | 0 0
  1. -PallaNetClient.java-
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.io.*;
  5. import java.net.*;
  6. import javax.swing.*;
  7. public class PallaNetClient extends JFrame {
  8.    
  9.     private Socket connessione = null;
  10.     private DataOutputStream out = null;
  11.     private DataInputStream input = null;
  12.     public PallaNetClient(){
  13.         super("PallaNet - Client");
  14.         this.setSize(500,400);
  15.         this.setLocationRelativeTo(null);
  16.         this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  17.         connetti();
  18.         this.setVisible(true);
  19.     }
  20.    
  21.     public void connetti(){
  22.         try{
  23.            
  24.             connessione = new Socket("localhost",6789);
  25.            
  26.             out = new DataOutputStream(connessione.getOutputStream());
  27.             input = new DataInputStream(connessione.getInputStream());
  28.         }catch(Exception e){
  29.             JOptionPane.showMessageDialog(null,e.toString());
  30.             System.exit(-1);
  31.         }
  32.        
  33.         Container contenitore = this.getContentPane();
  34.         PannelloClient pan = new PannelloClient(this,contenitore.getSize());
  35.         contenitore.add(pan);
  36.     }
  37.    
  38.     public DataInputStream getInput(){
  39.         return input;
  40.     }
  41.    
  42.     public DataOutputStream getOutput(){
  43.         return out;
  44.     }
  45. }
  46. -PannelloClient.java-
  47. import java.awt.*;
  48. import java.awt.event.*;
  49. import java.io.*;
  50. import java.net.*;
  51. import javax.swing.*;
  52. class PannelloClient extends JPanel implements ActionListener {
  53.     private PallaNetClient finestra;
  54.     private Dimension dimensioni;
  55.     private Image bufferVirtuale;
  56.     private Graphics offScreen;
  57.     private Timer tim = null;
  58.     private int xPallina = 0;
  59.     private int yPallina = 0;
  60.     private int diametroPallina = 40;
  61.     private int spostamento = 3;
  62.     private int timerDelay = 10;
  63.     private boolean destra,basso,pallinaPresente,arrivoComunicato;
  64.    
  65.     public PannelloClient(PallaNetClient finestra, Dimension dimensioni){
  66.         super();
  67.         this.finestra = finestra;
  68.         this.setSize(dimensioni);
  69.         destra = true;
  70.         basso = true;
  71.         pallinaPresente = false;
  72.         arrivoComunicato = false;
  73.         tim = new Timer(timerDelay,this);
  74.         tim.start();
  75.     }
  76.    
  77.     public void update(Graphics g){
  78.         paint(g);
  79.     }
  80.    
  81.     public void paint(Graphics g){
  82.         super.paint(g);
  83.        
  84.         bufferVirtuale = createImage(getWidth(),getHeight());
  85.         offScreen = bufferVirtuale.getGraphics();
  86.         Graphics2D screen = (Graphics2D) g;
  87.         offScreen.setColor(new Color(254,138,22));
  88.         if(pallinaPresente){
  89.             offScreen.fillOval(xPallina,yPallina,diametroPallina,diametroPallina);
  90.         }
  91.        
  92.         screen.drawImage(bufferVirtuale,0,0,this);
  93.         offScreen.dispose();
  94.     }
  95.    
  96.     public void actionPerformed(ActionEvent e){
  97.         if(pallinaPresente){
  98.             if(basso){
  99.                 if(yPallina > (this.getHeight()-diametroPallina)){
  100.                     basso = false;
  101.                     yPallina -= spostamento;
  102.                 }else{
  103.                     yPallina += spostamento;
  104.                 }
  105.             }else{
  106.                 if(yPallina <=0){
  107.                     basso = true;
  108.                     yPallina += spostamento;
  109.                 }else{
  110.                     yPallina -= spostamento;
  111.                 }
  112.             }
  113.            
  114.             if(destra){
  115.                 if(xPallina > 0){
  116.                     destra = false;
  117.                     xPallina += spostamento;
  118.                 }else{
  119.                     xPallina -= spostamento;
  120.                 }
  121.             }else{
  122.                 if((!arrivoComunicato)&&(xPallina < (this.getWidth()-diametroPallina))){
  123.                     try{
  124.                         finestra.getOutput().writeBoolean(basso);
  125.                         finestra.getOutput().writeInt(yPallina);
  126.                         finestra.getOutput().writeBoolean(true);
  127.                         arrivoComunicato = true;
  128.                     }catch(Exception ecc){
  129.                         JOptionPane.showMessageDialog(null,ecc.toString());
  130.                         System.exit(-1);
  131.                     }
  132.                 }else{
  133.                     xPallina -= spostamento;
  134.                     if(xPallina > this.getWidth()){
  135.                         pallinaPresente = false;
  136.                         arrivoComunicato = false;
  137.                     }
  138.                 }
  139.             }
  140.         }else{
  141.             boolean direzione = false;
  142.             int y = 0;
  143.             try{
  144.                 direzione = finestra.getInput().readBoolean();
  145.                 y = finestra.getInput().readInt();
  146.                 pallinaPresente = finestra.getInput().readBoolean();
  147.                 basso = direzione;
  148.                 destra = false;
  149.                 yPallina = y;
  150.                 xPallina = this.getWidth();
  151.             }catch(Exception ecc){
  152.                 JOptionPane.showMessageDialog(null,ecc.toString());
  153.                 System.exit(-1);
  154.             }
  155.         }
  156.         repaint();
  157.     }
  158. }
  159. -ProvaPallaClient.java-
  160. public class ProvaPallaClient {
  161.     public static void main(String[] args) {
  162.        PallaNetClient client = new PallaNetClient();
  163.     }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement