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??