📄 132-136.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:Adding Interactivity</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=4//-->
<!--PAGES=132-136//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="127-132.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="136-141.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P>As a String object, <I>helloString</I> can use all the methods defined in java.lang.String. The String class provides a lot of the functionality found in the C library strings.h, such as length(), which returns the length of the String, and substr(), which pulls out a substring. Furthermore, the + operator concatenates String arguments, as you have seen. Look in Appendix A, Java Reference Tables, for a list of String methods.</P>
<H4 ALIGN="LEFT"><A NAME="Heading14"></A><FONT COLOR="#000077">Choosing Fonts</FONT></H4>
<P>The font determines how the String will appear on the screen. To specify a certain font, point size, and font style (plain, bold, italic, bold + italic) you must create a Font object. For example, the following defines a Font object of Courier, 14-point bold and italic:
</P>
<!-- CODE SNIP //-->
<PRE>
Font courierFont =
new Font("Courier",Font.BOLD+Font.ITALIC,14);
</PRE>
<!-- END CODE SNIP //-->
<TABLE BORDER="2" BORDERCOLOR="#0000" ALIGN="CENTER">
<TR><TD>
<P><FONT SIZE="+1"><B>Fonts and Font Styles Supported by Java:</B></FONT></P>
<P>Fonts: Courier, Dialog, DialogInput, Helvetica, TimesRoman, ZapfDingbats.
</P>
<P>Font styles: These are static constants defined in the java.awt.Font class: Font.PLAIN, Font.ITALIC, Font.BOLD. If you need both bold and italic, use Font.ITALIC + Font.BOLD.</P>
</TABLE>
<P>If your applet picks a font that is not supported by the platform, Java uses a default font.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading15"></A><FONT COLOR="#000077">Drawing Strings</FONT></H4>
<P>There are two steps here. First, you should set the font of the graphics context, like this:
</P>
<!-- CODE SNIP //-->
<PRE>
g.setFont(courierFont);
</PRE>
<!-- END CODE SNIP //-->
<P>Then you can draw <I>helloString</I> at the given width and height:</P>
<!-- CODE SNIP //-->
<PRE>
g.drawString(helloString,width,height);
</PRE>
<!-- END CODE SNIP //-->
<P>A lot of times, you’ll want to justify the text. To do this, use the class java.awt.FontMetrics. A FontMetrics object can tell you how much space a String will take, for a given Font. For example:
</P>
<!-- CODE SNIP //-->
<PRE>
g.setFont(myFavoriteFont);
FontMetrics m = g.getFontMetrics();
int stringWidth = m.stringWidth(helloString);
</PRE>
<!-- END CODE SNIP //-->
<P><I>stringWidth</I> now contains the width, in pixels, of <I>helloString</I>.</P>
<P>Let’s illustrate all this in an applet.</P>
<H4 ALIGN="LEFT"><A NAME="Heading16"></A><FONT COLOR="#000077">Inserting a Text String into an Applet</FONT></H4>
<P>If you ran the MouseTest applet, you noticed that the applet was completely blank. Now we will display a little reminder of what the applet is supposed to do at the center of the applet.
</P>
<P>The new version of the MouseTest applet is shown in Listing 4-2. It also uses the Applet method showStatus() to display the mouse event at the bottom of the applet window.</P>
<P><B>Listing 4-2</B> Revised MouseTest applet</P>
<!-- CODE //-->
<PRE>
import java.applet.*;
import java.awt.*;
public class MouseTest2 extends Applet {
Font courierFont;
String testString = "Test the mouse in here!";
public void init() {
courierFont = new Font("Courier",Font.BOLD+Font.ITALIC,24);
}
public void paint(Graphics g) {
g.setFont(courierFont);
// center the string
FontMetrics m = g.getFontMetrics();
int stringWidth = m.stringWidth(testString);
int width = (bounds().width - stringWidth )/2;
int height = bounds().height / 2;
// draw the string
g.setColor(Color.green);
g.drawString(testString,width,height);
}
public boolean mouseDown(Event e,int x,int y) {
showStatus("mouseDown at (" + x + "," + y + ")" );
return true;
}
public boolean mouseUp(Event e,int x,int y) {
showStatus("mouseUp at (" + x + "," + y + ")" );
return true;
}
public boolean mouseMove(Event e,int x,int y) {
showStatus("mouseMove at (" + x + "," + y + ")" );
return true;
}
public boolean mouseDrag(Event e,int x,int y) {
showStatus("mouseDrag at (" + x + "," + y + ")" );
return true;
}
public boolean mouseEnter(Event e,int x,int y) {
showStatus("mouseEnter at (" + x + "," + y + ")" );
return true;
}
public boolean mouseExit(Event e,int x,int y) {
showStatus("mouseExit at (" + x + "," + y + ")" );
return true;
}
}
</PRE>
<!-- END CODE //-->
<P>The message
</P>
<!-- CODE SNIP //-->
<PRE>
Test the mouse in here!
</PRE>
<!-- END CODE SNIP //-->
<P>will display in the center of the applet, as Figure 4-6 shows.
</P>
<P><A NAME="Fig6"></A><A HREF="javascript:displayWindow('images/04-06.jpg',350,424 )"><IMG SRC="images/04-06t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/04-06.jpg',350,424)"><FONT COLOR="#000077"><B>Figure 4-6</B></FONT></A> Revised MouseTest applet</P>
<P>Now let’s see how you can control sprites with the mouse. In the next section, we will build an applet that lets you drag and move a rectangle.
</P>
<H3><A NAME="Heading17"></A><FONT COLOR="#000077">Clicking and Dragging Sprites</FONT></H3>
<P>Clicking and dragging an icon is a common activity in any GUI. First, the user selects the icon by clicking the mouse on it. Then, the icon moves with the mouse, as long as the button stays down. Figure 4-7 illustrates the steps involved.
</P>
<P><A NAME="Fig7"></A><A HREF="javascript:displayWindow('images/04-07.jpg',468,398 )"><IMG SRC="images/04-07t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/04-07.jpg',468,398)"><FONT COLOR="#000077"><B>Figure 4-7</B></FONT></A> Clicking and dragging</P>
<P>Now you’ll see one way of doing this in Java.
</P>
<P>First, let’s define, in Listing 4-3, a DragRect class, which will be used as the “icon” in this applet. The DragRect inherits from RectSprite, which was defined in the previous chapter. It defines an additional boolean called <I>draggable</I>, which records if the DragRect can be moved or not. Since <I>draggable</I> is <I>protected</I>, we will include the usual accessor methods.</P>
<P><B>Listing 4-3</B> DragRect class</P>
<!-- CODE //-->
<PRE>
/////////////////////////////////////////////////////////////////
class DragRect extends RectSprite {
protected boolean draggable; // is rectangle draggable?
// accessor methods: modify draggable
public void setDraggable(boolean b) {
draggable = b;
}
// return draggable
public boolean isDraggable() {
return draggable;
}
</PRE>
<!-- END CODE //-->
<P>Now, when does the rectangle become draggable? When the user clicks inside the rectangle. This means we need a method to check if an arbitrary point, (x,y), is inside the DragRect:
</P>
<!-- CODE SNIP //-->
<PRE>
// check if (x,y) is inside rectangle
public boolean inside(int x,int y) {
return (locx <= x && locy <= y &&
(locx + width >= x) &&
(locy + height >= y));
}
</PRE>
<!-- END CODE SNIP //-->
<P>Once the user selects the rectangle, it can move along with the mouse. The following method translates the rectangle by the specified amount:
</P>
<!-- CODE SNIP //-->
<PRE>
public void translate(int x,int y) {
locx += x;
locy += y;
}
</PRE>
<!-- END CODE SNIP //-->
<P>Finally, DragRect needs a constructor:
</P>
<!-- CODE SNIP //-->
<PRE>
public DragRect(int x,int y,int w,int h,Color c) {
super(x,y,w,h,c);
fill = true;
draggable = false; // initially not draggable
}
}
</PRE>
<!-- END CODE SNIP //-->
<P>This constructor calls the RectSprite constructor first, before setting the booleans.
</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="127-132.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="136-141.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</BODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -