السلام عليكم ، اريد كتابة برنامج بلفة جافا مستخدما الــSockets الفكرة الأساسية للبرنامج هي أن يقوم زبون (client) بالاتصال بــخادم (server) ، وذلك ليتحكم بكرة عن طريق لوحة المفاتيح (ببساطة عندما يستخدم الزبون لوحة مفاتيحه تتحرك كرته و كرة الخادم في نفس الوقت)، لقد قمت بكتابة البرنامج المشكلة هي أن عند تشغيله التحكم يتم في نافذة واحدة (يعني لا توجد علاقة بين النافذتين) و لا أعرف السبب الرجاء الاطلاع على البرنامج و مساعدتي .
class 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 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;
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);
}
/*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)
{
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;}
x=x+coor_x;
y=y+coor_y;
repaint(); //Repaints this component.
}
/*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 void 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);
}
}
***Class server* 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();
balle balle_env= new balle();
balle.lancer_la_balle();
balle.f.setTitle("Balle serveur.");
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();
}
}
***Class Client***
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.SocketException;
public class client {
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;
inStream = new ObjectInputStream(socket.getInputStream());
balle balle_recu = null;
try {
balle_recu = (balle) inStream.readObject();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
balle.lancer_la_balle();
balle.f.setTitle("Balle client.");
} catch (SocketException se) {
se.printStackTrace();
// System.exit(0);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
client client = new client();
client.communicate();
}
}
التعليقات