I did a Dots and Boxes game in java with Netbeans 5.5 I have a doubt because the game is between each two players, it's multiclient with sessions of two players.
Client 1 send the message with sal.println to the server and the server to Client 2 but this client only follows the path of the client 1 but it doesn't paint the lines that Client 1 is making.
The same happens when Client 2 is playing.
import java.net.*;
import java.io.*;
import javax.swing.*;
import java.awt.Graphics2D;
import java.awt.geom.*;
import java.awt.*;
import javax.swing.JOptionPane;
public class Cliente extends javax.swing.JFrame implements Runnable{
CLabel etiq;
Socket sCliente=null;
PrintWriter salida=null;
BufferedReader entrada=null;
int cliente;
Thread tarea=null;
String nombre="";
String msjServidor="";
String cad="";
String msjCliente="";
int n,k,j,x,y,p,q;
Graphics2D g2;
int x1,y1,x2,y2,a,b,c,d;
JTextArea TA;
boolean turno1,turno2;
/** Creates new form Cliente */
public Cliente() {
initComponents();
setBounds(0,0,700,700);
etiq=new CLabel();
jPanel1.add(etiq);
etiq.setBounds(5,5,650,550);
turno1=true;
turno2=false;
try{
TAMsj.append("\nCliente:Intentando la conexión con el servidor...");
sCliente=new Socket("127.0.0.1",6001);
TAMsj.append("\nCliente: Conexión aceptada...");
salida=new PrintWriter(sCliente.getOutputStream(),true);
entrada= new BufferedReader(new InputStreamReader(sCliente.getInputStream()));
cliente=sCliente.getInputStream().read();
tarea=new Thread(this);
tarea.start();
}catch(UnknownHostException e){
TAMsj.append("\nCliente: No se pudo encontrar el servidor...");
}catch(IOException e){
TAMsj.append("\nCliente: No se pudo efectuar la conexión...");
}
nombre=(String)JOptionPane.showInputDialog(this,"Escribe tu nombre","Identificación del cliente",JOptionPane.PLAIN_MESSAGE,null,null,"Anónimo");
setTitle("Timbiriche " + "Cliente: " + cliente + " " + nombre);
}
public void run(){
try{
while((msjServidor=entrada.readLine())!=null){
if(msjServidor.substring(0,2).equals("c:")){
TAMsj.append("\n" + msjServidor);
recibe(msjServidor);
}else{
n=nombre.length();
TAMsj.append("\n" + msjServidor.substring(0,n+2) + msjServidor.substring(n+4));
}
if(msjServidor.equals("Hasta pronto..."))
break;
}
}catch(IOException e){
TAMsj.append("\nCliente: No se pudo efectuar la conexión...");
}
}
public void recibe(String nn){
j=nn.indexOf(",");
a=Integer.valueOf(nn.substring(2,5));
j=nn.indexOf(",");
b=Integer.valueOf(nn.substring(6,9));
p=nn.indexOf("*");
q=nn.indexOf("=");
c=Integer.valueOf(nn.substring(10,13));
d=Integer.valueOf(nn.substring(14));
if(cliente==1){
Graphics g=etiq.getGraphics();
g2=(Graphics2D)g;
g2.setPaint(Color.RED);
g2.setStroke(new BasicStroke(4.0f));
g2.draw(new Line2D.Double(a,b,c,d));
repaint();
}else if(cliente==2){
Graphics g=etiq.getGraphics();
g2=(Graphics2D)g;
g2.setPaint(Color.BLUE);
g2.setStroke(new BasicStroke(4.0f));
g2.draw(new Line2D.Double(a,b,c,d));
repaint();
}
}
private void jPanel1MouseDragged(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
if(k==0){
x1=evt.getX();
y1=evt.getY();
k++;
}
if(k==1){
x2=evt.getX();
y2=evt.getY();
k=0;
}
Graphics g=etiq.getGraphics();
g2=(Graphics2D)g;
if(cliente==1){
g2.setPaint(Color.BLUE);
g2.setStroke(new BasicStroke(4.0f));
g2.draw(new Line2D.Double(x1,y1,x2,y2));
msjCliente="c:"+ x1 +"," + y1 + "*" + x2 + "=" + y2;
salida.println(msjCliente);
}else if(cliente==2){
g2.setPaint(Color.RED);
g2.setStroke(new BasicStroke(4.0f));
g2.draw(new Line2D.Double(x1,y1,x2,y2));
msjCliente="c:"+ x1 +"," + y1 + "*" + x2 + "=" + y2;
salida.println(msjCliente);
}
// g2.drawRect(x1,y1,5,30);
}
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
cad=jTextField1.getText();
msjCliente="t:" + cad;
if(msjCliente!=null)
salida.println(nombre+": " + msjCliente);
}