Skip to content

Instantly share code, notes, and snippets.

@ahmedissa
Created February 3, 2015 22:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahmedissa/7a4edae24703e135fde0 to your computer and use it in GitHub Desktop.
Save ahmedissa/7a4edae24703e135fde0 to your computer and use it in GitHub Desktop.
Balle / arabia.io #18691
package ball;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.Ellipse2D;
import java.io.Serializable;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.Ellipse2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Balle extends JPanel implements ActionListener,KeyListener
{
/**
*
*/
private static final long serialVersionUID = 1L;
// for Client-Side
private BallListener ballListener;
double x=0, y=0, coor_x=0, coor_y=0;
//A facility for threads to schedule tasks for future execution in a background thread.
Timer t=new Timer(2, this);
/*Constructeur*/
public Balle()
{
t.start();
//Adds the specified key listener to receive key events from this component.
addKeyListener(this);
//Sets the focusable state of this Component to the specified value. This value overrides the Component's default focusability.
setFocusable(true);
//Sets whether focus traversal keys are enabled for this Component.
setFocusTraversalKeysEnabled(true);
}
public Balle(BallListener bListener)
{
this.ballListener = bListener;
t.start();
//Adds the specified key listener to receive key events from this component.
addKeyListener(this);
//Sets the focusable state of this Component to the specified value. This value overrides the Component's default focusability.
setFocusable(true);
//Sets whether focus traversal keys are enabled for this Component.
setFocusTraversalKeysEnabled(true);
}
/*Calls the UI delegate's paint method, if the UI delegate is non-null.*/
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g1=(Graphics2D) g;
//Fills the interior of a Shape using the settings of the Graphics2D context.
g1.fill(new Ellipse2D.Double(x,y,40,40)); //The Double class defines an ellipse specified in double precision.
}
/*Invoked when an action occurs.*/
public void actionPerformed(ActionEvent e)
{
actionPreforme();
repaint(); //Repaints this component.
}
private void actionPreforme(){
if(x<0){coor_x=0; x=0;}
if(x>Balle.f.getWidth()-40){coor_x=0; x=Balle.f.getWidth()-55;}
if(y<0){coor_y=0; y=0;}
if(y>Balle.f.getHeight()-40){coor_y=0; y=Balle.f.getHeight()-80;}
if (ballListener != null)
ballListener.changedLocation(x,y);
x=x+coor_x;
y=y+coor_y;
repaint(); //Repaints this component.
}
public void changeLocation(double x,double y) {
if (x != this.x || y != this.y) {
this.x = x;
this.y = y;
actionPreforme();
}
}
/*HAUT*/
public void up()
{
coor_y=-1;
coor_x=0;
}
/*BAS*/
public void down()
{
coor_y=1;
coor_x=0;
}
/*GAUCHE*/
public void left()
{
coor_x=-1;
coor_y=0;
}
/*DROITE*/
public void right()
{
coor_x=1;
coor_y=0;
}
//Called just after the user presses a key while the listened-to component has the focus.
public void keyPressed(KeyEvent e)
{
int code=e.getKeyCode();
if(code==KeyEvent.VK_UP) //Constant for the non-numpad up arrow key.
{
up();
}
if(code==KeyEvent.VK_DOWN) //Constant for the non-numpad down arrow key.
{
down();
}
if(code==KeyEvent.VK_LEFT) //Constant for the non-numpad left arrow key.
{
left();
}
if(code==KeyEvent.VK_RIGHT) //Constant for the non-numpad right arrow key.
{
right();
}
}
/*nvoked when a key has been typed.*/
public void keyTyped(KeyEvent e){}
/*Invoked when a key has been released.*/
public void keyReleased(KeyEvent e)
{
coor_x=0;
coor_y=0;
}
static JFrame f=new JFrame();
public static Balle lancer_la_balle()
{
Balle l=new Balle();
f.add(l);
f.setVisible(true);
//Sets the operation that will happen by default when the user initiates a "close" on this frame.
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setTitle("Balle.");
f.setSize(300,300);
return l;
}
public static void lancer_la_balle(BallListener bListener)
{
Balle l=new Balle(bListener);
f.add(l);
f.setVisible(true);
//Sets the operation that will happen by default when the user initiates a "close" on this frame.
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setTitle("Balle.");
f.setSize(300,300);
}
public interface BallListener{
public void changedLocation(double x, double y);
}
}
package ball;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.SocketException;
public class Client implements Balle.BallListener{
private Socket socket = null;
private ObjectInputStream inputStream = null;
private ObjectInputStream inStream = null;
private boolean isConnected = false;
public Client() {
}
public void communicate() {
while (!isConnected) {
try {
socket = new Socket("localHost", 4445);
System.out.println("Client Connecte");
isConnected = true;
Balle.lancer_la_balle(this);
Balle.f.setTitle("Balle client.");
} catch (SocketException se) {
se.printStackTrace();
// System.exit(0);
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void changedLocation(double x, double y) {
if (socket.isConnected()) {
try {
socket.getOutputStream().write((int)x);
socket.getOutputStream().write((int)y);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Client client = new Client();
client.communicate();
}
}
package ball;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
public class Server {
private ServerSocket serverSocket = null;
private Socket socket = null;
private ObjectOutputStream outputStream = null;
public Server() {
}
public void communicate() {
try {
serverSocket = new ServerSocket(4445);
System.out.println("Serveur demmare..");
socket = serverSocket.accept();
int x, y;
Balle ball = Balle.lancer_la_balle();
Balle.f.setTitle("Balle serveur.");
while (socket.isConnected()) {
x = socket.getInputStream().read();
y = socket.getInputStream().read();
ball.changeLocation(x, y);
}
// outputStream = new ObjectOutputStream(socket.getOutputStream());
// outputStream.writeObject(balle_env);
System.out.println("balle envoye");
socket.close();
} catch (SocketException se) {
System.exit(0);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Server server = new Server();
server.communicate();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment