?? ch13.htm
字號:
* @see ImageConsumer#setPixels<BR> */<BR> public void setPixels(int x, int y, intw, int h,<BR> ColorModel model, byte pixels[],<BR> int off, int scansize)<BR> {<BR> int index = y* originalSpace.width + x;<BR> int srcindex =off;<BR> int srcinc = scansize- w;<BR> int indexinc =originalSpace.width - w;<BR> for ( int dy =0; dy < h; dy++ )<BR> {<BR> for( int dx = 0; dx < w; dx++ )<BR> {<BR> inPixels[index++]= model.getRGB(pixels[srcindex++] & 0xff);<BR> }<BR> srcindex+= srcinc;<BR> index+= indexinc;<BR> }<BR> }<BR><BR> /**<BR> * Set the pixels in our image array fromthe passed<BR> * array of integers. Xlatethe pixels into our default<BR> * color model (RGB).<BR> * @see ImageConsumer#setPixels<BR> */<BR> public void setPixels(int x, int y, intw, int h,<BR> ColorModel model, int pixels[],<BR> int off, int scansize)<BR> {<BR> int index = y* originalSpace.width + x;<BR> int srcindex =off;<BR> int srcinc = scansize- w;<BR> int indexinc =originalSpace.width - w;<BR> for ( int dy =0; dy < h; dy++ )<BR> {<BR> for( int dx = 0; dx < w; dx++ )<BR> {<BR> inPixels[index++]= model.getRGB(pixels[srcindex++]);<BR> }<BR> srcindex+= srcinc;<BR> index+= indexinc;<BR> }<BR> }<BR><BR> /**<BR> * Notification that the image is completeand there will<BR> * be no further setPixel calls.<BR> * @see ImageConsumer#imageComplete<BR> */<BR> public void imageComplete(int status)<BR> {<BR> if (status ==IMAGEERROR || status == IMAGEABORTED)<BR> {<BR> consumer.imageComplete(status);<BR> return;<BR> }<BR> double point[]= new double[2];<BR> int srcwidth =originalSpace.width;<BR> int srcheight= originalSpace.height;<BR> int outwidth =rotatedSpace.width;<BR> int outheight= rotatedSpace.height;<BR> int outx, outy,srcx, srcy;<BR><BR> outPixels = newint[outwidth * outheight];<BR> outx = rotatedSpace.x;<BR> outy = rotatedSpace.y;<BR> double end[] =new double[2];<BR> int index = 0;<BR> for ( int y =0; y < outheight; y++ )<BR> {<BR> for( int x = 0; x < outwidth; x++)<BR> {<BR> //find the originalSpace point<BR> transformBack(outx+ x, outy + y, point);<BR> srcx= (int)Math.round(point[0]);<BR> srcy= (int)Math.round(point[1]);<BR><BR> //if this point is within the original image<BR> //retrieve its pixel value and store in output<BR> //else write a zero into the space. (0 alpha = transparent)<BR> if( srcx < 0 || srcx >= srcwidth ||<BR> srcy< 0 || srcy >= srcheight )<BR> {<BR> outPixels[index++]= 0;<BR> }<BR> else<BR> {<BR> outPixels[index++]= inPixels[(srcy * srcwidth) + srcx];<BR> }<BR> }<BR> }<BR> // write the entirenew image to the consumer<BR> consumer.setPixels(0,0, outwidth, outheight, defaultRGBModel,<BR> outPixels,0, outwidth);<BR><BR> // tell consumerwe are done<BR> consumer.imageComplete(status);<BR> }<BR>}</TT></BLOCKQUOTE><HR><P>The rotation is complex. First, as Figure 13.4 shows, the rotatedobject is not completely within the screen's boundary. All therotated pixels must be translated back in relation to the origin.You can do this easily by assuming that the coordinates of rotatedspace are really 0,0-the trick is how the array is populated.An iteration is made along each row in rotated space. For eachpixel in the row, the rotation is inverted. This yields the positionof this pixel within the original space. If the pixel lies withinthe original image, grab its color and store it in rotated space;if it isn't, store a transparent color.<H4>SimpleRoll Revisited</H4><P>Now redo the SimpleRoll applet to incorporate the SpinFilter andbackground image. Instead of loading the four distinct images,apply the filter to perform the rotation:<BLOCKQUOTE><TT>/**<BR> * Check for the initial image load. Once complete,<BR> * rotate the image for (90, 180, 270 & 360 degrees)<BR> * When all rotations are complete, return true<BR> * @returns true when all animation images are loaded<BR> */<BR>boolean checkRoll()<BR>{<BR> finished = false;<BR> // if we have not rotated the images yet<BR> if ( complete == false )<BR> {<BR> if ( first.checkID(0, true))<BR> {<BR> for( int x = 0; x < 4; x++ )<BR> {<BR> //Generate the angle in radians<BR> doubleamount = x * 90;<BR><BR> //Create the filter<BR> ImageFilterfilter = new SpinFilter(amount);<BR><BR> //Use the filter to get a producer<BR> ImageProducerp = new FilteredImageSource(<BR> myImage.getSource(),<BR> filter);<BR><BR> //Use the producer to create the image<BR> allImages[x]= createImage(p);<BR> tracker.addImage(allImages[x],0);<BR> }<BR> complete= true;<BR> }<BR> }<BR> // else wait for all images to generate<BR> else<BR> {<BR> finished = tracker.checkID(0,true);<BR> }<BR> return finished;<BR>}</TT></BLOCKQUOTE><P>Instead of waiting for the four individual images to load, theroutine now waits for the four rotated images to generate. Inaddition, a background image is loaded.<P>Try running the new applet, which is in the file SpinRoll.javaon the CD-ROM that comes with this book. What happened when youran it? All that flashing is a common animation problem. Don'tdespair; you can eliminate it with double buffering.<H3><A NAME="DoubleBuffering">Double Buffering</A></H3><P><I>Double buffering</I> is the single best way to eliminate imageupdate flashing. Essentially, you update an offscreen image. Whenthe drawing is complete, the offscreen image is drawn to the actualdisplay. It's called double buffering because the offscreen imageis a secondary buffer that mirrors the actual screen.<P>To create the offscreen buffer, use <TT>createImage()</TT>with only the width and height as arguments. After creating theoffscreen buffer, you can acquire a graphics context and use theimage in the same manner as <TT>paint()</TT>.Add the following lines to the <TT>init()</TT>method of the applet:<BLOCKQUOTE><TT>Image offScreenImage = createImage(this.size().width,<BR> this.size().height);<BR>Graphics offScreen = offScreenImage.getGraphics();</TT></BLOCKQUOTE><P>When the image is completely drawn, use the following line tocopy it to the real screen:<BLOCKQUOTE><TT>g.drawImage(offScreenImage, 0, 0, this);</TT></BLOCKQUOTE><P>In addition, the <TT>update()</TT>method of the component needs to be overridden in the applet.Component's version of <TT>update()</TT>clears the screen before calling <TT>paint()</TT>.The screen clear is the chief cause of flashing. Your versionof <TT>update()</TT> should just call<TT>paint()</TT> without clearingthe screen.<BLOCKQUOTE><TT>public void update(Graphics g)<BR>{<BR> paint(g);<BR>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -