📄 037-041.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=037-041//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="032-037.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="041-045.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading46"></A><FONT COLOR="#000077">Creating Graphics</FONT></H4>
<P>Graphics operations are performed by a Graphics object (also known as a Graphics context). Graphics is a class that’s defined in the package java.awt. Every applet has a Graphics object associated with it that addresses the screen real estate allocated for the applet. For example, the Graphics object of the applet from the preceding section paints to a rectangle that’s 113 pixels wide and 117 pixels high. Figure 1-14 illustrates the coordinate system that’s used by the Graphics object.
</P>
<P><A NAME="Fig14"></A><A HREF="javascript:displayWindow('images/01-14.jpg',318,251 )"><IMG SRC="images/01-14t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/01-14.jpg',318,251)"><FONT COLOR="#000077"><B>Figure 1-14</B></FONT></A> Coordinate system of Graphics object</P>
<P>Now let’s look at some instance methods that java.awt.Graphics provides.
</P>
<H4 ALIGN="CENTER"><A NAME="Heading47"></A><FONT COLOR="#000077">Drawing</FONT></H4>
<DL>
<DD><B>•</B> <I>drawLine(int x1, int y1, int x2, int y2)</I> draws a line between (x1,y1) and (x2,y2). To draw a single point, use <DD><B>•</B> <I>drawLine(x,y,x,y)</I>.
<DD><B>•</B> <I>drawRect(int x,int y,int w,int h)</I> draws a rectangle at (x,y) with width <I>w</I> and height <I>h</I>.
<DD><B>•</B> <I>drawOval(int x, int y,int w,int h)</I> draws an oval inside the rectangle at (x,y) with width <I>w</I> and height <I>h</I>.
</DL>
<H4 ALIGN="CENTER"><A NAME="Heading48"></A><FONT COLOR="#000077">Filling</FONT></H4>
<DL>
<DD><B>•</B> <I>fillRect(int x,int y,int w,int h)</I> fills a rectangle with the specified dimensions.
<DD><B>•</B> <I>fillOval(int x,int y,int w,int h)</I> fills an oval with the specified dimensions.
</DL>
<H4 ALIGN="CENTER"><A NAME="Heading49"></A><FONT COLOR="#000077">Color</FONT></H4>
<DL>
<DD><B>•</B> <I>setColor(Color c)</I> sets the current Color of the Graphics object. Subsequent graphics operations, such as drawing or filling, will use the given Color object. The sidebar shows a list of static constants that are defined in the Color class. To set the color to green, for example, use
</DL>
<!-- CODE SNIP //-->
<PRE>
g.setColor(Color.green); // g is a Graphics object
</PRE>
<!-- END CODE SNIP //-->
<TABLE BORDER="2" BORDERCOLOR="#0000" WIDTH="100%" ALIGN="CENTER">
<TR><TD>
<B>java.awt.Color</B>
<P>Predefined colors<TT><I>:</I></TT> (These are static constants.)</P>
<P>Grayscale<TT><I>:</I></TT> white, lightGray, gray, darkGray, black</P>
<P>Other colors<TT><I>:</I></TT> red, green, blue, yellow, magenta, cyan, pink, orange</P>
</TABLE>
<P>Here’s a little example of how to use these Graphics methods. Assume that the Graphics object <I>g</I> is passed into the paint() method:</P>
<!-- CODE //-->
<PRE>
void paint(Graphics g) {
g.setColor(Color.green);
g.drawLine(17,17,273,273);
g.drawLine(17,18,273,274);
g.setColor(Color.red);
g.fillRect(30,130,130,130);
g.setColor(Color.yellow);
g.fillOval(100,50,130,130);
}
</PRE>
<!-- END CODE //-->
<P>This little masterwork is called Composition 2. Figure 1-15 shows what it looks like.
</P>
<P><A NAME="Fig15"></A><A HREF="javascript:displayWindow('images/01-15.jpg',462,283 )"><IMG SRC="images/01-15t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/01-15.jpg',462,283)"><FONT COLOR="#000077"><B>Figure 1-15</B></FONT></A> Composition 2</P>
<P>Now it’s time to combine your knowledge of Java and graphics into your first graphics applet!
</P>
<H4 ALIGN="LEFT"><A NAME="Heading50"></A><FONT COLOR="#000077">A Graphics Applet</FONT></H4>
<P>To write an applet, you must create a subclass of the Applet class. The Applet class is part of the java.applet package. To refer to the Applet class in your program, you can use its fully qualified name, java.applet.Applet, when it’s needed. It gets pretty inconvenient to type such long names, however, so Java allows you to <I>import</I> a class or package. The <I>import</I> statement</P>
<!-- CODE SNIP //-->
<PRE>
import java.applet.Applet;
</PRE>
<!-- END CODE SNIP //-->
<P>at the start of the source file allows you to use “Applet” as a shorthand for the fully qualified name. Another form of the <I>import</I> statement,</P>
<!-- CODE SNIP //-->
<PRE>
import java.applet.*;
</PRE>
<!-- END CODE SNIP //-->
<P>allows you to use the unqualified class names for all the classes in the package java.applet<I>.</I></P>
<P>Let’s jump right in. This applet, shown in Listing 1-7, is called Mondrian.java, and you’ll soon see why.</P>
<P><B>Listing 1-7</B> Mondrian applet</P>
<!-- CODE //-->
<PRE>
import java.applet.*;
import java.awt.*;
public class Mondrian extends Applet {
public void init() {
System.out.println(">> init <<");
setBackground(Color.black);
}
public void start() {
System.out.println(">> start <<");
}
// paint squares of varying colors
public void paint(Graphics g) {
System.out.println(">> paint <<");
g.setColor(Color.yellow);
g.fillRect(0,0,90,90);
g.fillRect(250,0,40,190);
g.fillRect(80,110,100,20);
g.setColor(Color.blue);
g.fillRect(80,200,220,90);
g.fillRect(100,10,90,80);
g.setColor(Color.lightGray);
g.fillRect(80,100,110,90);
g.setColor(Color.red);
g.fillRect(200,0,45,45);
g.fillRect(0,100,70,200);
g.setColor(Color.magenta);
g.fillRect(200,55,60,135);
}
public void stop() {
System.out.println(">> stop <<");
}
public void destroy() {
System.out.println(">> destroy <<");
}
}
</PRE>
<!-- END CODE //-->
<P>The associated HTML file is shown in Listing 1-8.
</P>
<P><B>Listing 1-8</B> Mondrian applet HTML code</P>
<!-- CODE //-->
<PRE>
<html>
<body>
<title> Mondrian </title>
<applet code="Mondrian.class" width=300 height=300>
</applet>
</body>
</html>
</PRE>
<!-- END CODE //-->
<P>Since Mondrian is a subclass of Applet, it inherits all of Applet’s public methods. Mondrian redefines, or <I>overrides</I>, some of Applet’s methods—init(), start(), paint(), stop(), and destroy()—to provide the appropriate behavior. You’ll learn what these particular methods do in the following section. For now, let’s run the applet.</P>
<P>First, compile Mondrian.java with javac (as before):</P>
<!-- CODE SNIP //-->
<PRE>
%javac Mondrian.java
</PRE>
<!-- END CODE SNIP //-->
<P>Now use appletviewer to see it:
</P>
<!-- CODE SNIP //-->
<PRE>
%appletviewer Mondrian.html
</PRE>
<!-- END CODE SNIP //-->
<P>Appletviewer finds the applet tag in the HTML file, loads the Mondrian class file, and starts executing it. The result is depicted in Figure 1-16. Hmmm...maybe Mondrian <I>was</I> a genius! (In case you were wondering, Mondrian was a 20th-century artist who specialized in arranging rectangles.)</P>
<P><A NAME="Fig16"></A><A HREF="javascript:displayWindow('images/01-16.jpg',300,374 )"><IMG SRC="images/01-16t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/01-16.jpg',300,374)"><FONT COLOR="#000077"><B>Figure 1-16</B></FONT></A> Mondrian applet</P>
<P>Now let’s explore the execution of a Java applet in more detail.
</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="032-037.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="041-045.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</BODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -