![]() |
Languages »
Java »
General
Advanced
License: The Code Project Open License (CPOL)
Java Chat With Customizable GUIBy Jeeva SA 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
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
Note: The latest source code has been uploaded along with this article now.
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.
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 serverQUIT - Remove users from chatKICK - Kickoff from chatCHRO - Change roomMESS - Send general message PRIV - Send private messageROCO - Get users count in specified roomCALL - Request for voice chat (not included with this one)I will briefly explain the concepts behind the server side.
UserObject class which will have the client details like username, the socket of user, and the room name, etc.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());
.....
}
I will also briefly explains the concepts behind the Client side Chat.
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. 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);
}
.................
}
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.
General
News
Question
Answer
Joke
Rant
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 |