Develop Java chat software with custom GUI

Source: Internet
Author: User

Summary: This article describes in detail the development process of an AWT version of java chat software, and focuses on how to use the AWT component to implement the personalized interface of the software.

Introduction

This Java chat software is purely based on AWT components and does not use any Swing components. In this program, I developed my own Tab controls and image canvases and provided complete source code analysis. For the software implementation interface, see:

Software features

1) use this article to pass smiling face images

2) Private Chat

3) Customized color-based good interface

4) Audio Functions

Description

In the software, the server and client modules are implemented. On the server side, I defined my own RFC command. The commands used in this application include:

HELO-initialize the connection to the server

QUIT-the user exits from the chat room

KICK-Disable pre-chat

CHRO-change chat room

MESS-send Common Information

PRIV-send private information

ROCO-gets the number of chats in a specified chat room

CALL-invite voice chat (not included in this example)

Server Module Design

In this module, I want to explain the following:

· Create a custom UserObject class that contains detailed client information such as the user name, user socket, and chat room name.

· When the server is running, it activates the server socket on port 1436 (which can be modified as well) and waits for the client connection. If the customer connects to the server, it will activate an independent service thread. In this way, when the customer sends the "QUIT" command, it will turn off the thread. For details, see the detailed programming in the source file ChatCommunication. java.

The following sample code obtains the connection from the chat room client and creates a new ChatCommunication object. In the ChatCommunication class, we will create a dedicated thread to monitor all the commands from the client and make corresponding answers.

// ChatServer. java

............

While (true)

{

Socket socket = serversocket. accept ();

ChatCommunication chat = new ChatCommunication (socket );

}

.........

// ChatCommunication. java

..........

ChatCommuncation (Socket socket)

{

Personalsocket = sokcet;

Dout = new Dataoutputstream (personalsocket. getoutoutstream ());

.....

}

Client Module Design

In this module, I want to explain the following:

· When the client is running, it activates a socket and sends a helo rfc to the server to establish a connection with the ChatServer. Once connected, the client will keep the socket connection and communication with the server, no matter when the user sends the command.

· User interface design. I have created a customized Tab component and a message canvas supported by Image.

· The basic logic for creating a message canvas is simpler: No matter when a user inputs a message, I store the message in an array list. In addition, in this list, I keep the offset positions of X and Y for each message. For details, see the following source code explanation:

// Ex:

...........

For (int I = 0; I <messagearraylist. size (); I ++)

{

PaintMessageToMessageCanvas (MessageObject) messagearraylist. get (I );

}.........

/*************************************** ***********************

The following function is used to draw images and text messages.

**************************************** **********************/

Private void paintmessage1_canvas (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 the 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 the image area ********/

If (messageobject. IsImage = true)

{

Tokenizer = new StringTokenizer (Message ,"");

While (tokenizer. hasMoreTokens ())

{

TokenString = tokenizer. nextToken ();

If (TokenString. indexOf ("~~ ")> = 0)

{

/******** If the image is correct *************/

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.