⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 340-344.html

📁 java game programming e-book
💻 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,&nbsp;Macmillan Computer Publishing
    <br>
    <b>ISBN:</b>&nbsp;1571690433<b>&nbsp;&nbsp;&nbsp;Pub Date:</b>&nbsp;11/01/96</font>&nbsp;&nbsp;
</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=340-344//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="338-340.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="344-347.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="CENTER"><A NAME="Heading46"></A><FONT COLOR="#000077">Event Constants</FONT></H4>
<P>The list of Event.id in Table 9-1 is not totally comprehensive, but it covers almost every Event you could possibly receive. For this applet, we are only going to handle those Events caused by the Components we created as part of the graphical user interface (GUI).
</P>
<TABLE WIDTH="100%"><TR>
<CAPTION ALIGN=LEFT><B>Table 9-1</B> List of Event.id constants
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TH WIDTH="40%" ALIGN="LEFT">Constant
<TH WIDTH="60%" ALIGN="LEFT">Description
<TR>
<TD COLSPAN="2"><HR>
<TR>
<TD VALIGN="TOP">ACTION_EVENT
<TD>Generated for different reasons by different Components. For instance, it is called by TextField if the user presses the &lt;ENTER&gt; key after typing. It is also called by List when the user double-clicks on an item.
<TR>
<TD>ALT_MASK
<TD>Generated if the &lt;ALT&gt; key is down.
<TR>
<TD>CTRL_MASK
<TD>Generated if the &lt;CTRL&gt; key is down.
<TR>
<TD>DOWN
<TD>Generated by the down arrow key.
<TR>
<TD>END
<TD>Generated if the &lt;END&gt; key is pressed.
<TR>
<TD>ESC
<TD>Generated by the &lt;ESC&gt; key.
<TR>
<TD>F1 &#133; F12
<TD>Generated by the F1 through F12 function keys.
<TR>
<TD VALIGN="TOP">GOT_FOCUS
<TD>Generated when the current Component gains the focus. The &#147;focus&#148; is reserved for one Component at a time, and indicates that the current Component is being used by the user.
<TR>
<TD>HOME
<TD>Generated by the &lt;HOME&gt; key.
<TR>
<TD>KEY_ACTION
<TD>Generated when key action has occurred.
<TR>
<TD>KEY_ACTION_RELEASE
<TD>Generated when the key action has been released.
<TR>
<TD>KEY_PRESS
<TD>Generated when a key has been pressed down.
<TR>
<TD VALIGN="TOP">KEY_RELEASE
<TD>Generated when a key has been released (no longer being pressed).
<TR>
<TD>LEFT
<TD>Generated by the left arrow key.
<TR>
<TD VALIGN="TOP">LIST_DESELECT
<TD>Caused when an item in a List is deselected. This does not happen too often if you only allow one item to be selected at a time.
<TR>
<TD>LIST_SELECT
<TD>Caused when an item in a List is selected.
<TR>
<TD>LOAD_FILE
<TD>Generated when a File is loaded.
<TR>
<TD>LOST_FOCUS
<TD>Generated when a Component loses the &#147;focus.&#148;
<TR>
<TD>MOUSE_DOWN
<TD>Generated when the mouse button is pressed down.
<TR>
<TD VALIGN="TOP">MOUSE_DRAG
<TD>Generated if the mouse moves while being clicked (dragged).
<TR>
<TD>MOUSE_ENTER
<TD>Generated when the mouse enters a Component.
<TR>
<TD>MOUSE_EXIT
<TD>Generated when the mouse leaves a Component.
<TR>
<TD VALIGN="TOP">MOUSE_MOVE
<TD>Generated when the user moves the mouse without pressing the button.
<TR>
<TD>MOUSE_UP
<TD>Generated when the mouse button is released.
<TR>
<TD VALIGN="TOP">PGDN
<TD>Generated when the &lt;PAGE DOWN&gt; key is pressed.
<TR>
<TD>PGUP
<TD>Generated when the &lt;PAGE UP&gt; key is pressed.
<TR>
<TD>RIGHT
<TD>Generated when the right arrow key is pressed.
<TR>
<TD>SAVE_FILE
<TD>Generated when a file is saved by the AWT.
<TR>
<TD>SHIFT_MASK
<TD>Indicates that the &lt;SHIFT&gt; key is down.
<TR>
<TD>UP
<TD>Generated when the up arrow key is pressed.
<TR>
<TD COLSPAN="2"><HR>
</TABLE>
<H4 ALIGN="LEFT"><A NAME="Heading47"></A><FONT COLOR="#000077">Implementing the Client Chat Room Applet Code</FONT></H4>
<P>First, let&#146;s get rid of all of the mundane steps.
</P>
<H4 ALIGN="CENTER"><A NAME="Heading48"></A><FONT COLOR="#000077">The ChatClient.java Startup File</FONT></H4>
<P>Start with a file called ChatClient.java and put this skeleton in it. It includes <I>all</I> of the global variables we will eventually need. You may not recognize them all now, but don&#146;t worry, you will.</P>
<!-- CODE //-->
<PRE>
import java.applet.*;
import java.net.*;
import java.lang.*;
import java.io.*;
import java.awt.*;
import java.util.*;

public class ChatClient extends Applet &#123;

/* network stuff first */
Socket sock;
DataInputStream dis;
PrintStream ps;
String name, theHost;
int thePort;

/* now the Thread */
Thread kicker = null;

/* And finally the AWT stuff */
TextField inputField;
TextArea outputArea;
Button B1,B2;
List L;
Panel p1,p2;
&#125;
</PRE>
<!-- END CODE //-->
<H4 ALIGN="CENTER"><A NAME="Heading49"></A><FONT COLOR="#000077">Text Output</FONT></H4>
<P>In addition to all of this, let&#146;s write up a quick method that will allow us to output text to the server. This really isn&#146;t necessary at this point, but it is a good idea to have a method for this in case you ever need to send complicated messages. Don&#146;t worry too much about this code. It is only a simple output to a PrintStream. Eventually we&#146;ll replace it with something more interesting. We return <I>true</I> for success, <I>false</I> for failure. Here it is:</P>
<!-- CODE SNIP //-->
<PRE>
public boolean output(String str) &#123;
try &#123;
       ps.println(str);
       return true;
&#125; catch(Exception e) &#123;
       return false;
&#125;
&#125;
</PRE>
<!-- END CODE SNIP //-->
<H4 ALIGN="CENTER"><A NAME="Heading50"></A><FONT COLOR="#000077">GUI Setup</FONT></H4>
<P>Now let&#146;s set up the GUI. Once this step is complete, you should be able to compile the applet and play with all the neat components! However, because their Events go unhandled, they will be totally useless. Let&#146;s write the init() method now:
</P>
<!-- CODE //-->
<PRE>
public void init() &#123;

/* first, assign a BorderLayout and add the two Panels */
setLayout( new BorderLayout() );

p1 = new Panel();
p2 = new Panel();

p1.setLayout( new FlowLayout() );

add("South", p1);
add("North",p2);

/* next create the Field used for input. For fun, make it 80 columns wide.
Add it to the south Panel */
inputField = new TextField(80);
p1.add( inputField );

/* create the output Area. Make it 10 rows by 60 columns. Add it to north
Panel */
outputArea = new TextArea(10, 60);
p2.add(outputArea);

/* don't let the user edit the contents, and make the background color
Cyan - because it looks nice */
outputArea.setEditable(false);
outputArea.setBackground(Color.cyan);

/* now for the Buttons. Make the first Button to let the user "login" */
B1 = new Button("login");
p1.add(B1);

/* The second Button allows the user to "logout", but is initially
     disabled */
B2 = new Button("logout");
p1.add(B2);
B2.disable();

/* Let's create the List next. Remember that the first item is always "All
Participants" */
L = new List();
p2.add(L);
L.addItem("All Participants");

/* Let's select the first item by default */
L.select(0);

&#125;
</PRE>
<!-- END CODE //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="338-340.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="344-347.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</BODY>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -