|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
Note: The latest source code has been uploaded along with this article now. IntroductionThis 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 Features
DescriptionIn 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:
Server Side ModuleI will briefly explain the concepts behind the server side.
This is a sample code of getting connection from the Chat Client and creating a new object of 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 ModuleI will also briefly explains the concepts behind the Client side Chat.
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);
}
.................
}
ConclusionIn 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.
|
||||||||||||||||||||||