This class will load transparent or non-transparent images into some buffer with there width and height
The transparent color will the pixel color at the lower left of the image.
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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
| import java.awt.Image; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.nio.ByteBuffer; import java.awt.Color; import javax.imageio.ImageIO;
public class StaticImages {
public static ByteBuffer _32BITImages[] = new ByteBuffer[32]; public static int _Width[]= new int[32]; public static int _Height[]= new int[32]; public static void setBTrans(BufferedImage name,int temp){ for (int _yi = 0; _yi<name.getHeight();_yi++){ for (int _xi = 0; _xi<name.getWidth();_xi++){ if (name.getRGB(_xi,_yi)==temp) name.setRGB(_xi,_yi,0); } } } private static int fixRGB(int num){ int Alpha = 0xFF000000; int Red = 0x00FF0000; int Green = 0x0000FF00; int Blue = 0x000000FF; int finalR = 0; Red = num & Red; Red = Red << 8; Green = num & Green; Green = Green << 8; Blue = num & Blue; Blue = Blue << 8; Alpha = num & Alpha; Alpha = Alpha >>> 24; finalR = Red | Green | Blue | Alpha; return finalR; }
public static void Load32BitImg(String name,int id,java.awt.GraphicsDevice GD,boolean trans){ try{
Image imgTemp; imgTemp = (Image)ImageIO.read(new File(name)); ByteBuffer buf; BufferedImage Bimg = GD.getDefaultConfiguration().createCompatibleImage(imgTemp.getWidth(null),imgTemp.getHeight(null),Color.BITMASK); Bimg.getGraphics().drawImage(imgTemp,0,0,null); if (trans) setBTrans(Bimg,Bimg.getRGB(0,Bimg.getHeight()-1)); buf = ByteBuffer.allocateDirect((Bimg.getWidth()*Bimg.getHeight())*4); for (int y = 0; y< Bimg.getHeight();y++){ for (int x = 0; x< Bimg.getWidth();x++) buf.putInt(fixRGB(Bimg.getRGB(x, Bimg.getHeight()-y-1))); } _32BITImages[id] = buf; _Width[id] = Bimg.getWidth(); _Height[id] = Bimg.getHeight(); }catch(IOException ie){ System.out.println("Error Reading Image"); } } } |
How to draw those images.
1 2 3 4 5 6 7 8 9 10
| tempGL.glEnable(GL.GL_BLEND); StaticImages._32BITImages[imgID].clear(); tempGL.glRasterPos2i(X Position,Y Position); tempGL.glDrawPixels(StaticImages._Width[imgID], StaticImages._Height[imgID], GL.GL_RGBA, GL.GL_UNSIGNED_INT_8_8_8_8_REV, StaticImages._32BITImages[imgID]); tempGL.glDisable(GL.GL_BLEND); |