Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Sams Teach Yourself Visual J++ 6 in 21 Days
(Publisher: Macmillan Computer Publishing)
Author(s): Rick Leinecker
ISBN: 0672313510
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


The code that gets the pixel data from the image is shown in Listing 13.11.

Listing 13.11 Getting the Pixel Data

1   // Declare the pixel array.
2   int m_nProcessedPixels[];
3
4   public void pullPixels()
5   {
6
7       // Call the method that creates the pixel array.
8       m_nProcessedPixels = getPixels( m_ProcessedImage );
9
10  }
11
12  public int[] getPixels( Image img )
13  {
14
15      // Get the width and height of the image.
16      int nWidth = img.getWidth( this );
17      int nHeight = img.getHeight( this );
18      // Calculate the number of pixels.
19      int nNumPixels = nWidth * nHeight;
20
21      // Create the array of integers for the pixel data.
22      int nRawPixels[] = new int[nNumPixels];
23      if( nRawPixels == null )
24          return( null );
25
26      // Create a PixelGrabber class.
27      PixelGrabber Grabber =
28          new PixelGrabber( img, 0, 0, nWidth, nHeight, nRawPixels, 0,
             ⇒nWidth );
29
30      try
31      {
32          // Get the pixels into the integer array.
33          Grabber.grabPixels();
34      }
35      catch( InterruptedException e )
36      {
37      }
38
39      // Return the array.
40      return( nRawPixels );
41
42  }

The pullPixels() method contains code similar to what we developed earlier in this chapter. Above it, an integer array named m_nProcessedPixels is declared. This array will contain the pixel data form the m_ProcessedImage and then the data after it’s been processed.

The pullPixels() method actually just calls the getPixels() method. But it passes the m_ProcessedImage to the getPixels() method and is returned an integer array.

The getPixels() array finds the width and height of the image. It then calculates the number of pixels in the image by multiplying the width by the height.

A local variable named nRawPixels is allocated so that it has enough space to hold the image pixel data. A PixelGrabber class is created. Then the PixelGrabber’s grabPixels() method is called. This method copies all the Image class pixel data into the array. The integer array is then returned to the pullPixels() method.

Putting the pixels back is the next chunk of code we’ll tackle. The pushPixels() and storePixels() methods make it all work. They are shown in Listing 13.12.

Listing 13.12 Storing the Pixel Data

1   public void pushPixels()
2   {
3
4       // Call the method that creates the new Image class.
5       m_ProcessedImage = storePixels( m_ProcessedImage,
        ⇒m_nProcessedPixels );
6
7   }
8
9   public Image storePixels( Image img, int nRawPixels[])
10  {
11
12      // Get the width and height of the image.
13      int nWidth = img.getWidth( this );
14      int nHeight = img.getHeight( this );
15      // Calculate the number of pixels.
16      int nNumPixels = nWidth * nHeight;
17
18      // Get the ColorModel object.
19      ColorModel cm = ColorModel.getRGBdefault();
20
21      // Create the MemoryImageSource object.
22      MemoryImageSource ImageSource =
23          new MemoryImageSource( nWidth, nHeight, cm, nRawPixels, 0,
            ⇒nWidth );
24
25      // Create the image.
26      img = createImage( ImageSource );
27
28      // Wait for the image to be created.
29      MediaTracker Tracker = new MediaTracker( this );
30      // Add the image to the MediaTracker.
31      Tracker.addImage( img, 0 );
32      try
33      {
34          // Wait for the completion of the image.
35          Tracker.waitForID( 0 );
36      }
37      catch( InterruptedException e )
38      {
39      }
40
41      return( img );
42
43  }

The pushPixels() and storePixels() methods are the last of the generic code shown in Listing 13.12. The pushPixels() method simply calls the storePixels() method with two arguments: an Image class and an array of integers. The storePixels() method returns an Image class.

The storePixels() array finds the width and height of the image. It then calculates the number of pixels in the image by multiplying the width by the height.

A ColorModel class is obtained via a call to the getRGBdefault() method. A MemoryImageSource class is created. This class is needed to create the new Image class. Finally, the createImage() class creates the new image from the MemoryImageSource class. One last detail, though, is to use a MediaTracker class to make sure that the image is completely created before returning to the calling code.

Horizontal and Waves

Now that our generic applet is created, all we have to do is add one method to perform an image processing function. The following methods and variables show you what was used:

  The horzWave() method is very simple, yet extremely effective when it comes to animation.
  The dNumWaves variable contains the number of waves you want in the image.
  The dPercent variable contains the amplitude of the wave, given in percent of the image’s width.
  The dOffset variable describes the wave’s position.

Amplitude is the strength, or height, of the wave. It’s similar to waves at the beach. The higher the wave, the greater the amplitude.

To add this method to the Generic applet, simply place it below the line in the init() method that says // Process the image. The source code for horzWave() is shown in Listing 13.13.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.