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

📄 nokiagraphicstest.java

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

/**
 * A demonstration MIDlet for the pixel manipulation features in the Nokia UI.
 * @author Martin J. Wells
 */
public class NokiaGraphicsTest extends MIDlet
{
   private MyCanvas myCanvas;

   /**
    * A custom FullCanvas which renders the manipulated image.
    */
   class MyCanvas extends FullCanvas
   {
      private Image redStripeImage = null;

      /**
       * The constructor loads up the image (make sure redstripe.png in in the
       * JAR for this to work!)
       */
      public MyCanvas()
      {
         try
         {
            redStripeImage = Image.createImage("/redstripe.png");
         }
         catch (IOException ioe)
         {
            System.out.println("unable to load image");
         }
      }

      /**
       * Overriden Canvas class paint method carries out the pixel manipulation.
       * For simplicity I'm calling this code from within the paint method.
       * Normally this is going to be too slow to do in real time so you'll
       * want to pre-manipulate the images and then just render them in paint.
       * @param graphics
       */
      protected void paint(Graphics graphics)
      {
         // Get the Nokia DirectGraphics object
         DirectGraphics dg = DirectUtils.getDirectGraphics(graphics);
         graphics.setColor(0, 0, 0);
         graphics.fillRect(0, 0, getWidth(), getHeight());

         // draw the original red striped graphics
         dg.drawImage(redStripeImage, 0, 0, Graphics.TOP|Graphics.LEFT, 0);

         // Get the native pixel format
         int pixFormat = dg.getNativePixelFormat();

         // make sure we have a pixel format we know how to handle properly
         if (pixFormat == DirectGraphics.TYPE_USHORT_444_RGB)
         {
            int imgWidth=redStripeImage.getWidth();
            int imgHeight=redStripeImage.getHeight();
            short pixels[] = new short[imgWidth*imgHeight];
            dg.getPixels(pixels, 0, imgWidth, 0, 0, imgWidth, imgHeight,
                         DirectGraphics.TYPE_USHORT_444_RGB);

            // A short is a 2 byte or 16 bit type. If the format for the graphics is
            // 444 then 12 bits are used for color (the high order four bits are
            // just zero); 0000RRRRGGGGBBBB

            // To extract the red component we do a bitwise AND using
            // 0000111100000000 = 0x0F00

            for (int y=0; y < imgHeight; y++)
            {
               for (int x = 0; x < imgWidth; x++)
               {
                  int a = (y * imgWidth) + x;
                  short pixel = pixels[a];

                  // extract red, green and blue components
                  short red = (short)(pixel & 0x0F00);
                  short green = (short)(pixel & 0x00F0);
                  short blue = (short)(pixel & 0x000F);

                  if (pixel == 0x0F00)
                     pixels[a] = (short)0x00F0; // change to green
               }
            }

            dg.drawPixels(pixels, false, 0, imgWidth, 60, 0, imgWidth, imgHeight,
                          0, DirectGraphics.TYPE_USHORT_444_RGB);

         }
      }

   }

   /**
    * MIDlet constructor that just creates the custom FullCanvas.
    */
   public NokiaGraphicsTest()
   {
      myCanvas = new MyCanvas();
   }

   /**
    * 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
   {
   }
}

⌨️ 快捷键说明

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