A Java project allowing users to draw shapes and send messages using Sockets, Threads, ObjectInputStream, ObjectOutputStream, and JPanel
- Run
socketPainter/Hub.java
to initialize the main communication hub. - Run
socketPainter/Painter.java
and enter a username when prompted to add a Painter (user) to the chatroom. - Begin drawing shapes and/or sending chat messages! If you want to have more than one user connected at a time, simply
Run another
Painter
(ensuring that your IDE configurations allow for multiple instances of the same class to run in parallel)
This is the main ServerSocket, it keeps track of the masterCanvas (an ArrayList of all PaintingPrimitive
s drawn on the shared canvas). All communication (shapes or text) is sent to/broadcasted by the Hub.
Once the Hub is started, it will listen for new Painter
Socket connections.
When a new connection is detected, it will accept the connection, and delegate the Painter
to a new PainterThread
,
send the masterCanvas to the new Painter
, broadcast the user's arrival into the chatroom, then continue listening for new connections.
The main line of communication between the Painter
and the Hub
.
Each PainterThread
is responsible for exactly one Painter
. When an update (either a chat message of type String
or
a newly drawn shape of type PaintingPrimitive
) is received from Painter
via ObjectInputStream
, it determines the
type of update and calls Hub.broadcastMessage
or Hub.broadcastShape
accordingly.
Similarly, each PainterThread
also listens for chat and shape updates from the hub (PainterThread.chatUpdateFromHub
and PainterThread.shapeUpdateFromHub
, respectively). When an update is received, it is forwarded to its respective
Painter
via ObjectOutputStream
.
Each Painter
is a unique user in the studio. When a new Painter
is initialized, it prompts the user for a username.
If a username is entered, the Painter
will try to connect to the Hub
. If the connection is successful,
it will initialize the canvas, and begin listening for:
- MouseEvents to define the size/location
of a shape (which is sent to the
PainterThread
to be forwarded along to / broadcasted by theHub
); - ActionEvents to change this
Painter
's pen color or shape; - Updates from the
Hub
(that have been forwarded by thePainterThread
). These updates can be of typeString
(chat messages) orPaintingPrimitive
(shapes). Once an update is received, thePainter
determines the type, then either appends the chat to thefeed
or adds the shape to thepaintPanel
accordingly.
New Painter
s can see the masterCanvas upon arrival, but cannot see any of the previous chatroom messages.
- Currently, when a
Painter
disconnects, theHub
crashes. Next, I hope to implement graceful disconnect so that when aPainter
leaves the studio, theHub
acknowledges its disconnect, and the otherPainter
s can continue painting/chatting without any hiccups. - When a user is actively drawing a shape (
mouseDragged
), show a preview of the shape being drawn to only that Painter - don't send shape toPainterThread
untilmouseReleased
.