Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (577)
games submitted by our members
Games in WIP (498)
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]
 

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

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (23 views)
2014-04-15 18:08:23

BurntPizza (18 views)
2014-04-15 03:46:01

UprightPath (32 views)
2014-04-14 17:39:50

UprightPath (16 views)
2014-04-14 17:35:47

Porlus (32 views)
2014-04-14 15:48:38

tom_mai78101 (58 views)
2014-04-10 04:04:31

BurntPizza (116 views)
2014-04-08 23:06:04

tom_mai78101 (216 views)
2014-04-05 13:34:39

trollwarrior1 (183 views)
2014-04-04 12:06:45

CJLetsGame (190 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
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!