在JPanel上绘图并将其广播到多个客户端

时间:2015-12-12 23:15:43

标签: java swing sockets jpanel graphics2d

我有多用户聊天室,共享白板应用程序,我使用Jpanel在特定客户端绘图,然后通过服务器广播到其他客户端(通过Java套接字编程)。 我的问题是我想实时绘制工作的功能,就像在一个客户端的JPanel上完成绘图一样,它应该对其他客户端可见。我在JPanel的mouseReleased事件上编写了函数,但只有在客户端的JPanel上触发了mouseReleased事件后才能看到其他客户端。 任何人都可以建议我可以更好地(实时)采取行动吗?

        @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);
                        }
                     } 

        }

//此方法负责在客户端重新绘制和广播

  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);
   }

//从mouseReleased事件中的gui调用以下方法

  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 个答案:

答案 0 :(得分:0)

Pivoted_TXN_DATES = pd.pivot_table(NeededColumns, 
  index=['SPC_LOT', 'SPC_LOT7_x', 'RAW_SCRIBE', 'RAW_ID_3',
         'SPC_PRODUCT'],
  columns='SPC_OPERATION',
  values='SPC_TXN_DATE_x',
  aggfunc=lambda x: x).reset_index()