Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (408)
games submitted by our members
Games in WIP (293)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Loading and Drawing 2d Images with drawPixels in JOGL  (Read 1320 times)
0 Members and 1 Guest are viewing this topic.
Offline Epo

Senior Newbie


Projects: 1


Green Tea and swords


« Posted 2009-05-11 15:04:26 »



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);
      }//END X FOR STATEMENT
     }//END Y FOR STATEMENT
     
      }//END METHOD
 
   
   
   
   
   
   
   
   
   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;
   }//END METHOD
 
   
   
   

   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;
      //System.out.println("Found File");
     
      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)));
      }//END FOR LOOP
       
      //System.out.println("BUFFER MADE");
     
      _32BITImages[id] = buf;
      _Width[id] = Bimg.getWidth();
      _Height[id] = Bimg.getHeight();
     
      }catch(IOException ie){
      System.out.println("Error Reading Image");
      }//END TRY AND CATCH STATEMENT
     }//END METHOD
 
   
   
   
}//END CLASS




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);

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Browse for soundtracks for your game!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (125 views)
2013-05-17 21:29:12

alaslipknot (133 views)
2013-05-16 21:24:48

gouessej (163 views)
2013-05-16 00:53:38

gouessej (155 views)
2013-05-16 00:17:58

theagentd (168 views)
2013-05-15 15:01:13

theagentd (153 views)
2013-05-15 15:00:54

StreetDoggy (197 views)
2013-05-14 15:56:26

kutucuk (221 views)
2013-05-12 17:10:36

kutucuk (220 views)
2013-05-12 15:36:09

UnluckyDevil (224 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.2 seconds with 20 queries.