Click here to Skip to main content
13,664,872 members
Click here to Skip to main content
Add your own
alternative version

Stats

967.5K views
59.7K downloads
133 bookmarked
Posted 14 Jun 2002
Licenced CPOL

Java Chat With Customizable GUI

, 7 May 2007
Rate this:
Please Sign up or sign in to vote.
A complete Java (AWT) Chat Application with great customizable GUI Interface. It has features such as general chat and private chat, music when message arrives, sending images and more
Sample Image - chat.jpg

Note: The latest source code has been uploaded along with this article now.

Introduction

This is my second article on The Code Project. I have already posted Tap Control in Java here.

This Java Chat is purely AWT based, no Swing Components used and still it has a great look and feel. For this application, I have developed my own Tab Control and Image Canvas. Also, I have uploaded the complete source code here. You can download it from there.

This is the updated article . Now I have posted all the concepts behind the chat instead
of putting the source code as per the request of The Code Project members.

Features

  1. Transfer Smilies with Text
  2. Private Chat
  3. Great Look and Feel with Customized Color
  4. Audio Enabled

Description

In this Chat application, we have both server side and client side modules.. In server side, I have defined our own RFC Commands. Some of the commands which I have used in this application are listed below:

  • HELO - Initialize connection to server
  • QUIT - Remove users from chat
  • KICK - Kickoff from chat
  • CHRO - Change room
  • MESS - Send general message
  • PRIV - Send private message
  • ROCO - Get users count in specified room
  • CALL - Request for voice chat (not included with this one)

Server Side Module

I will briefly explain the concepts behind the server side.

  • Created custom UserObject class which will have the client details like username, the socket of user, and the room name, etc.
  • When the Chat Server runs, it opens the Server Socket at port 1436 (we can modify too) and listen for the client to connect. If the client connects to the server, it will open a separate thread to service. So, when the client sends QUIT command, it will close the thread too. If you take a look ChatCommunication.java, you will get all the details.

This is a sample code of getting connection from the Chat Client and creating a new object of ChatCommunication. In ChatCommunication class, we will create a thread to watch all the commands from the client and responds to the client too.

ChatServer.java
while(true
{
    Socket socket = serversocket.accept();
    ChatCommunication chat = new ChatCommunication(socket);
}
.........
         
ChatCommunication.java
..........
ChatCommuncation(Socket socket)
{
    personalsocket = socket;
    dout = new Dataoutputstream(personalsocket.getoutoutstream());
.....
}

Client Side Module

I will also briefly explains the concepts behind the Client side Chat.

  • When the Chat Client runs, it will open a socket and connect to ChatServer by sending HELO RFC to Server.. Once it gets connected, the chat client will keep the socket connection and communicate with the server whenever the user commands it.
  • Another important thing in the client module is the USER INTERFACE. I have created my OWN Custom Components like Tab Control and Image Supported Message Canvas.
  • The basic idea of creating a message canvas is based on simple logic. Whenever users enter the message, I will store it in arraylist. Also, in the Arraylist, I keep the XOffset and YOffSet position of each message. If you have a look of this sample code, you might get an idea of what I mean.
Ex:
...........
for(int i =0; i < messagearraylist.size();i++)
{
PaintMessageToMessageCanvas((MessageObject)messagearraylist.get(i);
}.........

**************************************************************
This is the Function To Paint Images and Text Messages
**************************************************************
private void PaintMessageIntoCanvas(MessageObject messageObject)
{                
int m_YPos = messageobject.StartY - YOffset;
int m_XPos = 5 - XOffset;
int CustomWidth = 0;
String Message = messageobject.Message;

/*************Print The User Name in UserName Font **************/        
if(Message.indexOf(":") >= 0)
{
graphics.setFont(UserNameFont);        
chatclient.getGraphics().setFont(UserNameFont);
fontmetrics = chatclient.getGraphics().getFontMetrics();
String m_UserName = Message.substring(0,Message.indexOf(":")+1);
graphics.drawString(m_UserName,m_XPos+CustomWidth,m_YPos);            
CustomWidth+=fontmetrics.stringWidth(m_UserName)+HorizantalSpace;
Message = Message.substring(Message.indexOf(":")+1);
}

/*********Set the Text Font **********/
chatclient.getGraphics().setFont(TextFont);
graphics.setFont(TextFont);        
fontmetrics =  chatclient.getGraphics().getFontMetrics();

/**********Print Image Area********/
if(messageobject.IsImage == true)
{
tokenizer = new StringTokenizer(Message," ");    
while(tokenizer.hasMoreTokens())
{
TokenString = tokenizer.nextToken();                
if(TokenString.indexOf("~~") >= 0)
{
/********If its a Proper Image*************/
try {
int m_ImageIndex = Integer.parseInt(TokenString.substring(2));
if((m_ImageIndex >= 0) && (m_ImageIndex < chatclient.IconCount))
{                        
graphics.drawImage(chatclient.IconArray[m_ImageIndex]
,m_XPos+CustomWidth,m_YPos - 15,messageobject.Width,messageobject.Height,this);
CustomWidth+=messageobject.Width+HorizantalSpace;                            
}    
}catch(Exception _Exc) { }                
}
else
{                    
graphics.drawString(TokenString,m_XPos+CustomWidth,m_YPos);
CustomWidth+=fontmetrics.stringWidth(TokenString)+HorizantalSpace;
}
if(TotalWidth < m_XPos+CustomWidth)
{
TotalWidth = m_XPos+CustomWidth;
scrollview.setValues(TotalWidth,TotalHeight);
}
.................
}

Conclusion

In this updated article, I have uploaded my complete source code. To download the full source code, click here.

If you still have any doubts, feel free to contact me at vavjeeva@gmail.com.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Jeeva S
Architect
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralPlease help with canvas Pin
anti_genius14-Aug-04 4:52
memberanti_genius14-Aug-04 4:52 
GeneralHelp seting up Java on my site. Pin
Anonymous26-Jun-04 10:51
sussAnonymous26-Jun-04 10:51 
Generalhelp me for a chhating program in Java Pin
aks7713-Jun-04 23:06
memberaks7713-Jun-04 23:06 
Generalis your messenger platform independent Pin
dayamkat1238-Jun-04 21:17
memberdayamkat1238-Jun-04 21:17 
Generalhelp Pin
yekita30-May-04 2:35
memberyekita30-May-04 2:35 
GeneralRun as applet Pin
elara200428-May-04 11:15
memberelara200428-May-04 11:15 
GeneralPLEASE!!! Pin
MaiTheAnh12-May-04 18:10
memberMaiTheAnh12-May-04 18:10 
Generaladd in with smiley icon program Pin
karate100226-Apr-04 21:43
memberkarate100226-Apr-04 21:43 
QuestionCan U Send me the sourcecode Pin
nhthai29-Mar-04 1:04
membernhthai29-Mar-04 1:04 
AnswerRe: Can U Send me the sourcecode Pin
Anonymous29-Mar-04 18:49
sussAnonymous29-Mar-04 18:49 
GeneralHere is how to get the source code for those of u &quot;idiots&quot; Pin
kocrotus19-Mar-04 7:51
susskocrotus19-Mar-04 7:51 
GeneralRe: Here is how to get the source code for those of u &quot;idiots&quot; Pin
Jeeva S20-Mar-04 20:59
memberJeeva S20-Mar-04 20:59 
GeneralRe: Here is how to get the source code for those of u &quot;idiots&quot; Pin
Jitendra Sachdeva24-Mar-04 15:03
memberJitendra Sachdeva24-Mar-04 15:03 
QuestionCan U Send me the sourcecode Pin
Jitendra Sachdeva13-Mar-04 5:19
memberJitendra Sachdeva13-Mar-04 5:19 
Generalmod Pin
magix5127-Mar-04 9:08
membermagix5127-Mar-04 9:08 
Generaljavax.comm serial chat Pin
cloudyz4-Feb-04 20:11
susscloudyz4-Feb-04 20:11 
GeneralRe: javax.comm serial chat Pin
Jeeva S4-Feb-04 21:16
memberJeeva S4-Feb-04 21:16 
Generaljava chat with vrml Pin
erynn7-Jan-04 17:29
susserynn7-Jan-04 17:29 
GeneralRe: java chat with vrml Pin
Anonymous23-Apr-05 7:15
sussAnonymous23-Apr-05 7:15 
Generalparameters Pin
saturn54-Dec-03 17:36
membersaturn54-Dec-03 17:36 
GeneralWhere is the code?! Pin
Albert Fret19-Nov-03 22:53
memberAlbert Fret19-Nov-03 22:53 
Generalhi Pin
ananthuluva2-Sep-03 20:42
memberananthuluva2-Sep-03 20:42 
GeneralPlease help Poor Fool out !!! Pin
Mode021489-Aug-03 17:00
memberMode021489-Aug-03 17:00 
GeneralRe: Please help Poor Fool out !!! Pin
Anonymous11-Aug-03 2:02
sussAnonymous11-Aug-03 2:02 
Generalunable 2 download Pin
pvasu31-Jul-03 3:48
memberpvasu31-Jul-03 3:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.180810.1 | Last Updated 8 May 2007
Article Copyright 2002 by Jeeva S
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid