📄 335-338.html
字号:
<HTML>
<HEAD>
<META name=vsisbn content="1571690433"><META name=vstitle content="Black Art of Java Game Programming"><META name=vsauthor content="Joel Fan"><META name=vsimprint content="Sams"><META name=vspublisher content="Macmillan Computer Publishing"><META name=vspubdate content="11/01/96"><META name=vscategory content="Web and Software Development: Programming, Scripting, and Markup Languages: Java"><TITLE>Black Art of Java Game Programming:Advanced Networking and Multiplayer Gaming Concepts</TITLE>
<!-- HEADER --><STYLE type="text/css"> <!-- A:hover { color : Red; } --></STYLE><META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW"><script><!--function displayWindow(url, width, height) { var Win = window.open(url,"displayWindow",'width=' + width +',height=' + height + ',resizable=1,scrollbars=yes'); if (Win) { Win.focus(); }}//--></script><SCRIPT><!--function popUp(url) { var Win = window.open(url,"displayWindow",'width=400,height=300,resizable=1,scrollbars=yes'); if (Win) { Win.focus(); }}//--></SCRIPT><script language="JavaScript1.2"><!--function checkForQuery(fm) { /* get the query value */ var i = escape(fm.query.value); if (i == "") { alert('Please enter a search word or phrase'); return false; } /* query is blank, dont run the .jsp file */ else return true; /* execute the .jsp file */}//--></script></HEAD><BODY>
<TABLE border=0 cellspacing=0 cellpadding=0>
<tr>
<td width=75 valign=top>
<img src="../1571690433.gif" width=60 height=73 alt="Black Art of Java Game Programming" border="1">
</td>
<td align="left">
<font face="arial, helvetica" size="-1" color="#336633"><b>Black Art of Java Game Programming</b></font>
<br>
<font face="arial, helvetica" size="-1"><i>by Joel Fan</i>
<br>
Sams, Macmillan Computer Publishing
<br>
<b>ISBN:</b> 1571690433<b> Pub Date:</b> 11/01/96</font>
</td>
</tr>
</table>
<P>
<!--ISBN=1571690433//-->
<!--TITLE=Black Art of Java Game Programming//-->
<!--AUTHOR=Joel Fan//-->
<!--AUTHOR=Eric Ries//-->
<!--AUTHOR=Calin Tenitchi//-->
<!--PUBLISHER=Macmillan Computer Publishing//-->
<!--IMPRINT=Sams//-->
<!--CHAPTER=9//-->
<!--PAGES=335-338//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="331-335.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="338-340.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="CENTER"><A NAME="Heading33"></A><FONT COLOR="#000077">The finalize( ) Method</FONT></H4>
<P>sClientThread is very persistent, and will never give up unless someone else disconnects it from its Client. If this happens, finalize() is called and does some housecleaning:
</P>
<!-- CODE //-->
<PRE>
public void finalize() {
try {
ps.close();
dis.close();
theSock.close();
} catch(Exception e);
theSock = null;
}
</PRE>
<!-- END CODE //-->
<H4 ALIGN="CENTER"><A NAME="Heading34"></A><FONT COLOR="#000077">The message( ) Method</FONT></H4>
<P>Proceeding onward, we should write the message() method that gets called by sClientGroup whenever there is a message to be delivered to the client. The sClientThread doesn’t really care what the message is; whatever sClientGroup wants to send is fine with us, so we just send it verbatim. We also return <I>true</I> for success and <I>false</I> for some kind of error (just in case):</P>
<!-- CODE SNIP //-->
<PRE>
public boolean message(String str) {
try {
ps.println(str);
} catch (Exception e) {
return false;
}
return true;
}
</PRE>
<!-- END CODE SNIP //-->
<H4 ALIGN="CENTER"><A NAME="Heading35"></A><FONT COLOR="#000077">The Alias Handling Methods</FONT></H4>
<P>We are almost finished. We only need to write the last two methods, which are simple. They control the alias of the current client. This is the name the client logged in with, and it is the name the client is referred to in the user list. Here they are:
</P>
<!-- CODE SNIP //-->
<PRE>
public void setAlias(String str) {
alias = str;
}
public String getAlias() {
return alias;
}
</PRE>
<!-- END CODE SNIP //-->
<H4 ALIGN="CENTER"><A NAME="Heading36"></A><FONT COLOR="#000077">The Final Server Compile</FONT></H4>
<P>We are totally finished with the server now. If you want, you can run it by compiling all the classes and then using the <I>java</I> command. On a Windows 95 machine, it looks something like this:</P>
<!-- CODE SNIP //-->
<PRE>
c:\java\ChatServer> java ChatServer
</PRE>
<!-- END CODE SNIP //-->
<P>On UNIX, it would look like this:
</P>
<!-- CODE SNIP //-->
<PRE>
{/java/ChatServer} java ChatServer &
</PRE>
<!-- END CODE SNIP //-->
<P>Unfortunately, the server doesn’t have a client to connect to. If you are really desperate to see it do something, use Telnet to “telnet localhost 1123” and type something to the server. It should echo that to the screen, but it will only respond to the client if you issue a properly formatted command (as defined in the handleInput() method in the sClientGroup class).
</P>
<H3><A NAME="Heading37"></A><FONT COLOR="#000077">Creating the Chat Room Client</FONT></H3>
<P>Writing the chat room client is complicated. Because we are interacting with the user on this end, we have to be conscious of our user interface. This means another foray into the realm of the Abstract Windowing Toolkit (AWT). Hopefully, by now you are comfortable with AWT components, like TextFields and Buttons. In addition, we are going to have to review Lists, TextAreas, and, most importantly, Events.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading38"></A><FONT COLOR="#000077">General Design Considerations</FONT></H4>
<P>Let’s talk for a moment about how we are going to go about writing the client applet for the chat room. Its behavior is pretty well defined by the server. We already know the protocol we are going to have to follow, and the types of messages we are going to receive. Most of the processing is done by the server, so there really isn’t too much to do beyond receiving and sending specially formatted messages. This is complicated enough on its own. The network stuff can be handled in a thread, so we will definitely have to implement Runnable again.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading39"></A><FONT COLOR="#000077">User Interface Components</FONT></H4>
<P>Before any code can be written, we have to decide how the applet’s user interface will work. There are a variety of ways to do this, but for simplicity’s sake, we’ll stick with these four components. Although you’ve worked with them before, here is a little review:
</P>
<DL>
<DD><B>•</B> TextArea. A TextArea is a scrollable box that holds text. It can either be editable (meaning the user can change its contents) or not. We will use this TextArea to display all messages and chat to the user, so we don’t want it to be editable (although the user may still select and copy text to the Clipboard from it).
<DD><B>•</B> TextField. A TextField is a one-line text component. We will allow editing of it, because we are going to use it to get input from the user.
<DD><B>•</B> List. A List is a component that allows the user to view a list of Strings. The List also allows the user to select one of these Strings (and can also be configured to allow multiple selections, but we won’ be using that feature). We will use the List to display all of the current users who are logged into the chat room. By selecting a name, the user can choose to “whisper” to that person. The top item in the list will always be All Participants which, when selected, causes the user to “say” something to everyone in the room.
<DD><B>•</B> Button. There are two of these. When the applet first loads, one Button will read “login” and, when pressed, will initiate the network connection between the client and the server. The other button, which will read “logout”, will be <I>disabled</I> at the start. Buttons have two states: enabled and disabled. A Button that is disabled is grayed out and cannot be clicked. Once the connection is established, we will change the name of the first button to “say” and enable the second button. The label of the first button will vary according to the current selection in our List. When the user has selected All Participants, the Button will read “say”, but when the user selects one of the names in the List, the Button will read “whisper”.
</DL>
<P>That’s it for our Components. The next question is, of course, how are we going to lay them out?
</P>
<H4 ALIGN="LEFT"><A NAME="Heading40"></A><FONT COLOR="#000077">User Interface Design Considerations</FONT></H4>
<P>Although there are many possible combinations of Components, we will choose a relatively simple one. The scheme involves dividing the applet into two Panels, one for the “north” part and one for the “south” part of the applet window. For this, we are going to need a BorderLayout, which will handle the size and placement of the Panels. In the north Panel, we can put the TextArea and the List, in that order, with a FlowLayout. A FlowLayout is the simplest Layout, and it is also the default choice for Panels (so we don’t have to explicitly request it). Remember that all it does is place the Components in nice neat rows in the order they were added. In the south Panel, again using a FlowLayout, we put the TextField, and the two Buttons. Figure 9-4 shows what the end result will look like.
</P>
<P><A NAME="Fig4"></A><A HREF="javascript:displayWindow('images/09-04.jpg',1259,589 )"><IMG SRC="images/09-04t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/09-04.jpg',1259,589)"><FONT COLOR="#000077"><B>Figure 9-4</B></FONT></A> Picture of chat room applet with GUI layout<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="331-335.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="338-340.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</BODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -