Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -PallaNetClient.java-
- import java.awt.*;
- import java.awt.event.*;
- import java.io.*;
- import java.net.*;
- import javax.swing.*;
- public class PallaNetClient extends JFrame {
- private Socket connessione = null;
- private DataOutputStream out = null;
- private DataInputStream input = null;
- public PallaNetClient(){
- super("PallaNet - Client");
- this.setSize(500,400);
- this.setLocationRelativeTo(null);
- this.setDefaultCloseOperation(EXIT_ON_CLOSE);
- connetti();
- this.setVisible(true);
- }
- public void connetti(){
- try{
- connessione = new Socket("localhost",6789);
- out = new DataOutputStream(connessione.getOutputStream());
- input = new DataInputStream(connessione.getInputStream());
- }catch(Exception e){
- JOptionPane.showMessageDialog(null,e.toString());
- System.exit(-1);
- }
- Container contenitore = this.getContentPane();
- PannelloClient pan = new PannelloClient(this,contenitore.getSize());
- contenitore.add(pan);
- }
- public DataInputStream getInput(){
- return input;
- }
- public DataOutputStream getOutput(){
- return out;
- }
- }
- -PannelloClient.java-
- import java.awt.*;
- import java.awt.event.*;
- import java.io.*;
- import java.net.*;
- import javax.swing.*;
- class PannelloClient extends JPanel implements ActionListener {
- private PallaNetClient finestra;
- private Dimension dimensioni;
- private Image bufferVirtuale;
- private Graphics offScreen;
- private Timer tim = null;
- private int xPallina = 0;
- private int yPallina = 0;
- private int diametroPallina = 40;
- private int spostamento = 3;
- private int timerDelay = 10;
- private boolean destra,basso,pallinaPresente,arrivoComunicato;
- public PannelloClient(PallaNetClient finestra, Dimension dimensioni){
- super();
- this.finestra = finestra;
- this.setSize(dimensioni);
- destra = true;
- basso = true;
- pallinaPresente = false;
- arrivoComunicato = 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(pallinaPresente){
- offScreen.fillOval(xPallina,yPallina,diametroPallina,diametroPallina);
- }
- screen.drawImage(bufferVirtuale,0,0,this);
- offScreen.dispose();
- }
- public void actionPerformed(ActionEvent e){
- if(pallinaPresente){
- if(basso){
- if(yPallina > (this.getHeight()-diametroPallina)){
- basso = false;
- yPallina -= spostamento;
- }else{
- yPallina += spostamento;
- }
- }else{
- if(yPallina <=0){
- basso = true;
- yPallina += spostamento;
- }else{
- yPallina -= spostamento;
- }
- }
- if(destra){
- if(xPallina > 0){
- destra = false;
- xPallina += spostamento;
- }else{
- xPallina -= spostamento;
- }
- }else{
- if((!arrivoComunicato)&&(xPallina < (this.getWidth()-diametroPallina))){
- try{
- finestra.getOutput().writeBoolean(basso);
- finestra.getOutput().writeInt(yPallina);
- finestra.getOutput().writeBoolean(true);
- arrivoComunicato = true;
- }catch(Exception ecc){
- JOptionPane.showMessageDialog(null,ecc.toString());
- System.exit(-1);
- }
- }else{
- xPallina -= spostamento;
- if(xPallina > this.getWidth()){
- pallinaPresente = false;
- arrivoComunicato = false;
- }
- }
- }
- }else{
- boolean direzione = false;
- int y = 0;
- try{
- direzione = finestra.getInput().readBoolean();
- y = finestra.getInput().readInt();
- pallinaPresente = finestra.getInput().readBoolean();
- basso = direzione;
- destra = false;
- yPallina = y;
- xPallina = this.getWidth();
- }catch(Exception ecc){
- JOptionPane.showMessageDialog(null,ecc.toString());
- System.exit(-1);
- }
- }
- repaint();
- }
- }
- -ProvaPallaClient.java-
- public class ProvaPallaClient {
- public static void main(String[] args) {
- PallaNetClient client = new PallaNetClient();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement