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;
}
}
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.