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

📄 canvastest.java

📁 大量j2me源代码
💻 JAVA
字号:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.Random;

/**
 * A MIDlet that demonstrates the basic use of the Canvas class by loading
 * and drawing an image. Note that for this to work you must have the
 * alienhead.png file in the directory where you execute the class.
 *
 * @author Martin J. Wells
 */
public class CanvasTest extends MIDlet implements CommandListener
{
   private MyCanvas myCanvas;
   private Command quit;
   private Command redraw;

   /**
    * An inner class used to customize a Canvas for our needs.
    */
   class MyCanvas extends Canvas
   {
      private Random randomizer = new Random();

      /**
       * @return A simple random range finder (returns a random value between
       * an upper and lower range)
       */
      private int getRand(int min, int max)
      {
         int r = Math.abs(randomizer.nextInt());
         return (r % (max - min)) + min;
      }

      /**
       * Called by the Application Manager when the Canvas needs repainting.
       * This implementation uses the drawImage method to render the previously
       * loaded alienHeadImage.
       * @param graphics The graphics context for this Canvas
       */
      protected void paint(Graphics graphics)
      {
         for (int i=10; i > 0; i--)
         {
            graphics.setGrayScale(getRand(1,254));
            graphics.fillRect(0, 0, i*(getWidth()/10), i*(getHeight()/10));
         }
      }
   }

   /**
    * Constructor for the MIDlet that firstly loads up a PNG image from a file
    * then constructs the instance of our MyCanvas class and adds a quit and
    * redraw command and sets this MIDlet to be the listener for Command events.
    */
   public CanvasTest()
   {
      // Construct a the canvas
      myCanvas = new MyCanvas();

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

      // and a redraw trigger
      redraw = new Command("Redraw", Command.SCREEN, 1);
      myCanvas.addCommand(redraw);

      myCanvas.setCommandListener(this);
   }

   /**
    * Called by the Application Manager when the MIDlet is starting or resuming
    * after being paused. In this example it acquires the current Display object
    * and uses it to set the Canvas object created in the MIDlet constructor as
    * the active Screen to display.
    * @throws MIDletStateChangeException
    */
   protected void startApp() throws MIDletStateChangeException
   {
      Display.getDisplay(this).setCurrent(myCanvas);
   }

   /**
    * Called by the MID's Application Manager to pause the MIDlet. A good
    * example of this is when the user receives an incoming phone call whilst
    * playing your game. When they're done the Application Manager will call
    * startApp to resume. For this example we don't need to do anything.
    */
   protected void pauseApp()
   {
   }

   /**
    * Called by the MID's Application Manager when the MIDlet is about to
    * be destroyed (removed from memory). You should take this as an opportunity
    * to clear up any resources and save the game. For this example we don't
    * need to do anything.
    * @param unconditional if false you have the option of throwing a
    * MIDletStateChangeException to abort the destruction process.
    * @throws MIDletStateChangeException
    */
   protected void destroyApp(boolean unconditional) throws MIDletStateChangeException
   {
   }

   /**
    * The CommandListener interface method called when the user executes
    * a Command, in this case it can be the quit command, in which case we exit
    * or the redraw command, in which case we call MyCanvas repaint method which
    * will in turn cause it to be redrawn through the implement paint method.
    * @param command
    * @param displayable
    */
   public void commandAction(Command command, Displayable displayable)
   {
      try
      {
         if (command == redraw)
         {
            // ask the canvas to redraw itself
            myCanvas.repaint();
         }

         if (command == quit)
         {
            destroyApp(true);
            notifyDestroyed();
         }
      }

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

⌨️ 快捷键说明

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