www.xbdev.net
xbdev - software development
Sunday December 22, 2024
Home | Contact | Support | Slice of Java. Its simple and its powerful...
     
 

Slice of Java.

Its simple and its powerful...

 

 

 

Getting pixels from a loaded image

by Ben Kenwright


The power of the pixel is really under-estimated.  Once we can get to the pixels, we can do what we want!  Create stunning effects of numerous kinds - from water reflections to texture wrapped 3d shapes.  But we could only do this in java if we can get to those pixels.  So how are we going to do it!  Where working with java 1.1, its not exactly full of cool API's that allow us to do pixel operations....which is true...but there are still one or two methods we can use to get to our pixel data:

 

 

Running Applet:

You should really consider getting a Java capable browser... (or maybe just turning "java" on inside your browser...)

 

 

The code uses a class called Texture - which can be plugged into your own code when you need to deal with pixels and images....it leaves lots of room for expansion, but keeps things simple.

 

Applet Source Code: imagepixels.java (Download)

/*********************************************************************************************/

/*                                                                                           */

/*  Java Image Tutorials - Tut x.x                                                           */

/*  Auth: bkenwright@xbdev.net                                                               */

/*                                                                                           */

/*  Loading an image and getting at its pixels with java 1.1 and later!                      */

/*  We simple load in an image and extract the pixel data - the data is rendered to the      */

/*  screen to show its actually done what it was suppose to.                                 */

/*  We use a very simple pixel plotting code (e.g. fillRect) to draw the image pixels - even */

/*  though there are more efficent ways.

/*                                                                                           */

/*********************************************************************************************/

 

import java.awt.image.*;

import java.awt.*;

import java.applet.*;

 

/*********************************************************************************************/

/*                                                                                           */

/* class Texture                                                                             */

/* The class takes a filename, and loads the image -either jpg or a gif.  Once loaded it     */

/* extracts the pixel information into an array, so that you can access and use the pixel    */

/* information as you need - to create those special effects or demo's that require pixel    */

/* operations.                                                                               */

/*                                                                                           */

/* The class had to extend Applet so that we could use MediaTracker - also, we needed to     */

/* pass the pointer to the parent applet so that we could load the images.  As it woudln't   */

/* cooperate with 'getImage' or 'getDocumentBase()' unless we used the main applet...hence   */

/* the reason for passing the parent pointer.                                                */

/*                                                                                           */

/*********************************************************************************************/

 

class Texture extends Applet

{

   public int   Width;

       public int   Height;

   public int[] Pixels;

  

   public int Create(Applet parent, String szFileName)

   {

      // Load Image

      Image image=parent.getImage( parent.getDocumentBase(), szFileName);

      MediaTracker mediatracker = new MediaTracker(this);

      mediatracker.addImage(image, 0);

      try { mediatracker.waitForID(0); }

      catch(InterruptedException ex) {}

      // Init sprite pixels array

      Width = image.getWidth(null);

      Height = image.getHeight(null);

      Pixels = new int[Width*Height];

      // Grab pixels

      PixelGrabber pg= new PixelGrabber(image,0,0,Width,Height,Pixels,0,Width);

      try { pg.grabPixels(); }

      catch (InterruptedException e) {}

      //image=null; mediatracker=null;

      return 1;

   }// End Create(..)

  

}//End class texture

 

/*********************************************************************************************/

/*                                                                                           */

/*  Entry Class Point                                                                        */

/*  This is the class that is initialised and starts our applet                              */

/*                                                                                           */

/*********************************************************************************************/

 

public class imagepixels extends Applet

{

  Texture tex;

 

  public void init()

  {

     tex = new Texture();

     tex.Create(this, "test.jpg");

  }// End of init(..)

 

 

  public void paint(Graphics g)

  {

      int[] pixels = tex.Pixels;

          PaintTexture(g, pixels, tex.Width, tex.Height );

 

  }// End of paint(..)

 

  void PaintTexture(Graphics g, int pix[], int w, int h)

  {

     for(int y=0; y<h; y++)

        for(int x=0; x<w; x++)

        {

           int col = pix[x + y*w];

           setPixel(g, x, y, col);

        }//End loops

  }//End PaintTexture(..)

 

 

  public void setPixel(Graphics g, int x, int y, int c )

  {

      Color color = new Color( (c>>16)&0xFF,

                               (c>>8)&0xFF,

                               (c)&0xFF );

    g.setColor( color );

    g.fillRect( x, y, 1, 1 );

  }// End of setPixel(..) int

     

}// End of Applet

 

 

 
Advert (Support Website)

 
 Visitor:
Copyright (c) 2002-2024 xbdev.net - All rights reserved.
Designated articles, tutorials and software are the property of their respective owners.