Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (757)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (844)
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  
  gifs in JApplet, what happens to the alpha channel ??  (Read 970 times)
0 Members and 1 Guest are viewing this topic.
Offline r3c

Innocent Bystander

« Posted 2006-06-09 22: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.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.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 (;
            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]
  ignore  |  Print  

EgonOlsen (78 views)
2018-06-10 19:43:48

EgonOlsen (58 views)
2018-06-10 19:43:44

EgonOlsen (78 views)
2018-06-10 19:43:20

DesertCoockie (260 views)
2018-05-13 18:23:11

nelsongames (158 views)
2018-04-24 18:15:36

nelsongames (157 views)
2018-04-24 18:14:32

ivj94 (898 views)
2018-03-24 14:47:39

ivj94 (162 views)
2018-03-24 14:46:31

ivj94 (811 views)
2018-03-24 14:43:53

Solater (175 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05 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‑
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!