📄 029-032.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=029-032//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="025-029.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="032-037.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading30"></A><FONT COLOR="#000077">The Java API</FONT></H4>
<P>An API, or Application Programming Interface, is the boundary between your application and the underlying programming environment. The Java API consists of standard, predefined classes that implement functionality ranging from graphics, networking, and I/O to trigonometric functions and basic data structures. As with the C/C++ libraries, the Java API provides support for features that are found beyond the core language. And since Java is an object-oriented language, you can directly extend the functionality of API classes.
</P>
<P>The classes that make up the API are grouped into packages. Below are the packages in the API.</P>
<H4 ALIGN="CENTER"><A NAME="Heading31"></A><FONT COLOR="#000077">java.applet</FONT></H4>
<P>This package contains the classes necessary for creating applets. As applets, your games can be distributed over the Web and executed in a Web page. We’ll create applets at the end of this chapter.
</P>
<H4 ALIGN="CENTER"><A NAME="Heading32"></A><FONT COLOR="#000077">java.awt</FONT></H4>
<P>The extension <I>awt</I> stands for Abstract Windowing Toolkit. The java.awt package consists of classes that allow a Java application to use the underlying window environment. Here you’ll find declarations of standard GUI widgets, such as checkboxes and scrollbars, as well as classes for handling events and graphics. You’re going to be spending a lot of time in this package, because it’s crucial to writing video games.</P>
<P>The java.awt class that handles graphics is called java.awt.Graphics, and you’ll be using it by the end of this chapter.</P>
<H4 ALIGN="CENTER"><A NAME="Heading33"></A><FONT COLOR="#000077">java.awt.image</FONT></H4>
<P>This package provides classes for performing image processing. You’ll use it in Chapter 10, Advanced Techniques.
</P>
<H4 ALIGN="CENTER"><A NAME="Heading34"></A><FONT COLOR="#000077">java.awt.peer</FONT></H4>
<P>This is a package that’s used to port the AWT to different platforms. You won’t need to use it at all.
</P>
<H4 ALIGN="CENTER"><A NAME="Heading35"></A><FONT COLOR="#000077">java.io</FONT></H4>
<P>The java.io package is the Java version of stdio.h (if you use C) or stream.h (C++). In other words, here are the classes for performing input and output. You will use this package in Chapter 8, Implementing a High Score Server on a Network.
</P>
<H4 ALIGN="CENTER"><A NAME="Heading36"></A><FONT COLOR="#000077">java.lang</FONT></H4>
<P>This package is automatically imported by the Java compiler. It contains the fundamental classes used in Java development, such as Object, Class, Thread, and Exception, as well as classes that provide access to system resources, such as System and Runtime. You will also find a Math class that handles mathematical functions, and lots of other frequently used classes.
</P>
<H4 ALIGN="CENTER"><A NAME="Heading37"></A><FONT COLOR="#000077">java.net</FONT></H4>
<P>The java.net package contains classes for interfacing with the Internet and the World Wide Web. You’ll be using it to create multiplayer games, and you’ll start exploring it in Chapter 8, Implementing a High Score Server on a Network.
</P>
<H4 ALIGN="CENTER"><A NAME="Heading38"></A><FONT COLOR="#000077">java.util</FONT></H4>
<P>In this package you’ll find declarations of basic data structures that come in really handy, such as a stack and a hashtable. You’ll learn about these classes in Chapter 10. Now let’s see how Java programs are executed.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading39"></A><FONT COLOR="#000077">The Java Interpreter</FONT></H4>
<P>Once you’ve written a Java program, you’ll want to run it! There are few steps involved. First, compile the source file with the Java compiler. The compiler produces <I>bytecode,</I> which is stored in a class file. Then, invoke the Java interpreter for this class file. You can explicitly invoke the Java interpreter from a command-line prompt, or let a Web browser do it for you. (You’ll see how this works by the end of the chapter.)</P>
<P>Then, the Java interpreter takes over. It does a few things:</P>
<DL>
<DD><B>1.</B> The appropriate class file is loaded by the <I>bytecode loader</I>. The class file can come from the local file system or from the Internet.
<DD><B>2.</B> Since the bytecode may be of unknown origin, it is checked by the <I>bytecode verifier</I>. Code that passes the tests of the verifier can be safely executed. In effect, the bytecode verifier acts as a gatekeeper, which prevents nasty, damaging programs from being executed.
<DD><B>3.</B> The successfully verified bytecode is executed by the implementation of the Java virtual machine.
</DL>
<P>These components—loader, verifier, and virtual machine—work in conjunction so that classes are dynamically loaded and linked as needed. The components of the Java interpreter are diagrammed in Figure 1-12.
</P>
<P><A NAME="Fig12"></A><A HREF="javascript:displayWindow('images/01-12.jpg',563,383 )"><IMG SRC="images/01-12t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/01-12.jpg',563,383)"><FONT COLOR="#000077"><B>Figure 1-12</B></FONT></A> The Java interpreter</P>
<P>Now let’s apply your knowledge of Java to understanding three sample programs.
</P>
<H3><A NAME="Heading40"></A><FONT COLOR="#000077">Three Sample Applications</FONT></H3>
<P>In this section, you’ll examine three programs more closely to see how Java is used. These programs are <I>applications</I>, which means they can be run directly by the Java interpreter. All applications must define a method called main(), which is where execution starts.</P>
<H4 ALIGN="LEFT"><A NAME="Heading41"></A><FONT COLOR="#000077">Program 1: Your First Java Program</FONT></H4>
<P>Here it is, shown in Listing 1-3:
</P>
<P><B>Listing 1-3</B> Fun.java</P>
<!-- CODE SNIP //-->
<PRE>
// Your First Java Program
class Fun {
public static void main(String argv[]) {
System.out.println("Java is FUN!");
}
}
</PRE>
<!-- END CODE SNIP //-->
<P>Store this short program in a file called Fun.java. The name of the file must match the name of the class. Now, compile it by typing
</P>
<!-- CODE SNIP //-->
<PRE>
% javac Fun.java
</PRE>
<!-- END CODE SNIP //-->
<P>The Java compiler, javac, produces a file that’s called Fun.class, which contains the bytecode. To run your program, type
</P>
<!-- CODE SNIP //-->
<PRE>
% java Fun
</PRE>
<!-- END CODE SNIP //-->
<P>Now java, the Java interpreter, locates the file Fun.class, and starts execution from the main() method. The result is
</P>
<!-- CODE SNIP //-->
<PRE>
Java is FUN!
</PRE>
<!-- END CODE SNIP //-->
<P>The class Fun defines a method called main(). What meaning do the three keywords in front of main() have?
</P>
<DL>
<DD><B>•</B> <I>public</I>. This is an access specifier that states that any object can invoke main().
<DD><B>•</B> <I>static</I>. This means that main() is a class method, and can be invoked even if an instance of the class isn’t allocated.
<DD><B>•</B> <I>void</I>. This means that main() doesn’t return anything.
</DL>
<P>Inside main() is the line
</P>
<!-- CODE SNIP //-->
<PRE>
System.out.println("Java is FUN!");
</PRE>
<!-- END CODE SNIP //-->
<P>System is a class that’s part of the java.lang package, and thus automatically included by the Java compiler. System has a variable <I>out</I>; <I>out</I> is an object with a method println(). This method, as its name implies, takes an argument (in this case, a String) and prints it to standard output. So the result of running this program is a reminder that you are having fun!</P>
<P>Let’s go on to the next example.</P>
<H4 ALIGN="LEFT"><A NAME="Heading42"></A><FONT COLOR="#000077">Program 2: Parameter Passing</FONT></H4>
<P>In the language overview, we touched a little on passing parameters to methods. Now you’ll see a concrete example of how this affects your programs.
</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="025-029.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="032-037.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</BODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -