1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
| import java.awt.*; import java.awt.image.*; import java.io.*; import javax.imageio.ImageIO;
public class SpriteSheet{ String name; BufferedImage pic; SpriteSheet(String s){ name=s; try{ pic=ImageIO.read(getClass().getResource(s)); }catch(Exception e){ System.out.println("Could not initialize spritesheet"); pic=null; } } public BufferedImage getPic(int x, int y, int width, int height){ BufferedImage retval; int trans=pic.getColorModel().getTransparency(); Graphics2D temp; GraphicsConfiguration gc=GraphicsEnvironment.getLocalGraphicsEnvironment(). getDefaultScreenDevice().getDefaultConfiguration(); retval=gc.createCompatibleImage(width,height,trans); temp=retval.createGraphics(); temp.drawImage(pic,0,0,width,height,x,y,x+width,y+height,null); temp.dispose(); } public BufferedImage getPic(int x, int y, int width, int height, boolean reversed){ BufferedImage retval; int trans=pic.getColorModel().getTransparency(); Graphics2D temp; GraphicsConfiguration gc=GraphicsEnvironment.getLocalGraphicsEnvironment(). getDefaultScreenDevice().getDefaultConfiguration(); retval=gc.createCompatibleImage(width,height,trans); temp=retval.createGraphics(); if(reversed) temp.drawImage(pic,width,0,0,height,x,y,x+width,y+height,null); else temp.drawImage(pic,0,0,width,height,x,y,x+width,y+height,null); temp.dispose(); } public BufferedImage[] getPicArray(int x, int y, int width, int height, int num){ BufferedImage[] retval=new BufferedImage[num]; for(int i=0;i<num;i++){ retval[i]=getPic(x,y,width,height); x+=width; } return retval; } public BufferedImage[] getPicArray(int x, int y, int width, int height, int num, boolean reversed){ BufferedImage[] retval=new BufferedImage[num]; for(int i=0;i<num;i++){ retval[i]=getPic(x,y,width,height,reversed); x+=width; } return retval; } public BufferedImage[][] getDoublePicArray(int x, int y, int width, int height, int num1, int num2){ BufferedImage[][] retval=new BufferedImage[num2][]; for(int i=0;i<num2;i++){ retval[i]=getPic(x,y,width,height,num1); y+=height; } return retval; } public BufferedImage[][] getDoublePicArray(int x,int y,int width,int height,int num1,int num2,boolean reversed){ BufferedImage[][] retval=new BufferedImage[num2][]; for(int i=0;i<num2;i++){ retval[i]=getPicArray(x,y,width,height,num1,reversed); y+=height; } return retval; } } |