Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to display/draw part of a gif file.

 
Scott Matthews
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am able to display a GIF file to a JFrame with code similar to the following:
.
.
Toolkit currentTK = Toolkit.getDefaultToolkit();
Image theWholeImage = currentTK.getImage("Whole.gif");
g.drawImage(theWholeImage,0,0,this);
Now, I would like to "grab" part of the GIF once it is loaded above and display the part "grabbed".
For example: Go to the original "Whole.gif" and grab from location 100,200 to location 200,400 and display this piece of the GIF file.
What is the way to do this? I may need to refer to over 50 pieces of the GIF file so I do not want to have individual GIF files.
Thanks
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
PixelGrabber is the answer to all your problems my friend! It slices! It dices! Look at what it does to this innocent little image named "Washu.gif"!



BAM! Those pixels are pulled right out of the image and are placed safely into this convenient integer array. "An integer array?!?!", You may be asking yourself. "But, Nathan, I wanted an image not some array of smegging integers?!?" That's where you would be wrong my friend, because we're only halfway done!



SPLOOSH! The suprisingly useful MemoryImageSource takes your array of pixels in one end and with a turn of the crank... What's that! YES! That's a shiny new Image coming out of the other end!

How much are these wonderful devices, you may ask... Well, how much would you pay for the convenience of only getting the parts you want from an Image? $39.99? $19.99? Well, if you act now and take advantage of this limited time Internet offer, available only though JavaRanch, they can be yours for the amazingly low price of... FREE! Yes, FREE! Take advantage of this limited offer and we will send you both the PixelGrabber and the MemoryImageSource absolutely free. If you are not completely satisfied, we'll completely refund your money! How's that for an offer!
 
Scott Matthews
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You Da Man. Thanks for the solution.
FYI: I am new to java so the following may be a function of my greeness, but I had to make 2 changes to get the code to compile and run. If you are interested, these are the changes:
1. PixelGrabber pg = new PixelGrabber( img, 100, 200, width,
height, 0, width );
changed to have "pixels" as one of the parameters
PixelGrabber pg = new PixelGrabber( img, 100, 200, width,
height, pixels, 0,
width );
2. Image smallImage = new MemoryImageSource( width, height,
pixels, 0, width);
changed to have "createImage()" in the line:
Image smallImage = createImage(new MemoryImageSource(
width, height, pixels,
0, width ));

Once again. Thanks for the solution.
[This message has been edited by Scott Matthews (edited August 11, 2001).]
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whoops! Sorry! Those should have been in there... guess I didn't copy/paste enough of the line from my code...
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's pretty slick, Nate.
 
Reuben Cleetus
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nathan, you've provided me with my first good laugh of the morning!! And to think it came from a programming site! =)
Keep it up!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic