Click here to Skip to main content
6,596,602 members and growing! (21,387 online)
Email Password   helpLost your password?
Languages » Java » General     Advanced License: The Code Project Open License (CPOL)

Java Chat With Customizable GUI

By Jeeva S

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
Javascript, Java, Java, WinXP, ASP, Visual Studio, Dev
Posted:14 Jun 2002
Updated:7 May 2007
Views:305,365
Bookmarked:82 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
60 votes for this article.
Popularity: 6.25 Rating: 3.51 out of 5
10 votes, 19.2%
1
1 vote, 1.9%
2
3 votes, 5.8%
3
8 votes, 15.4%
4
30 votes, 57.7%
5
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)

About the Author

Jeeva S


Member
i am currently working as a senior software engnieer in dotnet platform (asp.net,c#,sql,vb.net) and i always loves to do think some innovative ideas and will try to implement it using the latest technologies.
Occupation: Web Developer
Location: United Arab Emirates United Arab Emirates

Other popular Java articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 177 (Total in Forum: 177) (Refresh)FirstPrevNext
Generaldocumentation on intranet chatting Pinmemberashumahakal4:10 31 Aug '09  
QuestionNeed a solution Pinmemberashu_3057:30 6 Aug '09  
Generalneed documentation Pinmembermanishdam10:54 6 Jul '09  
Questionneed documentation Pinmembermanishdam10:49 6 Jul '09  
Generalhello Pinmembershneil26219:52 11 Jun '09  
GeneralComplete chat system project PinmemberSam_Dasika19:01 3 Jun '09  
Questioncode in applet...pls... Pinmemberjayeth22:37 14 Apr '09  
AnswerRe: code in applet...pls... PinmemberDEEPIKA235:15 24 Apr '09  
Generaldocumentation of chat server Pinmemberanuradha bais7:28 14 Apr '09  
Questioncompile error... Pinmemberjayeth7:39 13 Apr '09  
Generalhello sir... Pinmemberniveditha.mm20:04 8 Apr '09  
Generalrtyer Pinmemberlienhop19:25 29 Mar '09  
GeneralHi PinmemberVerina9:59 17 Jan '09  
Generalunable to use it on a network Pinmemberyuvraj31104:12 1 Jan '09  
GeneralRe: unable to use it on a network Pinmembergaugau2:16 3 Jan '09  
GeneralRequest Pinmembersentaroso1:50 5 Dec '08  
QuestionHow to run this chat server project by using the same pc as client and server? Pinmembershurovi3:29 4 Nov '08  
QuestionHow to get Class Diagram Of Chat Server in JAVA ? PinmemberRohan_kanhai5:55 2 Nov '08  
GeneralSystem tray scrolling message application Pinmemberfred makossa7:28 28 Oct '08  
GeneralChatCommunication.java compile Pinmemberdsuazo8:50 24 Oct '08  
Generaldata flow diagram Pinmembershikhar mishra1:16 18 Oct '08  
Generalhow to run? Pinmemberniozect19:30 9 Oct '08  
Generalhow to create a java intraner chatting software Pinmembervivekanand34359:06 27 Sep '08  
QuestionCompilation of the java files Pinmembervinvishwa6:20 22 Sep '08  
AnswerRe: Compilation of the java files Pinmembershikhar mishra1:21 18 Oct '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 7 May 2007
Editor: Deeksha Shenoy
Copyright 2002 by Jeeva S
Everything else Copyright © CodeProject, 1999-2009
Web22 | Advertise on the Code Project