Client/Server Game.
I am making a client server game.
When I send the information to the server I send it in a String.
I want to send the coordinates x1,y1,x2,y2 to the other client so that it can draw a line with this coordinates.
I make a string called msjClient in this way:
msjClient= "c: "+ x 1 + "," + y1 + "*" + x2 + "=" + y2
salida.println(msjClient)
In the other client I put this code:
public void recibe(String nn){
j=nn.indexOf(",");
a=Integer.valueOf(nn.substring(2,j));
j=nn.indexOf(",");
b=Integer.valueOf(nn.substring(j+1,p));
p=nn.indexOf("*");
q=nn.indexOf("=");
c=Integer.valueOf(nn.substring(p+1,q));
d=Integer.valueOf(nn.substring(q+1));
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();
}
}
But it sends an error.
I changed a little the msjClient and it worked a little but the line wasn't drawn. It only followed the path of the other client cursor but it didn't paint the line.
[1395 byte] By [
vluvlua] at [2007-11-27 8:36:11]

How do you draw something and make that Client 2 see the same thing than Client1?Because it doesn't draw the line. It only follows the path but it doesn't paint nothing.
Could you tell me a good idea of draw lines in a Dots and Boxes Game because I try to draw a line with OnMouseDragged but Client2 doesn't draw the line.
Send a string like:"227,103,499,882"and the receiver should use String.split(",") and Integer.parseInt() methods.Of course in the paint() or paintComponent() method of the receiver, Graphics.drawLine() should be called.
hiwaa at 2007-7-12 20:33:01 >

Could you explain a little more?
Well, I changed the order of this
j=nn.indexOf(",");
a=Integer.valueOf(nn.substring(2,j));
j=nn.indexOf(",");
p=nn.indexOf("*");<--I had this declared after the line of b.
b=Integer.valueOf(nn.substring(j+1,p));
q=nn.indexOf("=");
c=Integer.valueOf(nn.substring(p+1,q));
d=Integer.valueOf(nn.substring(q+1));
and it doesn't send the exception that was sending.
But it doesn't paint nothing.
I have a class for a label. The class name is Labe. I declared an object of Labe type.
Labe etiq;
Later I write in the OnMouseDragged event:
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);
}
I can draw lines between points but in the other client it only follows the path but it doesn't paint nothing.
How can access the paintComponent method of the class Labe from the OnMouseDragged event.
Could you tell me what event I have to use that when the player moves the mouse near two points he can visualize the line and when he makes click the line stay painted? I want to make a game like the one in yahoo games. Dots and Boxes.
> Could you help me?reply #3
hiwaa at 2007-7-12 20:33:01 >

I'm going to use RadioButtons to make it easier.
> How can access the paintComponent method of the class Labe from
> the OnMouseDragged event.
If your object from class Labe is named lala, then you should call,
lala.repaint();
The repaint() method calls your paintComponent() method at the most appropriate timing.
hiwaa at 2007-7-12 20:33:01 >

Thanks.
I did the Dots and Boxes game with RadioButtons.
But I want to do a cycle or loop to make the lines with few code.
I put :
for(x=0; x<41;x++){
RB= (javax.swing.JRadioButton) evt.getComponent(x);
RB1=(javax.swing.JRadioButton) evt.getComponent(x+1);
x1=RB.getX();
y1=RB.getY();
x2=RB1.getX();
y2=RB1.getY();
g=etiq.getGraphics();
g2=(Graphics2D)g;
g2.setPaint(color);
g2.setStroke(bla,bla,bla);
g2.draw(Line2D(x1,y1,x2,y2);
}
But to do the lines, I have to push always the radiobutton 0;
Could you tell me a cycle to draw the lines with getComponent?
Yours is a wrong code. Do like this:
for (.......................){
... // update the values of parameters
... // which your paintComponent() uses
lala.repaint(); // if this is the component, simply repaint();
}
hiwaa at 2007-7-12 20:33:01 >
