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

📄 633-636.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:NetOthello</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=15//-->
<!--PAGES=633-636//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="629-633.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="636-639.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading19"></A><FONT COLOR="#000077">Writing the GUI Code</FONT></H4>
<P>Let&#146;s switch gears for a while and work on the user interface for this applet. We&#146;ve already seen how the user will interact with the GameBoard, but what about getting other kinds of input and displaying other kinds of output? For this, we are going to need to do some GUI layout. There are many ways you could write a user interface for this applet, but here is a simple GUI version:
</P>
<DL>
<DD><B>&#149;</B>&nbsp;&nbsp;First, the user will be presented with a dialog box asking for his or her name.
<DD><B>&#149;</B>&nbsp;&nbsp;Once we have the user&#146;s name, we can proceed with all of the game and network parts of the game.
<DD><B>&#149;</B>&nbsp;&nbsp;While the user is playing, messages from the game and the user&#146;s opponent are displayed in a TextArea below the GameBoard.
<DD><B>&#149;</B>&nbsp;&nbsp;The user can also reply to messages from the opponent by entering them into a TextField and then pressing &lt;ENTER&gt;.
</DL>
<P>Implementing this is not very difficult, because of Java&#146;s AWT. However, to get all of the components laid out exactly the way you want is a bit tricky (unless you have an expensive Visual Java tool), and involves some hefty use of Panels. Also, we are going to have to review some things in order to implement the dialog box. First, though, let&#146;s get the easy stuff out of the way.
</P>
<H4 ALIGN="CENTER"><A NAME="Heading20"></A><FONT COLOR="#000077">NetOthello&#146;s GUI Layout</FONT></H4>
<P>All of the components are created and added to the applet in the init() method, like this:
</P>
<!-- CODE //-->
<PRE>
/* set up all GUI components */
setLayout( new BorderLayout() );
inputPanel = new Panel();
inputPanel.setLayout( new BorderLayout() );
inputPanel.add( "West", dispA=new TextArea( 5,35));
inputPanel.add( "South",new TextField(20) );
inputPanel.add("Center", turnPanel=new Panel() );
turnPanel.add( new Button("Logout") );
turnPanel.add( new Button("New Game") );
add( "South", inputPanel );
</PRE>
<!-- END CODE //-->
<P>Try compiling NetOthello now. You should recognize that something is very wrong: The board gets cut off by the inputPanel. Why? Because in paint(), we told the GameBoard that it could draw all over the full dimension of the applet. This is no longer the case. The available space for the board to be drawn has shrunk, because there is a panel obscuring part of the applet! Let&#146;s inform the GameBoard of the change like this:
</P>
<!-- CODE SNIP //-->
<PRE>
public void paint(Graphics g) &#123;
       Dimension d = size();
       d.height -= inputPanel.preferredSize().height;
       theBoard.paintBoard(g, new Rectangle( 0,0, d.width, d.height ) );
&#125;
</PRE>
<!-- END CODE SNIP //-->
<P>Now when you compile, the board should fit nicely above the panel. You may want to play around with the size of the applet so that the squares don&#146;t look like rectangles.
</P>
<H4 ALIGN="CENTER"><A NAME="Heading21"></A><FONT COLOR="#000077">Handling Events</FONT></H4>
<P>All of these components are going to generate events that must be handled by NetOthello. You should already be pretty familiar with the procedure used to do this, so just dive on in:
</P>
<!-- CODE //-->
<PRE>
public boolean handleEvent(Event evt) &#123;

/* if the user entered text in the TextField and hit enter */
if( evt.target instanceof TextField &#38;&#38; evt.id == Event.ACTION_EVENT ) &#123;
       ps.println("say|"&#43;name&#43;" says: "&#43;((TextField)evt.target).getText()&#8656;);
       ((TextField)evt.target).setText("");
       return true;
       &#125;

/* if the user clicked on the restart button */
if( evt.target instanceof Button &#38;&#38; evt.arg.equals("Restart") ) &#123;
       kicker.stop();
       newGame();
       repaint();
       return true;
       &#125;
return super.handleEvent(evt);
&#125;
</PRE>
<!-- END CODE //-->
<P>Now that the events are handled, we are almost done with the GUI code. The only thing left is the dialog box.
</P>
<H4 ALIGN="CENTER"><A NAME="Heading22"></A><FONT COLOR="#000077">Implementing a Dialog Box</FONT></H4>
<P>Java provides programmers with a Dialog class designed to be used for dialog boxes that pop up and get information from the user. Unfortunately, Dialogs can only be created by Window objects, and, at present, applets are not considered Windows. Eventually, Sun will probably add some way of getting around this, but for now, we must create our own dialog box class. This involves the use of a Frame.
</P>

<TABLE BORDER="2" BORDERCOLOR="#0000" ALIGN="CENTER">
<TR><TD><FONT SIZE="+1"><B>Frames: Windows with an Attitude</B></FONT>
<P>You may be used to using Frames to hang pictures, but in Java, the Frame class is a special subclass of Window that allows you to do some pretty nifty things. What makes a Frame so special? A Frame has a title bar, can be resized, has close/maximize/shrink buttons, can be iconified, and can hold a menu bar and menus. Frames can even control the state of the mouse cursor within them (something not even Applet can do).
</P>
</TABLE>

<P>We&#146;re not going to use Frame to its full potential, but we&#146;ll at least get our feet wet. Frame is a class that is just dying to be subclassed, and we are going to do precisely that. We are going to create a special kind of Frame, called promptFrame, which will have the singular purpose of getting a particular kind of input from the user. Here&#146;s the code:
</P>
<!-- CODE //-->
<PRE>
/* this is a simple window that gets a name from the user */
class promptFrame extends Frame &#123;
TextField TF;
public String gotName;
promptFrame() &#123;
       super("Ready to play Othello?");
       setLayout( new FlowLayout() );
       add( "West",new Label("Input your name: ") );
       add("West",TF = new TextField(20) );
       add("Center",new Button("Login") );
       gotName = null;
&#125;

public boolean handleEvent(Event evt) &#123;

if( (evt.target instanceof TextField &#38;&#38; evt.id==Event.ACTION_EVENT) ||
(evt.target instanceof Button &#38;&#38; evt.arg.equals("Login")) ) &#123;
       if( TF.getText() == "" )
              gotName = "Noname";
       else
              gotName = TF.getText();
       return true;
       &#125;
return super.handleEvent(evt);
&#125;

&#125;
</PRE>
<!-- END CODE //-->
<P>You are probably wondering what good it does to get some name from the user and then store it in a variable (which is all promptFrame does). What will eventually happen is that the Thread executing in NetOthello will take this String and dispose of the promptFrame that was created. To assist, let&#146;s add some more code to NetOthello itself. This next method will create a new promptFrame, resize it, and show it to the user:
</P>
<!-- CODE SNIP //-->
<PRE>
/* pops up a new promptFrame */
public void promptUser() &#123;

pf = new promptFrame();
pf.resize(300,100);
pf.show();
&#125;
</PRE>
<!-- END CODE SNIP //-->
<P>Let&#146;s also now rewrite the newGame() method to incorporate the promptFrame:
</P>
<!-- CODE SNIP //-->
<PRE>
public void newGame() &#123;
/* initialize the board, then pop up a window */
       initBoard();
       promptUser();
&#125;
</PRE>
<!-- END CODE SNIP //-->
<P>You can recompile at this point if you like, although you will find that it is pretty darn hard (i.e., impossible) to get rid of that prompt window. We&#146;ll remedy this later.
</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="629-633.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="636-639.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</BODY>

⌨️ 快捷键说明

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