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 (406)
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   
  Show Posts
Pages: [1]
1  Game Development / Newbie & Debugging Questions / gifs in JApplet, what happens to the alpha channel ?? on: 2006-06-10 00:46:19
I've got about five days experince in Java, madening rush to get an info project finished. But anyways, even though it's gotta be in a hurry, I want to make the most of it and learn to use it.

So, I figured out a lot of problems I had before but I'm into one more and I've got no mor ideas.

I'm making a Snake game in an applet. And I want to mvove some gifs around and use those images to draw the body. I figured out how to turn and place where I wanted but now I am exploring the more obscure universe of the alpha chanel in my image.

And I don't understand where it dissapears when I put everything in the main drawing region. What am I missing???

protected void DrawImage (Graphics2D g2d, Image image, int x, int y, double angle)
          { int imgW = image.getWidth (this);
            int imgH = image.getHeight (this);
           
            Color transparent = new Color (0, 0, 0, 0);
                   
            BufferedImage buffImg = (BufferedImage) createImage (41, 41);
            Graphics2D buffImgSurface = buffImg.createGraphics();
           
            buffImgSurface.setBackground (transparent);
            buffImgSurface.setComposite(AlphaComposite.Src);
            buffImgSurface.drawImage (headImage, 20-imgW/2, 20-imgH/2, this);
           
            BufferedImage rotatedImage = (BufferedImage) createImage (41, 41);
            Graphics2D rotatedImageGraphics = rotatedImage.createGraphics();
            rotatedImageGraphics.setBackground (transparent);
           
            AffineTransform complTrans = new AffineTransform();
            complTrans.setToRotation (angle, 21, 21);
            rotatedImageGraphics.setComposite(AlphaComposite.Src);
            rotatedImageGraphics.drawImage (buffImg, complTrans, this);
           
//            float alpha = 1.0f;
//            g2d.setComposite (AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));                       
// This code sets the overall alpha, whereas I just want a certain portion of my pic to be invisible.

            g2d.setComposite (AlphaComposite.Src);
// This one makes a lot of artefacts and colors become to faint
            g2d.drawImage (rotatedImage, x-21, y-21, this);
           
            g2d.setColor (Color.blue);
            g2d.fillOval (x-1, y-1, 3, 3);
           
            System.out.println ("The image can be transparent: " + hasAlpha (headImage));
            System.out.println ("The first buffer can be transparent: " + hasAlpha (buffImage));
            System.out.println ("The final buffer can be transparent: " + hasAlpha (rotatedImage));
          }

I'm using two buffers to first draw the image further form the corners and then rotate it around it's center. This way I don't lose  nothing on the edges. I found in the books a cleaner way of doing this in only one buffer but this is just code on which I'm testing, the final code is tidyer.

hasAlpha (BufferedImage) is a function that says if there is an alpha channel or not in the buffer. It returns true, false, false. So I guess that when copying from buffer to buffer the alpha gets lost, but how can I keep it??
Pages: [1]
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Get high quality music tracks 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 (76 views)
2013-05-17 21:29:12

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

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

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

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

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

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

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

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

UnluckyDevil (185 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.098 seconds with 21 queries.