Click here to Skip to main content
15,885,767 members
Articles / Programming Languages / Java / Java SE
Article

Java Chat With Customizable GUI

Rate me:
Please Sign up or sign in to vote.
4.75/5 (105 votes)
7 May 2007CPOL3 min read 1.2M   63.3K   134   259
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.

Java
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.
Java
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)


Written By
Architect
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralChatCommunication.java compile Pin
dsuazo24-Oct-08 7:50
dsuazo24-Oct-08 7:50 
Generaldata flow diagram Pin
shikhar mishra18-Oct-08 0:16
shikhar mishra18-Oct-08 0:16 
Questionhow to run? Pin
niozect9-Oct-08 18:30
niozect9-Oct-08 18:30 
Questionhow to create a java intraner chatting software Pin
vivekanand343527-Sep-08 8:06
vivekanand343527-Sep-08 8:06 
QuestionCompilation of the java files Pin
vinvishwa22-Sep-08 5:20
vinvishwa22-Sep-08 5:20 
AnswerRe: Compilation of the java files Pin
shikhar mishra18-Oct-08 0:21
shikhar mishra18-Oct-08 0:21 
Questionhow to call jsp pages using ajax Pin
karanpsp16-Sep-08 21:21
karanpsp16-Sep-08 21:21 
GeneralHi sir...I am very interested with your chat software.... I have a question... [modified] Pin
Albz Malinao12-Sep-08 20:17
Albz Malinao12-Sep-08 20:17 
Hi sir Jeeva...I am Kasie Mae Lisen, a 4th year college (Computer Science) student at University of Cebu-Banilad Campus,Cebu,Philippines. I'm currently doing my thesis proposal and my thesis is related with your chat software....I would like to ask what is the architectural design of the chat software..And how would it run...what kind of software that is capable of running that program.I need your reply as soon as possible...Hope you can help me with my thesis..

modified on Saturday, September 13, 2008 2:33 AM

QuestionThe code in VB.net Pin
danielagaba26-Aug-08 2:25
danielagaba26-Aug-08 2:25 
AnswerRe: The code in VB.net Pin
Shihab110-Dec-08 20:58
Shihab110-Dec-08 20:58 
GeneralChat server Pin
Umamadu22-Aug-08 6:39
Umamadu22-Aug-08 6:39 
Generalchat server Pin
aanchal3113-Aug-08 22:46
aanchal3113-Aug-08 22:46 
GeneralRequest Pin
Ranganayaki10-Jul-08 3:03
Ranganayaki10-Jul-08 3:03 
Generalcomplete source code Pin
sagar thareja21-Jun-08 1:54
sagar thareja21-Jun-08 1:54 
Generalrequest Pin
adike25-May-08 7:10
adike25-May-08 7:10 
GeneralProblem with your chat application Pin
maheshraju21-May-08 2:33
maheshraju21-May-08 2:33 
GeneralHello Sir Pin
suhas1216-May-08 23:56
suhas1216-May-08 23:56 
GeneralHello Sir Pin
suhas1216-May-08 23:56
suhas1216-May-08 23:56 
GeneralHello Sir Pin
suhas1216-May-08 23:43
suhas1216-May-08 23:43 
QuestionHi Sir......how it include in a web page Pin
Vinjith N.V10-Apr-08 0:26
Vinjith N.V10-Apr-08 0:26 
AnswerRe: Hi Sir......how it include in a web page Pin
Pankaj Dube4-May-08 8:02
Pankaj Dube4-May-08 8:02 
QuestionRe: Hi Sir......how it include in a web page Pin
mantas8812-Oct-08 13:22
mantas8812-Oct-08 13:22 
AnswerRe: Hi Sir......how it include in a web page Pin
vikas13001228-Jan-09 0:18
vikas13001228-Jan-09 0:18 
Generaljava chat with customizable Pin
sanju_as8-Apr-08 15:39
sanju_as8-Apr-08 15:39 
Questionif there is no net? Pin
ahmedrabea327-Mar-08 15:40
ahmedrabea327-Mar-08 15:40 

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.