Skip to content

Instantly share code, notes, and snippets.

@arwagner
Created January 12, 2013 17:22
Show Gist options
  • Save arwagner/4519394 to your computer and use it in GitHub Desktop.
Save arwagner/4519394 to your computer and use it in GitHub Desktop.
public static Sprite fromSpriteSheet(String path, int totalWidth, int width, int height, int x, int y) throws IOException{
BufferedImage target = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
int[] pixels = ((DataBufferInt)target.getRaster().getDataBuffer()).getData();
BufferedImage source = ImageIO.read(Sprite.class.getResource(path));
int[] sourcePixels = ((DataBufferInt)source.getRaster().getDataBuffer()).getData();
for (int i = 0; i < width; i++){
for (int j = 0; j < height; j++){
pixels[width * j + i] = sourcePixels[totalWidth * y + x + i];
}
}
return new Sprite(target, width, height);
}
// Exception in thread "main" java.lang.ClassCastException: java.awt.image.DataBufferByte cannot be cast to java.awt.image.DataBufferInt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment