HOME - TOC - 1 - 2 - 3 - 4 - 5 
Lesson 05   MemoryImageSource
 
  [ MemoryImageSource ]

In this tutorial you will learn how to :
- use MemoryImageSource,
- grab pixels from an Image using PixelGrabber.

MemoryImageSource is an implementation of the ImageProducer interface which uses an array to produce pixel values for an Image. For more information take a look at sun documention in "AWT 1.1 Graphics & Image enhancements - AWT memory image dynamic update API".



[ Code ]

//  Lesson05
//  - MemoryImageSource
//  - PixelGrabber
//  This code is better viewed with tab size=4

import java.applet.Applet;
import java.awt.image.*;
import java.awt.*;

public class Lesson05 extends Applet implements Runnable
{
  int    width,height;    // applet width,height

  int    pixels[];      // pixels array
  Image  target;        // Image
  MemoryImageSource mis;    // MemoryImageSource

  Image   backImage;      // back Image
  Graphics backGraphics;    // back Graphics

  Thread  thread;        // Thread
  boolean  threadRunning;    // thread running ?

  int    spriteWidth, spriteHeight;
  int    spritePixels[];    // sprite pixels array
  int    spriteX,spriteY;  // Sprite position

  Color  txtColor;      // Text color

  public void init()
  {
    width=Integer.parseInt(getParameter("width"));
    height=Integer.parseInt(getParameter("height"));

    // Init pixels array
    pixels=new int[width*height];
    // Init MemoryImageSource - pixels will be in 32bit ARGB format
    DirectColorModel dcm=new DirectColorModel(32, 0xff0000, 0xff00, 0xff);
    mis=new MemoryImageSource(width,height,dcm,pixels,0,width);
    mis.setAnimated(true);
    mis.setFullBufferUpdates(true);
    target=createImage(mis);
    // Init double buffering
    backImage = createImage(width, height);
    backGraphics = backImage.getGraphics();

    // Load Image
    Image image=getImage(getDocumentBase(),getParameter("image"));
    MediaTracker mediatracker = new MediaTracker(this);
    mediatracker.addImage(image, 0);
    try { mediatracker.waitForID(0); }
    catch(InterruptedException ex) {}
    // Init sprite pixels array
    spriteWidth = image.getWidth(null);
    spriteHeight = image.getHeight(null);
    spritePixels = new int[spriteWidth*spriteHeight];
    // Grab pixels
    PixelGrabber pg= new PixelGrabber(image,0,0,spriteWidth,spriteHeight,spritePixels,0,spriteWidth);
    try { pg.grabPixels(); }
    catch (InterruptedException e) {}
    image=null; mediatracker=null;

    spriteX = width/2;
    spriteY = height/2;

    txtColor = Color.white;
  }

  public void start() {
    if(thread == null)
      thread = new Thread(this);
    threadRunning=true;
    thread.start();
  }

  public void run()
  {
    while (threadRunning)
    {
      try
      {
        Thread.sleep(20);            // sleep a while
      }      
      catch(InterruptedException ex) {}      // catch exception

      drawSprite(spriteX,spriteY);        // draw sprite
      repaint();                  // repaint will call update
    }
  }

  public void stop() {
    if(thread != null) {
      threadRunning=false;
      //thread.stop();      // this method has been deprecated
      thread = null;
    }
  }

  public void destroy()
  {
  }

  public void paint(Graphics g)
  {
    mis.newPixels(0,0,width,height);
    backGraphics.drawImage(target, 0, 0, this);
    backGraphics.setColor(txtColor);
    backGraphics.drawString("Lesson05",10,20);
    g.drawImage(backImage, 0, 0, this);
  }

  // Overwrite imageUpdate
  public boolean imageUpdate(Image image, int i1, int j1, int k, int i2, int j2) {
    return true;
  }

  public void update(Graphics g)
  {
    paint(g);
  }

  public void drawSprite(int x, int y) {
    int index=(y-spriteHeight/2)*width+(x-spriteWidth/2);
    for (int i=0;i     {
      for (int j=0;j       {
        try { pixels[index+j] = spritePixels[j*spriteWidth+i]; }
        catch (ArrayIndexOutOfBoundsException e) {}
      }
      index+=width;
    }
  }

  public boolean mouseEnter(Event event, int i, int j) {
    setCursor(new Cursor(Cursor.HAND_CURSOR));    // Change mouse cursor
    return true;
  }

  public boolean mouseExit(Event event, int i, int j) {
    setCursor(new Cursor(Cursor.DEFAULT_CURSOR));  // Change mouse cursor
    return true;
  }

  public boolean mouseMove(Event event, int i, int j) {
    spriteX=i;
    spriteY=j;
    return true;
  }

}

[ HTML TAG ]


<applet code="Lesson05.class" codebase = "lesson05" width="150" height="150">
<param name="image" value="lesson05/flare.jpg">
</applet>


[ Source ]

- Lesson.java

HOME - TOC - 1 - 2 - 3 - 4 - 5