我有一个多用户聊天室共享白板应用程序,在其中我使用Jpanel在特定客户端上进行绘制,然后通过Java Socket编程通过服务器广播到其他客户端。我的问题是,我希望绘图功能能够实时工作,也就是说,一旦在一个客户端的JPanel上完成绘图,其他客户端就应该立即看到。我在JPanel的mouseReleased事件中编写了该函数,但只有在该客户端的JPanel上触发mouseReleased事件后,其他客户端才能看到它。是否有人可以建议我如何让操作更好(实时)?
//下面的方法是在鼠标释放事件上从GUI中调用的。
@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);
}
//下面的方法是在鼠标释放事件上从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;
}
}
ChatApplication_Client
类,还有LineMessage
类也可能很有用。 - Jonny Henly