📄 373-377.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:Advanced Techniques</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=10//-->
<!--PAGES=373-377//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="368-373.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="377-380.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading20"></A><FONT COLOR="#000077">Filtering the Images</FONT></H4>
<P>Once the strip is loaded, you can extract the original bitmaps by filtering the image strip. Let’s see how to filter images using the interfaces and classes defined in<I> </I>java.awt.image. It’s not hard at all!</P>
<P>First, you need to have an object that implements the ImageProducer interface. An ImageProducer, as the name implies, is an object that can generate an image. With an ImageProducer you can create an image dynamically:</P>
<!-- CODE SNIP //-->
<PRE>
// this code appears in a Component
ImageProducer p;
... // assign an ImageProducer object to p
Image i = createImage(p);
</PRE>
<!-- END CODE SNIP //-->
<P>The Component method createImage() takes an ImageProducer argument and returns a reference to an image.
</P>
<P>Now let’s see what kinds of ImageProducers are out there. The simplest are associated with the Images themselves, and they generate the data that’s in the image. Use the Image method getSource() to get its ImageProducer:</P>
<!-- CODE SNIP //-->
<PRE>
Image i = getImage(getCodeBase(),"strip.gif");
...
ImageProducer p = i.getSource();
</PRE>
<!-- END CODE SNIP //-->
<P>This suggests a way to copy an Image: Get its ImageProducer, and then use the Component method createImage(). To continue the example above, let’s create another copy of strip.gif:
</P>
<!-- CODE SNIP //-->
<PRE>
Image j = createImage(p); // copies Image i
</PRE>
<!-- END CODE SNIP //-->
<P>Now let’s see how to filter image data. The idea is to create a new ImageProducer, which takes data from the original image (using getSource()) and processes it with an ImageFilter object. Figure 10-10 illustrates this sequence.
</P>
<P><A NAME="Fig10"></A><A HREF="javascript:displayWindow('images/10-10.jpg',600,406 )"><IMG SRC="images/10-10t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/10-10.jpg',600,406)"><FONT COLOR="#000077"><B>Figure 10-10</B></FONT></A> Filtering an Image</P>
<P>An example of an ImageFilter is CropImageFilter. This class implements a filter that crops an image to the rectangle defined in the arguments
</P>
<!-- CODE SNIP //-->
<PRE>
ImageFilter filter = new CropImageFilter(x,y,width,height);
</PRE>
<!-- END CODE SNIP //-->
<P>Next, create an ImageProducer that will generate the filtered image. To do this, construct an instance of FilteredImageSource, specifying the original ImageProducer, and the filter to be used:
</P>
<!-- CODE SNIP //-->
<PRE>
ImageProducer filterProducer =
FilteredImageSource(image.getSource(),filter);
</PRE>
<!-- END CODE SNIP //-->
<P>The last step is to create the filtered image:
</P>
<!-- CODE SNIP //-->
<PRE>
Image filteredImage = createImage(filteredProducer);
</PRE>
<!-- END CODE SNIP //-->
<P>Once you’ve created the filtered image, you can display it, or even pass it through another image filter!
</P>
<H4 ALIGN="LEFT"><A NAME="Heading21"></A><FONT COLOR="#000077">Extracting Images from an Image Strip</FONT></H4>
<P>Now you’re ready for the applet shown in Listing 10-4, which takes an image strip and creates and displays the constituent images. The dirty work’s done by the extractImages() method, which iterates through the strip and extracts each bitmap in turn.
</P>
<P><B>Listing 10-4</B> ExtractImageTest applet</P>
<!-- CODE //-->
<PRE>
// Demo of how to extract images from an image strip
import java.applet.*;
import java.awt.*;
import java.awt.image.*;
public class ExtractImageTest extends Applet {
Image strip; // the image strip
Image images[]; // the constituent images
int num_images = 0; // number of images in strip
int width = 10; // width of each image in strip
int height;
public void init() {
Image strip = getImage(getCodeBase(),"strip.gif");
// wait for image to load
// (here's how to do it without using MediaTracker)
while (strip.getWidth(this) <0);
// define number of images in strip
num_images = strip.getWidth(this)/width;
// define height of each image
height = strip.getHeight(this);
// define array of constituent images
images = new Image[num_images];
// extract constituent images
extractImages(strip,images,num_images,width,height);
}
/////////////////////////////////////////////////////////////////
// Extract the constituent images from a strip.
// There are num_images to extract, each with the
// specified width and height.
public void extractImages(Image strip,
Image images[],
int num_images,
int width,
int height) {
ImageProducer source = strip.getSource();
for (int i = 0; i<num_images; i++) {
// define filter to pull image at (i*width,0) with
// dimensions (width,height)
ImageFilter extractFilter = new CropImageFilter(i*width,
0,
width,
height);
// define producer from source and filter
ImageProducer producer =
new FilteredImageSource(source,extractFilter);
// extract the subimage!
images[i] = createImage(producer);
}
}
// display constituent images in a diagonal
public void paint(Graphics g) {
for (int i=0; i<num_images; i++) {
g.drawImage(images[i],i*width,i*13,this);
}
}
}
</PRE>
<!-- END CODE //-->
<P>You can use a method such asextractImages() to reduce the loading time of images in your applets. java.awt.image contains other filters and image producers that you can use, so be sure to check them out!
</P>
<P>Table 10-1 shows the methods we’ve covered in this section.</P>
<TABLE WIDTH="100%"><CAPTION ALIGN=LEFT><B>Table 10-1</B> Methods for image processing
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TH WIDTH="50%" ALIGN="LEFT">Class/Interface
<TH WIDTH="50%" ALIGN="LEFT">Methods
<TR>
<TD COLSPAN="2"><HR>
<TR>
<TD VALIGN="TOP">java.awt.Component
<TD>public Image createImage(ImageProducer producer);
<TR>
<TD VALIGN="TOP">java.awt.Image
<TD>public abstract ImageProducer getSource();
<TR>
<TD VALIGN="TOP">java.awt.image.CropImageFilter
<TD>public CropImageFilter(int x,int y,int w,int h); // constructor
<TR>
<TD VALIGN="TOP">java.awt.image.FilteredImageSource
<TD>public ImageFilterSource(ImageProducer orig, ImageFilter imgf); // constructor
<TR>
<TD COLSPAN="2"><HR>
</TABLE>
<P>Now, let’s talk about java.util.
</P>
<H3><A NAME="Heading22"></A><FONT COLOR="#000077">Using the java.util Classes</FONT></H3>
<P>The package java.util defines several classes that will come in handy when you’re creating games (and other applications). In fact, other classes of the API use this package as well. A quick look at the class hierarchy, shown in Figure 10-11, will tell you why.
</P>
<P><A NAME="Fig11"></A><A HREF="javascript:displayWindow('images/10-11.jpg',600,407 )"><IMG SRC="images/10-11t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/10-11.jpg',600,407)"><FONT COLOR="#000077"><B>Figure 10-11</B></FONT></A> Class hierarchy of java.util</P>
<P>In this section, you’ll see how the following classes might be used in gaming situations:
</P>
<DL>
<DD><B>•</B> Date
<DD><B>•</B> Vector
<DD><B>•</B> Stack
<DD><B>•</B> Hashtable
<DD><B>•</B> Random
</DL>
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="368-373.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="377-380.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</BODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -