3

I have Multi-user Chat-room with shared whiteboard application where I used a Jpanel to draw in a specific client and then broadcast over server to other clients(through Java Socket Programming). My issue is that I wanted to make the function of draw work real-time as in as soon as a drawing is done on one client's JPanel it should be visible to the other clients. I wrote the function on mouseReleased event of JPanel, but it is visible to the other clients only after a mouseReleased event is fired on that client's JPanel. Can anyone suggest something by which I can make the action better(real-time)?

        @Override
        public void mouseReleased(MouseEvent e) {           
            lineObject =  new LineMessage(); 
            lineObject.setImageMessage(DrawPanel.linelist);
            ChatApplication_Client.Action_Paint(lineObject);

        }

ChatApplication_Client.java

  public void run(){
    System.out.println("Listening for messages from server . . . ");                       
    try{
                    while(!receivingdone){
                        object = myInputStream.readObject();

                       if(object instanceof LineMessage)
                        {
                            lineObject = (LineMessage) object;
                            WhiteBoardMessageReceive(lineObject);
                        }
                     } 

        }

// This method responsible for re-painting and broadcasting at client's end

  private void WhiteBoardMessageReceive(LineMessage lineObject)
   {
                ArrayList<Line> linelist = (ArrayList) 
                lineObject.getImageMessage();                          
                ChatClient_GUI.TA_ChatWindow.append(lineObject.Name+": "
                                +lineObject.Text + "\n" + "At [" 
                                    +DateUtils.now()+ "] " + "\n");
                    drawPanel.drawit(linelist);
   }

//The Following method is called from the gui on mouseReleased event

  public static void Action_Paint(LineMessage lineObject)
   {

       try
       {
        myOutputStream.reset();
        myOutputStream.writeObject(lineObject);
        myOutputStream.flush();
       }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
        }
   }

LineMessage.java

class LineMessage implements Serializable 
   {
    ArrayList<Line> message;
    Line line = new Line();
    String Name =line.getName() ;
    String Text ;
    public void setImageMessage(Object message) {
            this.message = (ArrayList) message;
    }

    public Object getImageMessage() {
    return message;
    }
}
class Line extends ChatMessage implements Serializable {
int startx, starty, endx, endy;
    public Line() {
    }
    public Line(int sx, int sy, int ex, int ey)
    {
    setStartX(sx);
    setStartY(sy);
    setEndX(ex);
    setEndY(ey);
    }
        public void setStartX(int sx) {

        startx = sx;
        }
        public void setStartY(int sy) {
        starty = sy;
        }
        public void setEndX(int ex) {
        endx = ex;
        }
        public void setEndY(int ey) {
        endy = ey;
        }
        public int getStartX() {
        return startx;
        }
        public int getStartY() {
        return starty;
        }
        public int getEndX() {
        return endx;
        }
        public int getEndY() {
        return endy;
        }
}

1 Answers1

0
    List<Point> pointList = new ArrayList<>();
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300, 500);

    JPanel panel = new JPanel(){
        @Override
        public void paint(Graphics g) {
            super.paint(g);
            g.setColor(Color.WHITE);
            g.fillRect(0, 0, getWidth(), getHeight());

            g.setColor(Color.BLACK);
            Point l = null;
            for(Point p : pointList){
                if(l != null && p != null)
                    g.drawLine(l.x, l.y, p.x, p.y);
                l = p;
            }
        }
    };
    frame.setContentPane(panel);

    panel.addMouseMotionListener(new MouseMotionListener() {
        boolean isDrawing = false;
        @Override
        public void mouseDragged(MouseEvent e) {
            pointList.add(e.getPoint());
            isDrawing = true;
            panel.repaint();
        }

        @Override
        public void mouseMoved(MouseEvent e) {
            if(isDrawing)
                pointList.add(null); //null is a placeholder to mean line is finished
            isDrawing = false;
        }
    });


    frame.setVisible(true);
John E.
  • 418
  • 2
  • 10
  • Maybe add an explanation to go along with the code. – Jonny Henly Dec 12 '15 at 23:57
  • I do have the piece of code written in DrawPanel class, where else do u suggest me to make changes? – ANUSHREE SAHA Dec 13 '15 at 00:08
  • Where is the DrawPanel class? You should be using MouseMotionListener instead as shown above. Any time the mouse is moved it calls mouseDragged if the mouse button is down, or mouseMoved if the mouse button is up. Try running the code above see if you can figure out how it works from there. If there are still problems i'm here. – John E. Dec 13 '15 at 00:12
  • It does not work, it does not get displayed even if I write it using MouseMotionListener – ANUSHREE SAHA Dec 13 '15 at 00:51
  • can you show me the problem code? the one with the MouseMotionListener? – John E. Dec 13 '15 at 01:14