⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 imageset.java

📁 J2ME Game Programming的隋书源代码
💻 JAVA
字号:
/**
 * A container for sets of image frames; typically sprites. A single set
 * is made up of one or more states. Each state represents an animation sequence
 * including Image objects for the frames, animation timing and frame dimensions.
 * <p>
 * An example use of this class would be to animate a little dude. If he had two
 * states of existence, standing (which has short breathing animation) and
 * walking (which has a much longer animation). This would be implemented by
 * creating an ImageSet object and then adding two states, each with their own
 * Image array for all the animation frames (use the static methods at the end
 * of this class to load a clipped file image and then extract the image frame
 * array from it). You can then use a Sprite class associated with this ImageSet
 * to draw the character to the screen as well as keep track of animation frames.
 * <p>
 * @see Sprite
 */

//#ifdef nokia
//# import com.nokia.mid.ui.DirectGraphics;
//# import com.nokia.mid.ui.DirectUtils;
//#endif
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Graphics;
import java.io.IOException;

public class ImageSet
{
   private int totalStates;		// incremented by addState method only

   private Image[][] stateFrames;
   private int[] stateAnimTime, stateFrameWidth, stateFrameHeight;

   public ImageSet(int numStates)
   {
      stateAnimTime = new int[numStates];
      stateFrameWidth = new int[numStates];
      stateFrameHeight = new int[numStates];
      stateFrames = new Image[numStates][];
   }

   public final void addState(Image frames[], int animTime)
   {
      int state = totalStates++;

      if (state >= stateFrames.length)
      {
         // expand the number of states
         stateAnimTime = Tools.expandArray(stateAnimTime, 1);
         stateFrameWidth = Tools.expandArray(stateFrameWidth, 1);
         stateFrameHeight = Tools.expandArray(stateFrameHeight, 1);
         stateFrames = Tools.expandArray(stateFrames, 1);
      }

      stateAnimTime[state] = animTime;
      stateFrameWidth[state] = frames[0].getWidth();
      stateFrameHeight[state] = frames[0].getHeight();
      stateFrames[state] = frames;
   }

   public final int getTotalFrames(int state)
   {
      return stateFrames[state].length;
   }

   public final int getAnimTime(int state)
   {
      return stateAnimTime[state];
   }

   public final int getAnimTimePerFrame(int state)
   {
      return stateAnimTime[state] / stateFrames[state].length;
   }

   /**
    * Draw a specific frame of this sprite onto a graphics object
    */
   public final void draw(Graphics target, int state, int frame, int targetX, int targetY)
   {
      //#ifdef debug
      if (state >= totalStates)
         System.out.println("oops, bad state " + state);

      if (frame >= stateFrames[state].length)
         System.out.println("oops, bad frame " + frame + " in draw(" +
                            frame + ", " + state + ")");
      //#endif
      if (stateFrames[state][frame] != null)
         target.drawImage(stateFrames[state][frame],
                          targetX, targetY, Tools.GRAPHICS_TOP_LEFT);
   }

   /**
    * get a specific frame
    */
   public final Image getFrame(int state, int frame)
   {
      //#ifdef debug
      if (state >= totalStates)
         System.out.println("oops, bad state " + state);

      if (frame >= stateFrames[state].length)
         System.out.println("oops, bad frame " + state + ", " + frame + " in getFrame()");
      //#endif

      return stateFrames[state][frame];
   }

   /**
    * Get all frames as a single image
    * @param framesWide
    * @return an image with all frames; used to debug image files
    */
/*	public Image getAllFrames(int framesWide)
	{
		int numLines = (totalFrames / framesWide);
		if (numLines == 0) numLines = 1;

		Image result = Image.createImage(frameWidth * framesWide, frameHeight * numLines);
		Graphics g = result.getGraphics();

		int i = 0;
		for (int fy = 0; fy < numLines; fy++)
		{
			for (int fx = 0; fx < framesWide; fx++)
			{
				draw(g, i, 0 + (frameWidth * fx), fy * frameHeight);
				if (i < totalFrames - 1)
					i++;
			}
		}
		return result;
	}
*/

   //
   // STATIC IMAGE TOOLS
   //

   public final static Image loadClippedImage(String filename, int originX, int originY, int width,
                                              int height)
   {
      try
      {
         // load full image from file and create a mutable version
         Image fileImage = Image.createImage(filename);
         return getImageRegion(fileImage, originX, originY, width, height);
      }

      catch (IOException ioe)
      {
         System.out.println("can't load file: " + filename);
         return null;
      }
   }

   public final static Image loadClippedImage(String filename, int originX, int originY)
   {
      try
      {
         // load full image from file and create a mutable version
         Image fileImage = Image.createImage(filename);

         // shortcut out of here so we can avoid creating another image
         if (originX == 0 && originY == 0) return fileImage;

         return getImageRegion(fileImage, originX, originY, fileImage.getWidth(), fileImage.getHeight());
      }

      catch (IOException ioe)
      {
         System.out.println("can't load file: " + filename);
         return null;
      }
   }

   public final static Image getImageRegion(Image source, int x, int y, int width, int height)
   {
      // create a placeholder for our resulting image region
      Image result = null;

      //#ifdef nokia
      //# result = DirectUtils.createImage(width, height, 0x00000000);
      //#else
      result = Image.createImage(width, height);
      //#endif

      if (x + width > source.getWidth() || y + height > source.getHeight())
         System.out.println("Warning: attempting extract using (" +
                            x + "," + y + "," + width + "," + height + ") when image is " +
                            "(" + source.getWidth() + "," + source.getHeight() + ")");

      // draw the image, offset by the region starting position
      result.getGraphics().drawImage(source, -x, -y, Tools.GRAPHICS_TOP_LEFT);

      return result;
   }

   public final static Image[] extractFrames(Image sourceImage, int sourceX, int sourceY,
                                             int framesWide, int framesHigh,
                                             int frameWidth, int frameHeight)
   {
      // extract all the frames from the source image
      Image[] frames = new Image[framesWide * framesHigh];
      int frameCount = 0;

      for (int fy = 0; fy < framesHigh; fy++)
         for (int fx = 0; fx < framesWide; fx++)
            frames[frameCount++] =
                    getImageRegion(sourceImage, sourceX + (fx * frameWidth),
                                   sourceY + (fy * frameHeight),
                                   frameWidth, frameHeight);
      return frames;
   }


	////////////////////////////////////////////////////////////////////////////
	//
	//	NOKIA DEVICE SPECIFIC
	//
	////////////////////////////////////////////////////////////////////////////

	//#ifdef nokia
//# 
	//# /**
	 //# * Generates the axis reflections of an array of images; for example, for
	 //# * 1 image we return the vertical reflection, for 2 we return the vertical
	 //# * and horizontal reflections, for more we return the appropriate angles
	 //# * reflected. You should never pass in a source image not representing a
    //# * direction within the 0 to 90 degrees (anything else is just a reflection
    //# * of one of these). Images are always returned in clockwise order, ie. give
    //# * me degrees 0, 45 and 90 and you'll get back images for 0, 45, 90, 135,
    //# * 180, 225, 270, 315, 360.
	 //# *
	 //# * @param source array of source images
	 //# * @return clockwise array of images (including those from the source) using axis reflection
	 //# */
	//# public final static Image[] getAxisReflections(Image[] source)
	//# {
		//# int n = source.length;
		//# if (n == 0) return null;
//# 
		//# // figure the size of the result
		//# int total = (n - 1) * 4;
		//# // special case for simple reflections
		//# if (n < 3) total = n * 2;
//# 
		//# Image[] result = new Image[total];
//# 
		//# // copy the original images to the result
		//# for (int i = 0; i < n; i++)
			//# result[i] = source[i];
//# 
		//# // mirror the vertical (0 to 180)
		//# result[total / 2] = getReflectedImage(source[0], true, false);
//# 
		//# // mirror the horizontal (90 to 270)
		//# if (n > 1)
		//# {
			//# // you can think of total/4 as 90 degrees and total/2 as 180
			//# // keep in mind we're starting at 0 for array access
			//# result[total / 2 + (total / 4)] = getReflectedImage(source[n - 1], false, true);
		//# }
//# 
		//# // mirror everything between 0 and 90 to the three other quadrants
		//# if (n > 2)
		//# {
			//# // now this gets a little messy; we need to mirror everything
			//# // in between 0 and 90 to the other quadrants. Since N > 2 we know we
			//# // have at least 1 image we need to reflect. First let's figure out how
			//# // many there are in the first quadrant, minus the two axis we already
			//# // took care of.
			//# int f = (n - 2);
//# 
			//# // now we mirror these to their opposing sides for the other 3
			//# // quadrants
			//# for (int i = 1; i <= f; i++)
			//# {
				//# result[total / 2 - i] = getReflectedImage(source[i], true, false);
				//# result[total / 2 + i] = getReflectedImage(source[i], true, true);
				//# result[total - i] = getReflectedImage(source[i], false, true);
			//# }
		//# }
		//# return result;
	//# }
//# 
	//# /**
	 //# * Return an image reflected veritcally, horizontally or both
	 //# * @param source the original image to reflect
	 //# * @param flipHorizontal carry out a horizontal flip
	 //# * @param flipVertical carry out a vertical flip
	 //# * @return the reflected image
	 //# */
	//# public final static Image getReflectedImage(Image source, boolean flipHorizontal, boolean flipVertical)
	//# {
		//# Image result = DirectUtils.createImage(source.getWidth(), source.getHeight(), 0x00000000);
		//# DirectGraphics rdg = DirectUtils.getDirectGraphics(result.getGraphics());
//# 
		//# int flipType = 0;
		//# if (flipHorizontal) flipType |= DirectGraphics.FLIP_HORIZONTAL;
		//# if (flipVertical) flipType |= DirectGraphics.FLIP_VERTICAL;
//# 
		//# rdg.drawImage(source, 0, 0, Tools.GRAPHICS_TOP_LEFT, flipType);
		//# return result;
	//# }
	//#endif


}











⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -