Hi,
Can someone help me? I want to draw a Image into a JPanel in a semi-transparent way. I use the drawImage method of the java.awt, but i can not find a overloaded method with a assigned transparent parameter.
thx Luca
Hi,
Can someone help me? I want to draw a Image into a JPanel in a semi-transparent way. I use the drawImage method of the java.awt, but i can not find a overloaded method with a assigned transparent parameter.
thx Luca
You need to set the composite for that.
yourGraphics.setComposite( AlphaComposite.getInstance(AlphaComposite.SrcOver, yourAlpha) );
Hi thx for the reply, but i dont get it :-\
I am currently useing Graphics and not a Graphics2D. To change that should not be a big problem. But I am drawing only Images to the graphics which
maybe have a alpha value. I dont want to draw the whole graphics semi-transparent. So what do I have to implement⌠do I have to draw a grapics2D object (with alpha) into the JPanelâs graphics2D object?
here is my Graphic-Thread:
public class GamePanel extends JPanel implements Runnable{
private Thread animation;
private boolean running = false;
private Graphics dbg;
private Image dbImage = null;
private Color clearColor = Color.black;;
public void addNotify()
{
super.addNotify();
startAnimation();
}
private void startAnimation()
{
if ( animation != null || !running ){
animation = new Thread( this );
animation.start();
}
}
public void stopAnimation(){
running = false;
}
public void run()
{
running = true;
while( running ){
gameRender();
paintScreen();
repaint();
try {
Thread.sleep( 80 );
}
//TODO console Output
catch( InterruptedException ex ){}
}
}
private void gameRender()
{
if( dbImage == null )
{
dbImage = createImage( 100, 100 );
if ( dbImage == null {
// TODO: errormsg
}
else
{
dbg = dbImage.getGraphics();
}
}
dbg.setColor( clearColor );
dbg.fillRect( 0, 0, 100, 100 );
paintVisible();
}
private void paintVisible()
{
Visible tmp = null;
// running through a List of Visible objects wich containing a Image, a position and a alpha value [0,100]
for (int i = 0 ; i < RegistrationManager.getVisibleObjectCount() ; i++)
{
tmp = RegistrationManager.getVisibleObject( i );
// if a Visible object has a alpha-value < 100, it should be drawn semi-transparent
dbg.drawImage( tmp.getImage() , tmp.getXPos() , tmp.getYPos() , null );
}
}
public void paintComponent( Graphics g )
{
super.paintComponent( g );
if ( dbImage != null ) g.drawImage( dbImage , 0 , 0 , null );
}
private void paintScreen()
{
Graphics g;
try {
g = this.getGraphics();
if ((g != null) && ( dbImage != null ))
{
g.drawImage( dbImage , 0 , 0 , null );
}
g.dispose();
}
catch (Exception e){}
}
}
In the method paintVisible all Visible objects are drawn. If one of these Visible objects do have a alpha valueâŚ
(float) tmp.getAlpha();
⌠it should be drawn with the Alpha-Value.
Can someone help me?
As far as i know, when requesting your Graphics object, you now only get a Graphics2D one, never a simple Graphics. I believe that can be confirmed by the java2D team, but a quick look around in jdk sources would confirm/infirm that rapidly.
If so, you can simply cast away and go on.
By the way, prepare for some critiques. I see you intend to use the swing repaint mechanism for animating, people around generally think itâs bad for a game.
Hi LucaH,
if you the required alpha values are already in your image, then there shouldnât be anything else to do except calling the Graphics2D drawImage method. You just have to make sure the ColorModel of your image supports the transparency.
If you want to apply some additional transparency to your whole image, then you should probably use the AlphaComposite as Pepe mentioned.
To use the Graphics2D instead of the Graphics, just use a cast: g2d = (Graphics2D)g
Cheers.
OK guys thank you.
My problem was the fileformat. A âgifâ does not contain a alpha value. They are only full transparent or not. But I can load the âpngâ fileformat as well, and they have ;D. I actually dont have to cast the graphics to a graphics2D and I dont have to implement a logic to describe that a imgae should have a aplha value.
But pepe, because of your critiques⌠which other options do I have, not useing the swing repaint mechanism? To satisfy people around and maybe improve perfomance or something
Google âMartak Active Renderingâ
Happy reading
Edit: I should add that your other choice is OpenGL through either JOGL or LWJGL. This is how th Puppy Games 2D games are done.