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

📄 rgbimageexample.java

📁 《J2ME Game Programming》随书光盘源代码
💻 JAVA
字号:

import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.midlet.*;
import java.io.IOException;

public class RGBImageExample extends MIDlet
{
   private MyCanvas myCanvas;

   class MyCanvas extends Canvas
   {
      private Image redImage = null;
      private int[] blueRGBImage;
      private int[] greenRGBImage;

      public MyCanvas()
      {
         try
         {
            // load up the original red alien head from a resource file
            redImage = Image.createImage("/alienhead.png");

            // create new versions by flipping color bytes around
            blueRGBImage = flipImageColor(redImage, SHIFT_RED_TO_BLUE);
            greenRGBImage = flipImageColor(redImage, SHIFT_RED_TO_GREEN);

         }
         catch (IOException ioe)
         {
            System.out.println("unable to load image");
         }
      }

      protected void paint(Graphics g)
      {
         g.setColor(0, 0, 0);
         g.fillRect(0, 0, getWidth(), getHeight());

         // draw the original
         g.drawImage(redImage, 25, 50, Graphics.TOP | Graphics.LEFT);

         // draw the colour shifted images
         g.drawRGB(blueRGBImage, 0, redImage.getWidth(), 75, 50,
                   redImage.getWidth(), redImage.getHeight(), true);
         g.drawRGB(greenRGBImage, 0, redImage.getWidth(), 125, 50,
                   redImage.getWidth(), redImage.getHeight(), true);
      }

      private static final int SHIFT_RED_TO_GREEN = 0;
      private static final int SHIFT_RED_TO_BLUE = 1;
      private static final int SHIFT_GREEN_TO_BLUE = 2;
      private static final int SHIFT_GREEN_TO_RED = 3;
      private static final int SHIFT_BLUE_TO_RED = 4;
      private static final int SHIFT_BLUE_TO_GREEN = 5;

      public int[] flipImageColor(Image source, int shiftType)
      {
         // we start by getting the image data into an int array - the number
         // of 32-bit ints is equal to the width multiplied by the height
         int[] rgbData = new int[(source.getWidth() * source.getHeight())];
         source.getRGB(rgbData, 0, source.getWidth(), 0, 0, source.getWidth(),
                       source.getHeight());

         // now go through every pixel and adjust it's color
         for (int i=0; i < rgbData.length; i++)
         {
            int p = rgbData[i];

            // split out the different byte components of the pixel by applying
            // a mask so we only get what we need, then shift it to make it
            // a normal number we can play around with
            int a = ((p & 0xff000000) >> 24);
            int r = ((p & 0x00ff0000) >> 16);
            int g = ((p & 0x0000ff00) >> 8);
            int b = ((p & 0x000000ff) >> 0);

            int ba=a, br=r, bb=b, bg=g; // backup copies

            // flip the colors around according to the operation required
            switch(shiftType)
            {
               case SHIFT_RED_TO_GREEN:   g = r; r = bg; break;
               case SHIFT_RED_TO_BLUE:    b = r; r = bb; break;
               case SHIFT_GREEN_TO_BLUE:  g = b; b = bg; break;
               case SHIFT_GREEN_TO_RED:   g = r; r = bg; break;
               case SHIFT_BLUE_TO_RED:    b = r; r = bb; break;
               case SHIFT_BLUE_TO_GREEN:  b = g; g = bb; break;
            }

            // shift all our values back in
            rgbData[i] = (a << 24) + (r << 16) + (g << 8) + b;
         }

         return rgbData;
      }

   }

   public RGBImageExample()
   {
      myCanvas = new MyCanvas();
   }

   protected void startApp() throws MIDletStateChangeException
   {
      Display.getDisplay(this).setCurrent(myCanvas);
   }

   protected void pauseApp()
   {
   }

   protected void destroyApp(boolean unconditional) throws MIDletStateChangeException
   {
   }
}

⌨️ 快捷键说明

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