A Java project allowing users to draw shapes and send messages using Sockets, Threads, ObjectInputStream, ObjectOutputStream, and JPanel
- Run
socketPainter/Hub.javato initialize the main communication hub. - Run
socketPainter/Painter.javaand 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 PaintingPrimitives 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
PainterThreadto 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, thePainterdetermines the type, then either appends the chat to thefeedor adds the shape to thepaintPanelaccordingly.
New Painters can see the masterCanvas upon arrival, but cannot see any of the previous chatroom messages.
- Currently, when a
Painterdisconnects, theHubcrashes. Next, I hope to implement graceful disconnect so that when aPainterleaves the studio, theHubacknowledges its disconnect, and the otherPainters 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 toPainterThreaduntilmouseReleased.