时间: 2021-07-31 作者:daque
纲要:正文精细引见一个awt本子的java谈天软硬件的开拓进程,并提防领会怎样用awt组件实行软硬件的天性化界面。
简介
本java谈天软硬件,简单鉴于awt组件,没有运用任何swing组件。在本步调中,我开拓了本人的tab控件和图像画布并给出完备的源码领会。软硬件实行界面见下图:
软硬件特性
1)用正文传播笑容图片
2)个人谈天功效
3)鉴于定制脸色的杰出界面
4)音频功效
刻画
在软硬件中,实行了效劳器端模块与存户端模块。在效劳器端,我设置了本人的rfc吩咐。在该运用步调顶用到的吩咐有:
helo - 初始化到效劳器的贯穿
quit - 用户退出谈天室
kick - 遏止参预谈天
chro - 调换谈天室
mess - 发送普遍消息
priv - 发送个人消息
roco - 博得指定谈天室的谈天人头
call - 恭请语音谈天(正文示例中没有囊括)
效劳器端模块安排
在本模块中,我想证明以次几点:
·创造一个定制的userobject类,该类包括了如用户名,用户套接字,谈天室称呼之类精细的存户端消息。
·当效劳器端运转时,它在端口1436(固然咱们也不妨窜改)开明效劳器端套接字,听候存户端的贯穿。即使存户贯穿上效劳器,它将开明一个独力的效劳线程。如许,当存户发送"quit"吩咐时,它就会关掉该线程。详见源文献chatcommunication.java中的详细编制程序。
底下的示例代码实行了从谈天室存户端获得贯穿并创造一个新的chatcommunication东西的功效。在类chatcommunication中,咱们将创造一个特意线程来控制监守来自存户端的一切吩咐并作出相映的回复。
//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());
.....
}
存户端模块安排
在本模块中,我想证明以次几点:
·当存户端运转时,它开明一个套接字并经过发送 helo rfc给效劳器与chatserver 创造贯穿。一旦贯穿上,存户端将从来维持该套接字的贯穿和与效劳器的通讯,不管用户在什么功夫发送吩咐。
·对于用户接口的安排。我创造了定制的tab组件和image扶助的动静画布。
·创造动静画布的基础论理较大略些:不管用户什么功夫输出动静,我都把该动静保存到一个数组列表中。并且,在这个列表中,我保持了每条动静的x和y的偏移场所。详见底下的源码证明:
//ex:
...........
for(int i =0; i < messagearraylist.size();i++)
{
paintmessagetomessagecanvas((messageobject)messagearraylist.get(i);
}.........
/**************************************************************
底下因变量用来绘绘图像和文本动静
**************************************************************/
private void paintmessageintocanvas(messageobject messageobject)
{
int m_ypos = messageobject.starty - yoffset;
int m_xpos = 5 - xoffset;
int customwidth = 0;
string message = messageobject.message;
/*************以用户名字体打字与印刷出用户名 **************/
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);
}
/*********树立文本的字体**********/
chatclient.getgraphics().setfont(textfont);
graphics.setfont(textfont);
fontmetrics = chatclient.getgraphics().getfontmetrics();
/**********打字与印刷图像地区********/
if(messageobject.isimage == true)
{
tokenizer = new stringtokenizer(message," ");
while(tokenizer.hasmoretokens())
{
tokenstring = tokenizer.nexttoken();
if(tokenstring.indexof("~~") >= 0)
{
/********即使是精确的图像*************/
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);
}
.................
}