Image flipping in java

Status
Not open for further replies.

cyclonus

Ars Scholae Palatinae
618
Ok, so i have an image object. My goal is to be able to flip the image vertically, and have that as a new image object. Anybody have any idea as to how i go about doing this?

I looked into..the graphics class in java.awt, and there is a drawImage class that could flip..but it would only flip the display. I'm searching on how to actually create a new image object, only with a flipped image. Thanks for any tips.

-cyc
 

wb

Ars Legatus Legionis
25,208
Subscriptor
Well, I don't normally do GUI stuff in Java, but here's my stab at it (I tested this code and it seems to work ok):

code:<HR>
Code:
      int width = imageNormal.getWidth(this);
      int height = imageNormal.getHeight(this);
      if( width > 0 && height > 0 && imageFlipped == null )
      {
         // create a couple of integer arrays to store pixel information
         // for our normal and reversed images.  Of course you could
         // also get by with one array plus an array large enough to
         // store a single raster line, and swap the lines one at a time.
         int[] pixels = new int[width*height];
         int[] reversed = new int[width*height];
         // create a java.awt.image.PixelGrabber to get the pixel data
         PixelGrabber pg = 
              new PixelGrabber( imageNormal, 0,0, width/2, height/2,
                                pixels, 0, width );
         try 
         {
            pg.grabPixels(); 
         } 
         catch(InterruptedException e )
         {
            System.out.println( "Pixel Grabbing interrupted!" );
         }
         // reverse the pixels vertically
         for( int i=0; i<width; i++ )
            for( int j=0; j<height; j++ )
               reversed[(height-j-1)*width+i] = pixels[j*width+i];
         // create a new image from the reversed pixels
         imageFlipped = this.createImage( 
                          new MemoryImageSource(width,height,reversed,0,width));
      }
 
Status
Not open for further replies.