creating new BufferedImage copy

i have a bufferedimage and use the getSubimage() method. but it is important for me to create a new instance of the image in the size of the choosed subimage (i want to avoid referencing the whole image).

because bufferedimage has nothing like a copy-constructor
i tried somthing like this:


...
{
      BufferedImage frame = createBufferedImage(img.getSubimage(0, 0, 0, 0));
}

public BufferedImage createBufferedImage(BufferedImage image) 
      {
          ColorModel cm = image.getColorModel();
          boolean premultiplied = cm.isAlphaPremultiplied();
          WritableRaster raster = image.copyData(image.getRaster());
          return new BufferedImage(cm, raster, premultiplied, null);
      }

but i get the following exception:

java.awt.image.RasterFormatException: negative or zero width
at java.awt.image.Raster.(Raster.java:1094)
at java.awt.image.WritableRaster.(WritableRaster.java:114)
at sun.awt.image.SunWritableRaster.(SunWritableRaster.java:52)
at sun.awt.image.ByteComponentRaster.(ByteComponentRaster.java:140
)
at sun.awt.image.ByteInterleavedRaster.(ByteInterleavedRaster.java
:177)
at sun.awt.image.ByteInterleavedRaster.createWritableChild(ByteInterleav

Just use drawImage instead of getSubImage :slight_smile:


BufferedImage[] tiles = new BufferedImage[6];

for(int i=0;i<6;i++)
{
      tiles[i] = gc.createCompatibleImage(32,32,Transparency.BITMASK);
      Graphics2D tg = (Graphics2D)tiles[i].getGraphics();
      tg.setComposite(AlphaComposite.Src);
      tg.drawImage(img,
            0,0,
            32,32,
            i*32,0,
            i*32+32,32,
            null);
      tg.dispose();
}

The snipped above slices a 192x32 image into 6 32x32 images.

Yes. It’s really that simple :slight_smile:

it doesnt work for me properly. first i got only disturbed images, then i tried to get it work for only one tile and used the following code.


public void paint(Graphics g)
      {
            BufferedImage frame;
            Graphics2D g2D = (Graphics2D)g;
      
            
            
            GraphicsEnvironment       ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
          GraphicsDevice                   gs = ge.getDefaultScreenDevice();
          GraphicsConfiguration       gc = gs.getDefaultConfiguration();
            
            
            frame = gc.createCompatibleImage(32,32,Transparency.BITMASK);
            Graphics2D tg = (Graphics2D)frame.getGraphics();
            tg.setComposite(AlphaComposite.Src);
            tg.drawImage(img,0,0,32,32,32,0,32,32,null);
            
            tg.dispose();
            g2D.drawImage(frame, 0, 0, this);
      
      }

// img is a valid bufferedimage

but it seems that im not able to adept your code. all i see is a rectangle of white nothing … =)
i tried it with painting the whole image and a scaled version: therefore it works. for any reason i get no image painted with passing the four corners as parameters…

tg.drawImage(img,
0,0, //dx1, dy1 (upper left corner)
32,32, //dx2, dy2 (lower right corner)
i32,0, //sx1, sy1 (upper left corner)
i
32+32,32, //sx2, sy2 (lower right corner
null);

see here

d=destination s=source

Use pen and paper to sketch it out… it isn’t that complicated once you got your head around :wink:

You can test my snipped from before with this image:

ohhh… once upon a time a wiseman said

“read the fucking manual!”

thought the corners were ment clockwise …
but i havent said anything …

but i havent said anything …

Well, you said “four corners” that was a bit odd, since there has to be two pairs :wink:

Together with the problem you described and that piece of code wich just must result in something strange I gathered enough hints to spot your problem.

Don’t forget to post your game once it’s done :slight_smile: