Created
January 12, 2013 17:22
-
-
Save arwagner/4519394 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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