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

📄 advancedspritetest.java

📁 《J2ME Game Programming》随书光盘源代码
💻 JAVA
字号:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.IOException;

/**
 * A demonstration of the ImageSet and Sprite classes.
 * @author Martin J. Wells
 */
public class AdvancedSpriteTest extends MIDlet implements CommandListener,
        Runnable
{
   private static int MINE_FRAME_WIDTH = 16;
   private static int MINE_FRAME_HEIGHT = 16;

   private MyCanvas myCanvas;
   private Command quit;
   private boolean running;
   private Sprite mineSprite1;
   private Sprite mineSprite2;
   private Sprite mineSprite3;

   /**
    * An inner class canvas used to draw the three animating mines.
    */
   class MyCanvas extends Canvas
   {
      /**
       * Paints the mine sprites onto a blank canvas.
       * @param graphics The graphics context to draw onto.
       */
      protected void paint(Graphics graphics)
      {
         // Set the color to black and draw a complete rectangle in order to
         // clear the screen.
         graphics.setColor(0);
         graphics.fillRect(0, 0, getWidth(), getHeight());

         // Draw each of the sprite objects at different positions on the
         // screen.

         // Draw the first sprite at position 25, 25.
         mineSprite1.draw(graphics, 25, 25);
         // Draw the first sprite at position 50, 50.
         mineSprite2.draw(graphics, 50, 50);
         // Draw the first sprite at position 75, 75.
         mineSprite3.draw(graphics, 75, 75);
      }
   }

   /**
    * Constructor for our demo that loads up the mine graphics and uses these
    * to create an image set and then three sprites. It them creates a canvas as
    * well as a quit command.
    */
   public AdvancedSpriteTest()
   {
      try
      {
         // Load up the mine.png image and then use extract frames to rip out
         // the frames of the image. In this code we start at position 0, 0 in
         // the mine.png (which means our mine frames are in the top left of
         // the file) and then load 3 frames across and 2 down -- a total of
         // 6 frames.
         Image[] frames = ImageSet.extractFrames(Image.createImage("/mine.png"),
                                                 0, 0, 3, 2,
                                                 MINE_FRAME_WIDTH,
                                                 MINE_FRAME_HEIGHT);
         // Create an ImageSet for the mine frames.
         ImageSet set = new ImageSet(1);

         // Set the animation speed to be 500 (so we'll animate through all 6
         // frames in half a second (500 ms).
         set.addState(frames, 500);

         // Create the first Sprite with a starting state of 0 and starting
         // frame of 0.
         mineSprite1 = new Sprite(set, 0, 0);
         // Create another sprite (using the same mine graphics - ImageSet) but
         // this time set the starting frame to 3 in order to offset the
         // animation a little. Changing the starting frame like this stops
         // all the mines animating with exactly the same frames.
         mineSprite2 = new Sprite(set, 0, 3);
         // For the last one we start at frame 5.
         mineSprite3 = new Sprite(set, 0, 5);
      }
      catch (IOException ioe)
      {
         System.out.println("unable to load image");
      }

      // Construct a the canvas.
      myCanvas = new MyCanvas();

      // And a way to quit.
      quit = new Command("Quit", Command.EXIT, 2);
      myCanvas.addCommand(quit);

      myCanvas.setCommandListener(this);

      // Create a thread to do the animation and then redraw the Canvas.
      running = true;
      Thread t = new Thread(this);
      t.start();
   }

   /**
    * Executed when we start the Thread for this class. Calls cycle on the three
    * Sprite objects as well as asking for a repaint on the Canvas.
    */
   public void run()
   {
      while (running)
      {
         myCanvas.repaint();

         // Call the cycle method in order to have it advance the animation. To
         // save on code here I'm just used a simple value of 100 to represent
         // the amount of time that has passed. This isn't accurate but it will
         // do for a demo of sprites. You should replace this with proper code
         // to track and determine the time difference between each cycle call.
         mineSprite1.cycle(100);
         mineSprite2.cycle(100);
         mineSprite3.cycle(100);

         try
         {
            Thread.sleep(100);
         }
         catch (InterruptedException e)
         {
         }

      }
   }

   /**
    * Handles Application Manager notification the MIDlet is starting (or
    * resuning from a pause). In this case we set the canvas as the current
    * display screen.
    * @throws MIDletStateChangeException
    */
   protected void startApp() throws MIDletStateChangeException
   {
      Display.getDisplay(this).setCurrent(myCanvas);
   }

   /**
    * Handles Application Manager notification the MIDlet is about to be paused.
    * We don't bother doing anything for this case.
    */
   protected void pauseApp()
   {
   }

   /**
    * Handles Application Manager notification the MIDlet is about to be
    * destroyed. We don't bother doing anything for this case.
    */
   protected void destroyApp(boolean unconditional)
           throws MIDletStateChangeException
   {
   }

   /**
    * The CommandListener interface method called when the user executes
    * a Command, in this case it can only be the quit command we created in the
    * consutructor and added to the Canvas.
    * @param command The command that was executed.
    * @param displayable The displayable that command was embedded within.
    */
   public void commandAction(Command command, Displayable displayable)
   {
      try
      {
         if (command == quit)
         {
            running = false;
            destroyApp(true);
            notifyDestroyed();
         }
      }

      catch (MIDletStateChangeException me)
      {
         System.out.println(me + " caught.");
      }
   }

}

⌨️ 快捷键说明

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