6

I am building a whiteboard, which would have a server(teacher) and clients(students). Teacher would draw something on his side, which will be shown exactly same to the students.

  1. I want to know which component i should use to do the drawing? i am currently drawing on JPanel .
  2. I want the screen of Server gets copied on the clients, so for that what could be the right method to do this?
    • option1: i save the JPanel as image, and send thru socket, and loads it on the screen of client, also it always saves the background image only, not what the user has drawn onto it. OR
    • option2: both server and client JPanel dimensions are same, so i just send the new coordinates drawn everytime thru socket, with some protocol to understand whether it is rubber or pencil..

Any help would be appreciated.

Nikhar
  • 1,074
  • 8
  • 23
  • 3
    If this is for online use, are you sure that Java Swing is the best choice? – Hovercraft Full Of Eels Apr 01 '12 at 05:07
  • 4
    @Josh: actually that's one of the main reasons for avoiding non-standard abbreviations -- so that those whose first language isn't English won't have to struggle to understand the post. – Hovercraft Full Of Eels Apr 01 '12 at 05:09
  • it is experimental, i wanted to work on this concept, and i want to know the right direction to proceed. – Nikhar Apr 01 '12 at 05:12
  • @AndrewThompson lol..thanks and sorry for spelling mistake. – Nikhar Apr 01 '12 at 05:13
  • @HovercraftFullOfEels if not SWING, then plz suggest what can be used? – Nikhar Apr 01 '12 at 05:17
  • 2
    In deference to @Hovercraft, I think a free floating, frame based app. launched from a link on a web page would be perfect for this! For the first part, use Swing (`JFrame`). For the 2nd part, launch that frame using [Java Web Start](http://stackoverflow.com/tags/java-web-start/info). – Andrew Thompson Apr 01 '12 at 05:20
  • I don't know as this is far afield from my expertise (which is mainly in creating Swing applications), but since you appear to be wanting to create a web application that resides on a page (I think), then perhaps something using javascript, like [Ajax](http://en.wikipedia.org/wiki/Ajax_(programming)). – Hovercraft Full Of Eels Apr 01 '12 at 05:21
  • @NikharSharma You're welcome, but I missed one in the title! Note that it is spelled 'Java', not 'JAVA' - also, if it is in a tag, (e.g. [tag:java], [tag:swing]), it does not need to be in the title of the post. I'll leave that to your discretion, to edit, and your future reference. :) – Andrew Thompson Apr 01 '12 at 05:23

2 Answers2

7
  1. Use a JComponent unless you need to add other components, then use a JPanel.
  2. Send the drawing objects rather than an image. It is less bandwidth than pushing the image. Also implement the protocol to allow the 'adding or deleting' of drawn elements. That way you only need to send the latest object(s) to each client. This approach also has the added benefit of being more use to the end-user (student). You might add functionality later to allow the student to adjust/add/delete the elements in the drawing.

Update

if I draw a line on teacher's side, the same should happen to the child's screen, live, so there is a feeling of realtime drawing..

Then you definitely want to go the least bandwidth intensive route possible. That will be the bottleneck.

..have a JFrame, onto which i have 2 JPanels, 1 for drawing, 1 for buttons.

In that case, make the drawing component a JComponent & stay with a JPanel (or a JToolBar) for the controls.

..what would be that "least bandwidth intensive route possible"?

In order of bandwidth and ignoring corner cases, they would decrease in approximately this order:

  1. Sending full-screen image:
    1. High quality JPEG
    2. PNG.
    3. High compression JPEG
  2. Sending part-screen image:
    1. High quality JPEG
    2. PNG.
    3. High compression JPEG
  3. Dealing with the drawing elements directly, by sending:
    1. The entire list of drawn objects each update
    2. The current modifications, additions, deletions of drawn elements, relying on the client to hold a model and adjust it as needed.

In case there is any confusion, I recommend option 3.2.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • i have a JFrame, onto which i have 2 JPanels, 1 for drawing, 1 for buttons. – Nikhar Apr 01 '12 at 05:18
  • +1 It seems that you've been using Swing for years. I just checked [this old post (from 2005)](http://www.javakb.com/Uwe/Forum.aspx/java-gui/4544/Added-JTree-to-JScrollPane-but-no-Hori-Scrollbars) yesterday, I was looking for a solution for a JTree issue and I found your name right there ;) – Eng.Fouad Apr 01 '12 at 05:20
  • Yeah, his name is on a lot of amazing Swing answers here. Another 1+. – Hovercraft Full Of Eels Apr 01 '12 at 05:22
  • And what would be that "least bandwidth intensive route possible"?? – Nikhar Apr 01 '12 at 05:31
  • @NikharSharma: a lot depends on your requirements. If you want the user to be able to access an app and use it, then Java Web Start or applets would be good as the application is downloaded to the client's machine and most work is done on the client's machine. If you want a whiteboard for collaborative work, then the complexity level and probably the band with goes up (but again, this is far afield for me). – Hovercraft Full Of Eels Apr 01 '12 at 05:38
  • @Eng.Fouad Ah yes, the 'older days'. That was actually a mirror of usenet newsgroups. I had been a regular contributor to comp.lang.java.programmer/gui/help for some time. Then Google groups came along, provided wider access to usenet & killed it with spam. :( Lucky for sites like SO. :) – Andrew Thompson Apr 01 '12 at 05:53
  • @Eng.Fouad As an aside, got curious about a link posted in the best answer in that thread by Monique Y. Mudama, which pointed to an old article at Sun that is now gone. I managed to track it down to [Effective Layout Management Short Course](http://www.cs.buu.ac.th/~seree/lecture/AWTLayoutMgr/shortcourse.html). It is a bit out of date on layouts, but seems an interesting article. – Andrew Thompson Apr 01 '12 at 06:03
4

Use Robot.createScreenCapture() and broadcast the entire teacher's screen.

to capture:

sendBuffer[index++] = robot.createScreenCapture(new Rectangle(0,0,1360,768));

to display:

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.drawImage(receiveBuffer[index], 0, 0, jFrame.getWidth(), jFrame.getHeight(), null);
    receiveBuffer[index++]=null;
}

You'll have to do rescaling in the receiver.

Java42
  • 7,628
  • 1
  • 32
  • 50
  • y do i have to send the whole screen, can't i just send the new coordinates everytime thru sockets? – Nikhar Apr 01 '12 at 05:14
  • 1
    1) A very bandwidth intensive approach. If the drawings are ..drawings, it would be a lot less to serialize & send the elements. 2) `Robot` is not needed to gain an image of a component that is in your code. See [this answer](http://stackoverflow.com/a/5853992/418556) for example. – Andrew Thompson Apr 01 '12 at 05:14
  • And if this is a local classroom environment, use RMI and have the clients pull the buffers. – Java42 Apr 01 '12 at 05:16
  • @Andrew Thompson Depends on the refresh rate. Screen capture will let teacher use all desktop tools/apps. I don't think he is after enterprise-class solution. – Java42 Apr 01 '12 at 05:20
  • how sending coordinates would be bandwidth intensive as compared to sending image? also sending image won't give a feel of drawing in real time(if i draw a line on teacher's side, the same should happen to the child's screen, live, so there is a feeling of realtime drawing..) – Nikhar Apr 01 '12 at 05:21
  • 1
    @NikharSharma - Agree, so sounds like this robot idea is not for you. Watch for other suggestions. – Java42 Apr 01 '12 at 05:29
  • +1 for both offering an approach, and then as requirements became more clear, conceding that it might not be the best approach for this use-case. – Andrew Thompson Apr 01 '12 at 05:42