The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
December 2000

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Only for the age of 17 !!!

Posted by Kishori Sharan on December 15, 2000 at 11:45 PM

Hi Jon
I have developed an application ( same as you want client-server) in which you can draw a free hand picture on client and that picture will be drawn on server. There are four classes involed
1) ALine
2) DPanel
3) DClient
4) DServer
I have attached all these four files in this posting. Copy them and put them in four files ALine.java, DPanel.java, DClient.java and DServer.java . Then compile these files. First run DServer class and then click on the button "Start Server". Then run DClient class and draw some free hand picture by just dragging your mouse and you can see these picure on server screen.
The code is not optimized. It is only for demo purpose. If you need some improvement you can do it. You want to run the client and server on different machine then change SERVER and PORT variables in DPanel.java.
If you need any help , let me know.
Thanx
Kishori

////////// Start of ALine.java
import java.io.* ;

public class ALine implements Serializable {
private int x1 = 0 ;
private int x2 = 0 ;
private int y1 = 0 ;
private int y2 = 0 ;

public ALine ( ) {
}

public ALine ( int x1, int y1, int x2, int y2 ) {
this.x1 = x1 ;
this.y1 = y1 ;
this.x2 = x2 ;
this.y2 = y2 ;
}

public int getX1 ( ) {
return x1 ;
}

public int getY1 ( ) {
return y1 ;
}

public int getX2 ( ) {
return x2 ;
}

public int getY2 ( ) {
return y2 ;
}

}

////////////End of ALine.java

////////////Start of DPanel.java
import java.io.* ;
import java.net.* ;
import java.awt.* ;
import java.awt.event.* ;
import javax.swing.* ;


public class DPanel extends JPanel {
private int x1 = 0 ;
private int x2 = 0 ;
private int y1 = 0 ;
private int y2 = 0 ;

public final static String SERVER = "127.0.0.1" ;
public final static int PORT = 8888 ;

// Socket to connect to server
Socket connection = null ;

ObjectOutputStream out = null ;

// Flag to indicated that connection to server is ok
boolean connected = false ;

// Client or server panel
private boolean client = true ;


public DPanel ( boolean client ) {
super ( ) ;

this.client = client ;

// For client add mouse motion listenr
if ( client ) {
addMouseMotionListener ( new MouseMotionAdapter ( ) {
public void mouseMoved ( MouseEvent e ) {
x1 = e.getX ( ) ;
y1 = e.getY ( ) ;
}

public void mouseDragged ( MouseEvent e ) {
x2 = e.getX ( ) ;
y2 = e.getY ( ) ;
repaint ( ) ;
}

}
) ;

try {
// Create the socket
connection = new Socket ( SERVER, PORT ) ;

// Get the output stream to send the data to server
out = new ObjectOutputStream ( connection.getOutputStream ( ) ) ;
connected = true ;
}
catch ( Exception e ) {
System.out.println ( "Cound not establish connection with server\n" + e.getMessage ( ) ) ;
}
}


}

public void paint ( Graphics g ) {
// If connected to server then send the line coordinates to server
if ( connected && client ) {
try {
out.writeObject ( new ALine ( x1, y1, x2, y2 ) ) ;
out.flush ( ) ;
}
catch ( Exception e ) {
System.out.println ( "Could not wtire a line coordinates to server" + e.getMessage ( ) ) ;
}
}

g.drawLine ( x1, y1, x2, y2 ) ;
//Reset the starting point
x1 = x2 ;
y1 = y2 ;
}

public void setLine ( ALine line ) {
this.x1 = line.getX1 ( ) ;
this.y1 = line.getY1 ( ) ;
this.x2 = line.getX2 ( ) ;
this.y2 = line.getY2 ( ) ;

if ( !client ) {
System.out.println ( "Got a line (" + x1 + ", " + y1 + ", " + x2 + ", " + y2 + ")" ) ;
}
}


public void setPoints ( int x1, int y1, int x2, int y2 ) {
this.x1 = x1 ;
this.y1 = y1 ;
this.x2 = x2 ;
this.y2 = y2 ;
}
}

////////////End of of DPanel.java

////////////Start of DClient.java
import java.awt.* ;
import javax.swing.* ;


public class DClient extends JFrame {
DClient ( String title ) {
super ( title ) ;
Container cp = getContentPane ( ) ;
cp.add ( new DPanel ( true ) ) ;

}

public static void main ( String[] args ) {
DClient dc = new DClient ( "Client" ) ;
dc.setBounds ( 50, 50 , 200, 200 ) ;
dc.show ( ) ;
}

}
////////////End of of DClient.java

////////////Start of DServer.java

import java.io.* ;
import java.awt.* ;
import javax.swing.* ;
import java.awt.event.* ;
import java.net.* ;

public class DServer extends JFrame {
DPanel panel = new DPanel ( false ) ;
JButton start = new JButton ( "Start Server" ) ;

DServer ( String title ) {
super ( title ) ;
Container cp = getContentPane ( ) ;
cp.add ( panel ) ;
cp.add ( start, "North" ) ;

//When button is clicked then start the server
start.addActionListener (
new ActionListener ( ) {
public void actionPerformed ( ActionEvent e ) {
try {
ServerSocket s = new ServerSocket ( DPanel.PORT ) ;
Socket connection = s.accept ( ) ;
ObjectInputStream in = new ObjectInputStream ( connection.getInputStream ( ) ) ;

while ( true ) {
panel.setLine ( ( ALine ) in.readObject ( ) ) ;

// If we call repaint ( ) then it doesn't work
// If fact , we should read the new line coordinates
// in a separate thread. This is ok for demo
panel.paint ( panel.getGraphics ( ) ) ;
}

}
catch ( Exception ex ) {
System.out.println ( ex.getMessage ( ) ) ;
}
}
}
) ;

}

public static void main ( String[] args ) {
DServer dc = new DServer ( "Server" ) ;
dc.setBounds ( 50, 50 , 200, 200 ) ;
dc.show ( ) ;
}

}
////////////End of of DServer.java




Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us