📄 032-037.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:Fundamental Java</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=1//-->
<!--PAGES=032-037//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="029-032.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="037-041.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P>In Java, variables of primitive types (numeric, character, and boolean types) are <I>passed by value</I>. In other words, the value of the variable, and not the address of the variable, is passed to methods. When you pass a variable of primitive type to a method, you can be sure that the value of the variable won’t be changed by the method.</P>
<P>Arrays and objects behave differently. An array or object variable actually stores a reference to array or object contents. When you pass an array or object variable to a method, the array or object contents can be modified by the method. In effect, arrays and objects are <I>passed by reference</I>. The equivalent behavior in C is achieved by passing a pointer to the data, and dereferencing the pointer in the function body.</P>
<P>Here’s a demonstration. The program shown in Listing 1-4 illustrates the difference between passing a primitive type and passing an array.</P>
<P><B>Listing 1-4</B> Parameter passing (ParamTest.java)</P>
<!-- CODE //-->
<PRE>
// Parameter Passing Comparison
class ParamTest {
// method foo illustrates passing a variable of primitive type
static void foo (int bar) {
bar++;
}
// method foo2 illustrates passing an array
static void foo2 (int a[]) {
a[0] = 1;
}
// this method does the test
static public void main(String argv[]) {
int x; // x is int;
x = 3; // x is assigned 2
foo(x); // pass x to foo()
System.out.println("x = " + x);
// x is unchanged!!!!
int y[]; // y refers to array
y = new int[2]; // y is allocated 2 ints
y[0] = 17; // y[0] is assigned 17
foo2(y); // pass array variable y to foo2()
System.out.println("y[0] = " + y[0]);
// y[0] is changed!!!!
}
}
</PRE>
<!-- END CODE //-->
<P>After compiling and running this class, the output will be
</P>
<!-- CODE SNIP //-->
<PRE>
x = 3
y[0] = 1
</PRE>
<!-- END CODE SNIP //-->
<P>Thus, <I>x</I> hasn’t been modified, as the <I>value</I> of <I>x</I> is passed to method foo(). On the other hand, <I>y</I> is an array, so the contents of <I>y</I> can be modified by method foo2().</P>
<P>Now for our final example.</P>
<H4 ALIGN="LEFT"><A NAME="Heading43"></A><FONT COLOR="#000077">Program 3: A Linked List</FONT></H4>
<P>Java, unlike C and C++, has no explicit pointers. This not only has an effect on the way parameters are passed, as you saw in the previous example, but also on the declaration of data structures that use pointers, such as <I>linked lists</I> or <I>binary trees</I>.</P>
<P>For example, consider the problem of defining a linked list of integers. A linked list is a data structure that stores a sequence of elements that can grow or shrink dynamically. Each <I>node</I> in the list contains an integer and a pointer to the next node. The diagram in Figure 1-13 illustrates a circular linked list, where the end of the list points to the front.</P>
<P>In Java, a node of a linked list may be defined like this:</P>
<!-- CODE SNIP //-->
<PRE>
class Node {
private int value; // stores int
private Node next; // refers to the next Node
...
}
</PRE>
<!-- END CODE SNIP //-->
<P>A node variable is a reference handle to the contents of the node, so this definition works. Now, let’s define, in Listing 1-5, the circular linked list that’s shown in Figure 1-13:
</P>
<P><B>Listing 1-5</B> Circular linked list (CircularList.java)</P>
<!-- CODE //-->
<PRE>
// a single node of the list
class Node {
private int value;
private Node next;
// Node constructor
public Node(int i) {
value = i; // initialize value
next = null; // no next node
}
// print value stored in this node
public void print() {
System.out.println(value);
}
// get handle to next node
public Node next() {
return next;
}
// set the next node
public void setNext(Node n) {
next = n;
}
}
// define the circular list in Figure 1-12
class CircularList {
public static void main(String argv[]) {
Node a; // declare nodes
Node b;
Node c;
a = new Node(1); // allocate nodes
b = new Node(2);
c = new Node(3);
a.setNext(b); // create circular list
b.setNext(c);
c.setNext(a);
Node i;
i = a;
while (i != null) { // print circular list
i.print(); // print value in node i
i = i.next(); // set i to next node
}
}
}
</PRE>
<!-- END CODE //-->
<P><A NAME="Fig13"></A><A HREF="javascript:displayWindow('images/01-13.jpg',460,315 )"><IMG SRC="images/01-13t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/01-13.jpg',460,315)"><FONT COLOR="#000077"><B>Figure 1-13</B></FONT></A> Circular linked list</P>
<P>Upon compiling and running CircularList.java, the output will be an infinite sequence 1, 2, 3, 1, 2, 3, 1, and so on, which means there’s a circular list. We’ll stay away from using linked lists in writing games, since arrays are simpler and faster. But the point of this program is to show that the elimination of pointers from Java doesn’t prevent you from coding data structures that use them.
</P>
<P>Now that you’ve seen Java in action, it’s time write a graphics applet. For this, you’ll need to use the class java.awt.Graphics.</P>
<H3><A NAME="Heading44"></A><FONT COLOR="#000077">Understanding Applets</FONT></H3>
<P>In this section you will learn all about applets: executing an applet, creating graphics in an applet, and the applet life cycle. At the end, you’ll write your own graphics applet!
</P>
<H4 ALIGN="LEFT"><A NAME="Heading45"></A><FONT COLOR="#000077">Executing an Applet</FONT></H4>
<P>An <I>applet</I>, as you’ll recall, is a Java program that executes in conjunction with a Web browser, or <I>appletviewer</I> (a program that runs applets). Here are the steps needed to execute an applet you’ve written:</P>
<DL>
<DD><B>1.</B> Compile the applet source file with javac, the Java compiler. The result is a class file.
<DD><B>2.</B> Create an HTML file with an applet tag that refers to the class file.
<DD><B>3.</B> Invoke appletviewer (or the browser) on the HTML file.
</DL>
<P>For example, the minimal HTML document shown in Listing 1-6 refers to applet code found in Example.class.
</P>
<P><B>Listing 1-6</B> Sample HTML file for an applet</P>
<!-- CODE SNIP //-->
<PRE>
<html>
<body>
<applet code="Example.class" width=113 height=117>
</applet>
</body>
</html>
</PRE>
<!-- END CODE SNIP //-->
<P>The applet tag tells the Web browser (or appletviewer) the dimensions, in pixels, required by the applet. After the browser loads the HTML file, it fetches the applet, places the applet on the screen with the desired dimensions, and executes the applet. In this particular case, the browser will set aside a 113x117 swatch of screen real estate.
</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="029-032.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="037-041.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</BODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -