client server - client problem
WhiteboardApplet.java
--------------------------------
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.util.*;
import java.net.*;
import java.io.*;
public class WhiteBoardApplet extends Applet
implements ActionListener, AdjustmentListener,KeyListener,
MouseListener,
MouseMotionListener, WindowListener
{
Socket skt;
ObjectOutputStream oos;
Vector ht;
ServerSocket ss2;
Socket cs2;
Vector ht2;
ObjectInputStream ois2;
//declare variables
Image offscreenImage;
Graphics offscreenImageG;
Graphics drawPanelG;
int offscreeImage_width, offscreeImage_height;
boolean isButtonPressed;
int pre_x, pre_y; //the coordinates at the beginning of mouse
dragged
int x, y; //the coordinates in the end of mouse dragged
int top_left_corner_x, top_left_corner_y; //the x and y
coordonate of top-left corner
//that a image is drawn with
in the graphic context
int scrollbarV_value; //to hold vertical scrollbar value
int scrollbarH_value; //to hold herizonal scroll bar value
String s; //to hold string input from keyboard
/***************************
* init()
* description: The entry point for the applet.
* build window and initiate variables
* **************************
*/
public void init()
{ //build window
buildWindow ( );
//initiate variables
isButtonPressed=false;
top_left_corner_x=0;
top_left_corner_y=0;
offscreeImage_width 00;
offscreeImage_height 00;
drawPanelG = drawPanel.getGraphics ();
offscreenImage = this.createImage( offscreeImage_width,
offscreeImage_height);
offscreenImageG = offscreenImage.getGraphics();
offscreenImageG.setColor( Color.white );
offscreenImageG.fillRect( 0, 0, offscreeImage_width,
offscreeImage_height);
offscreenImageG.setColor( Color.black );
ht = new Vector();
ht2 = new Vector();
GandhiUpdate2();
}
/***************************
* update()
* description:
* update the container, call the method paint()
* **************************
*/
public void update( Graphics g ) {
paint( g );
}
/***************************
* paint()
* description:
* drawPanelG draw offscreenImage,
* begin from the top left corner
* **************************
*/
public void paint( Graphics g ) {
drawPanelG.drawImage ( offscreenImage,top_left_corner_x,
top_left_corner_y, this );
}
/**************Build Window****************/
//declare variables for buiding window
Frame outerBox; //to place all visible components
Panel topPanel; //to hold "clear" button
Panel drawPanel; // draw area
Panel bottomPanel; // to hold herizonal scrollbar and message
area
Scrollbar scrollbarV; //vertical scrollbar
Scrollbar scrollbarH; //herizonal scrollbar
TextField message;
Button clearButton;
private void buildWindow ( ) {
// *** Instantiate objects
outerBox = new Frame ( );
topPanel= new Panel();
drawPanel = new Panel ( );
bottomPanel = new Panel ( );
//***build button
Button clearButton=new Button("Clear");
clearButton.setBackground(new Color(193, 211, 245));
clearButton.addActionListener(this);
// *** build the scrollbars and add addAdjustmentListener
scrollbarV = new Scrollbar (Scrollbar.VERTICAL, 0, 100, 0,
1000 );
scrollbarV.addAdjustmentListener ( this );
scrollbarH = new Scrollbar (Scrollbar.HORIZONTAL, 0, 100, 0,
1000 );
scrollbarH.addAdjustmentListener ( this );
// *** build the message display area
message = new TextField ( "Messages Displayed Here", 80 );
message.setEditable ( false ); //set the textField message as
not editable
//build the top panel
topPanel.setSize(600, 30);
topPanel.add(clearButton);
//register envent on the draw panel(draw area)
drawPanel.addKeyListener(this);
drawPanel.addMouseListener(this);
drawPanel.addMouseMotionListener(this);
// *** build the bottom panel
bottomPanel.setLayout ( new BorderLayout ( ) );
bottomPanel.add ( "North", scrollbarH );
bottomPanel.add ( "South", message );
// *** Set colors
topPanel.setBackground(Color.gray);
drawPanel.setBackground (Color.white);
// *** build the window
outerBox.setLayout (new BorderLayout ( ) );
outerBox.add ("North", topPanel);
outerBox.add ("Center", drawPanel);
outerBox.add ("South", bottomPanel);
outerBox.add ("East", scrollbarV);
outerBox.setSize (600, 500);
outerBox.setBackground (Color.white);
outerBox.addWindowListener (this);
outerBox.setVisible ( true );
} // end buildWindow ()
//*********** Interface Methods ***********
//**** actionPerformed methods
public void actionPerformed ( ActionEvent e ) {
//call actionClear()
actionClear();
}//end actionPerformed method
//actionClear method
private void actionClear(){
//fill the offscreenImage all white
offscreenImageG.setColor( Color.white );
offscreenImageG.fillRect( 0, 0, offscreeImage_width,
offscreeImage_height);
offscreenImageG.setColor( Color.black );
repaint();
}//end actionClear
//**** AdjustmentListener method
public void adjustmentValueChanged ( AdjustmentEvent e ) {
Object s = e.getSource();
// *** process Scrollbar actions
if ( s instanceof Scrollbar ) {
if ( s == scrollbarV ) {
scrollbarV_value=scrollbarV.getValue();
top_left_corner_y = - scrollbarV_value;
message.setText ( "Vertical ScrollBar value: " +
scrollbarV_value );
repaint();
}
if ( s == scrollbarH ) {
scrollbarH_value=scrollbarH.getValue();
top_left_corner_x = - scrollbarH_value;
message.setText ( "Horlzontal ScrollBar value: " +
scrollbarH_value);
repaint(); }
} // end process scrollbar actions
} //end adjustmentValueChanged method
//*****KeyListener methods
public void keyTyped( KeyEvent e ) {
char c = e.getKeyChar();
if ( c != KeyEvent.CHAR_UNDEFINED ) {
s = s + c;
offscreenImageG.drawString( s, x+scrollbarH_value, y
+scrollbarV_value);
repaint();
e.consume();
} message.setText("Typing...");
}
public void keyPressed(KeyEvent e){}
public void keyReleased(KeyEvent e){}
//****MouseListener methods
public void mouseClicked(MouseEvent e){
// called after a press and release of a mouse button
// with no motion in between
// (If the user presses, drags, and then releases, there
will be
// no click event generated.)
x = e.getX();
y = e.getY();
s = "";
offscreenImageG.drawString( s, x+scrollbarH_value, y
+scrollbarV_value);
repaint();
e.consume();
message.setText("Mouse clicked");
} //end mouseClicked
public void mousePressed(MouseEvent e){
//called after a mouse button is pressesd;
isButtonPressed=true;
message.setText("Mouse Pressed.");
}
public void mouseReleased(MouseEvent e)
{
isButtonPressed=false;
try
{
skt = new Socket("localhost", 9000);
oos = new ObjectOutputStream(skt.getOutputStream());
oos.writeObject(ht);
oos.close();
ht.clear();
}
catch (Exception x)
{
x.printStackTrace();
}
message.setText("Mouse released.");
}
public void mouseEntered(MouseEvent e){
}
public void mouseExited(MouseEvent e){
}
//*****MouseMotionListener methods
public void mouseDragged(MouseEvent e){
if(isButtonPressed==true)
{
pre_x=e.getX()+scrollbarH_value;
pre_y=e.getY()+scrollbarV_value;
isButtonPressed=false;
}
x=e.getX()+scrollbarH_value;
y=e.getY()+scrollbarV_value;
offscreenImageG.drawLine(pre_x, pre_y, x, y);
drawLines(pre_x + ":" + pre_y + ":" + x + ":" + y);
pre_x=x;
pre_y=y;
repaint();
e.consume();
message.setText(pre_x + ":" + pre_y + ":" + x + ":" + y);
} //end mouseDragged
public void drawLines(String m)
{
ht.add(m);
}
public void GandhiUpdate2()
{
try
{
ss2 = new ServerSocket(8000);
cs2 = ss2.accept();
while(true)
{
ois2 = new ObjectInputStream(cs2.getInputStream());
ht2 = (Vector)ois2.readObject();
//System.out.println(ht2);
ois2.close();
cs2.close();
cs2= ss2.accept();
repaint();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void mouseMoved(MouseEvent e) {
message.setText("x: "+e.getX()+", y: "+e.getY());
}
//**** WindowListener methods
public void windowActivated ( WindowEvent e ) {
}
public void windowDeactivated ( WindowEvent e ) {
} public void windowOpened ( WindowEvent e ) {
}
public void windowClosed ( WindowEvent e ) {
}
public void windowClosing ( WindowEvent e ) {
outerBox.setVisible ( false );
outerBox.dispose ();
}
public void windowIconified ( WindowEvent e ) {
}
public void windowDeiconified ( WindowEvent e ) {
}
}//end WhiteBoardApplet
GandhiServer2.java
----------------------------
import java.net.*;
import java.util.*;
import java.io.*;
public class GandhiServer2
{
ServerSocket ss;
Socket cs;
Vector ht;
ObjectInputStream ois;
Socket skt;
ObjectOutputStream oos;
public GandhiServer2()
{
try
{
ss = new ServerSocket(9000);
cs = ss.accept();
while(true)
{
ois = new ObjectInputStream(cs.getInputStream());
ht = (Vector)ois.readObject();
if(!ht.isEmpty())
{
try
{
skt = new Socket("localhost", 8000);
oos = new
ObjectOutputStream(skt.getOutputStream());
oos.writeObject(ht);
oos.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
System.out.println(ht);
ois.close();
ht.clear();
cs.close();
cs = ss.accept();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
new GandhiServer2();
}
}
The code above is a client server application. What the client does is
that it sends the coordinates of what is drawn on a canvas to the
server. The server will receive this coordinates and retransmit it to
all the clients connected to the server and display it on the canvas.
The client server concept work well, however after implementing this
concept, I cant see what I have drawn on the canvas, but it does send
the coordinates to the server.
How do I solve the problem.
Your help is kindly appreciated.
Regards.