Advertisement

[java] copy small regions from big Image

Started by December 21, 2000 06:57 AM
4 comments, last by dr-m 24 years ago
Ok.. I need to copy small sections (frames) from a bigger Image. Then I store all my frames in a Image[]-array and just draw them. But the thing is that the cutting doesn''t work... I get a NullPointerException when getting the Graphics.. (FYI: Rect is my own Rectangle class)

private Image getImageFromOffset(Image srcImage, Rect source)
	throws InterruptedException
{   
	Image destImg = createImage(source.getWidth(), source.getHeight());
	Graphics gDestImg = destImg.getGraphics();
        
	gDestImg.drawImage(srcImage, 0, 0,
			source.getWidth(), source.getHeight(),
			source.getTopX(), source.getTopY(),
			source.getWidth() + source.getTopX(),
			source.getHeight() + source.getTopY(),
			null);  
	return destImg;
}
 
Does anyone have a suggestion of a better routine that does the same thing but with better code..? Or atleast tell me whats wrong with this one..? / dr-m
/ dr-m
quote: Original post by dr-m

Ok.. I need to copy small sections (frames) from a bigger
Image. Then I store all my frames in a Image[]-array and just
draw them. But the thing is that the cutting doesn''t work...

I get a NullPointerException when getting the Graphics..
(FYI: Rect is my own Rectangle class)

private Image getImageFromOffset(Image srcImage, Rect source)	throws InterruptedException{   	Image destImg = createImage(source.getWidth(), source.getHeight());	Graphics gDestImg = destImg.getGraphics();        	gDestImg.drawImage(srcImage, 0, 0,			source.getWidth(), source.getHeight(),			source.getTopX(), source.getTopY(),			source.getWidth() + source.getTopX(),			source.getHeight() + source.getTopY(),			null);  	return destImg;}  


Does anyone have a suggestion of a better routine that does the
same thing but with better code..? Or atleast tell me whats
wrong with this one..?



/ dr-m


that createImage(width,height).getGraphics() method is fine, my guess would be that source is undef. One assumes your extending Applet??
Advertisement
Yes, it extends Applet (but just to get the
Component.createImage(int,int) method). I want to
create the new Image, and I couldn''t think of another
way to do that.

And the source-object works fine. Even if I hardcode the
values i want in there, I still get that NullPointerException..

/ dr-m
/ dr-m
Hello,

I tried your code out in my applet and it worked fine for me, the only thing I did was change the code like this:

    private Image getImageFromOffset(Image srcImage )          throws InterruptedException{   	  Image destImg = createImage( 20, 20 );	  Graphics gDestImg = destImg.getGraphics();        	  gDestImg.drawImage(srcImage, 0, 0,			                     20, 20,			                     10, 10,			                     20,			                     20,			                     null);  	  return destImg;}  


But you said you hard coded the values in there as well, so I don't know why it won't work on your pc. Guess I wasn't much
help...

ao

Edited by - ao on December 21, 2000 11:03:59 AM
Play free Java games at: www.infinitepixels.com
Well the thing is, that I extend the class with Applet, but the class is not really an applet at all. It''s just a standalone class used by an applet.

Today, I use this pathetic routine going from Image to Int[] to new Image... But there must be a better way to do this!


      private Image getImageFromOffset(Image srcImage, Rect Source)        throws InterruptedException    {        Toolkit tk = Toolkit.getDefaultToolkit();        int tempImg[] = new int[Source.getWidth() * Source.getHeight()];        PixelGrabber pg = new PixelGrabber(srcImage,                        Source.getTopX(), Source.getTopY(),                        Source.getWidth(), Source.getHeight(),                        tempImg, 0, Source.getWidth());        pg.grabPixels();        return tk.createImage(new MemoryImageSource(Source.getWidth(),Source.getHeight(),                                                 tempImg, 0, Source.getWidth()));    }  



/ dr-m
/ dr-m
There are problems using createImage(int,int) in other classes than the applet main class.. This code runs fine in my appletclass, but generates NullPointerException if moved to another class thats not run as an applet;

          Image test = createImage(200,200);        Graphics g = test.getGraphics();        g.setColor(Color.black);        g.fillRect(0,0,200,200);  


Can someone explain why (and how to create offscreen bitmaps)?

/ dr-m
/ dr-m

This topic is closed to new replies.

Advertisement