Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (533)
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  
  Sprite Alpha  (Read 1585 times)
0 Members and 1 Guest are viewing this topic.
Offline Dreamcatchermatt

Junior Member





« Posted 2010-05-23 20:40:34 »

Ok, one question with possibley two parts:

First, basicaly: How can I set the alpha (or transparency) of a sprite held as a BufferedImage in Graphics2D?

Secondly: What I want to be able to do is fade in and out my particles neatly as they are created/destroyed.

I sort of invision the particle eventualy having some sort of 'Stop' system like:
1  
2  
3  
Age=0, Alpha=0.0;
Age=50, Alpha=1.0;
age 200, Alpha=0.0;

Where my sprite Render() method then interperlates between those values for any given life of the sprite.

Ahh, part three of two:
It would also be usefull to change the color of the sprite using the above method - is it possible, given a grey (or colored) image, to tint the image to a color?

Thanks  Smiley
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Reply #1 - Posted 2010-05-24 04:53:43 »

Ok, one question with possibley two parts:

First, basicaly: How can I set the alpha (or transparency) of a sprite held as a BufferedImage in Graphics2D?
Either use a GIF with transparency or a PNG with an alpha channel.
Secondly: What I want to be able to do is fade in and out my particles neatly as they are created/destroyed.

I sort of invision the particle eventualy having some sort of 'Stop' system like:
1  
2  
3  
Age=0, Alpha=0.0;
Age=50, Alpha=1.0;
age 200, Alpha=0.0;

Where my sprite Render() method then interperlates between those values for any given life of the sprite.
What's the question?
Ahh, part three of two:
It would also be usefull to change the color of the sprite using the above method - is it possible, given a grey (or colored) image, to tint the image to a color?

Thanks  Smiley

It is not possible directly with the 2D API like it is with OpenGL.  Instead you would have to read the data out of the BufferImage, change it to the colour you want and write it back.

Offline Dreamcatchermatt

Junior Member





« Reply #2 - Posted 2010-05-24 09:58:22 »

Ahh, ok. What I ment was modifying the overall opacity of an image during the Render() method of my game loop to any value I like, rather than having an image with pixles that are transparrent, which I can do fine.

The second question was, assuming that you could dynamicly alter the overall alpha of an image (gif, png, jpeg whatever). So if this was then done over time, the image could be faded in and out. Would this be possible in the wayi described.

It would seem not then.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Reply #3 - Posted 2010-05-24 16:44:35 »

You might be able to do it with java.awt.image.ColorConvertOp.  I am not that familiar with the image sub package so I don't know how it works.  The only way I know how is to pull the data out of the image and transform the colours and then put it back.

Offline Dreamcatchermatt

Junior Member





« Reply #4 - Posted 2010-05-25 00:31:46 »

yeh, I've had a look through the docs and all the methiods seem rally longwinded.

I think that I'm going to have to move to JOGL if I want to get the results that I want to get with this project.
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11


Game Engineer


« Reply #5 - Posted 2010-05-25 02:58:36 »

yeh, I've had a look through the docs and all the methiods seem rally longwinded.

I think that I'm going to have to move to JOGL if I want to get the results that I want to get with this project.

1  
2  
glColor4f(1.0f, 1.0f, 1.0f, 0.5f);
//BOOM


Yes, OpenGL will be a good idea. You might want to go with LWJGL instead as it is more frequently updated.

See my work:
OTC Software
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #6 - Posted 2010-05-25 10:56:00 »

You might be able to do it with java.awt.image.ColorConvertOp.  I am not that familiar with the image sub package so I don't know how it works.  The only way I know how is to pull the data out of the image and transform the colours and then put it back.

yeh, I've had a look through the docs and all the methiods seem rally longwinded.

I think that I'm going to have to move to JOGL if I want to get the results that I want to get with this project.

Guys, you're making this harder than it needs to be:

1  
2  
3  
4  
5  
g.setComposite( AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha)); // Set blending mode and alpha

g.drawImage(...);

g.setPaintMode(); // Restore previous state


You might want a different blend mode for particles depending on what effect you're after.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 743
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #7 - Posted 2010-06-21 17:31:26 »

I sort of invision the particle eventualy having some sort of 'Stop' system like:
1  
2  
3  
Age=0, Alpha=0.0;
Age=50, Alpha=1.0;
age 200, Alpha=0.0;


Something like this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
private float[] ages = new float[]{0,50,200};
private float[] alphas = new float[]{0,1,0};

public float getAlphaForAge(float age)
{
   for(int i=1; i<ages.length; i++)
   {
      if(age > ages[i])
         continue;

      float loAge = ages[i-1];
      float hiAge = ages[i-0];
      float ratio = (age - loAge) / (hiAge - loAge);
   
      float loAlpha = alphas[i-1];
      float hiAlpha = alphas[i-0];
      return loAlpha + ratio * (hiAlpha - loAlpha);
   }

   return alphas[alphas.length-1];
}


Or your can precalculate it into a float[200]...

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline ChouChou

Senior Newbie





« Reply #8 - Posted 2010-06-22 04:06:27 »

If your doing what I'm thinking you are doing then you can try this, this allows you to change the transparency on an image, preferably a png:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
        /** Image transperancy value*/
   float transperancy = 0;

   public BufferedImage drawAlpha() {
      /** Load Image, note that it is better if you preload the image else where, this is just to show you how its loaded. */
      BufferedImage loaded = ImageIO.read(new File("images/buildings/g_u_1.png"));

      /** Create alpha image */
      BufferedImage aimg = new BufferedImage(loaded.getWidth(), loaded.getHeight(), BufferedImage.TRANSLUCENT);  

       /** Get image graphics */
      Graphics2D g = aimg.createGraphics();

      /** Set the Graphics composite to Alpha */
      g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, transperancy));  

      /** Draw the loaded image into the prepared reciver image */
      g.drawImage(loaded, null, 0, 0);  

      /** Release system resource in this graphics */
      g.dispose();
     
      return aimg;
   }


And this is how you would draw it, the transparent value has a range from 1.0 ~ 0.0, hope it helps.:
1  
2  
3  
public void paint(Graphics2D g) { 
      g.drawImage(drawAlpha(), x, y, width, height, null);
}
Offline DavidX

Senior Newbie





« Reply #9 - Posted 2010-06-26 16:37:05 »

whats with all this filtering bullshit when we can just use the AlphaComposite class?!
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

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

The first screenshot will be displayed as a thumbnail.

pw (24 views)
2014-07-24 01:59:36

Riven (22 views)
2014-07-23 21:16:32

Riven (18 views)
2014-07-23 21:07:15

Riven (21 views)
2014-07-23 20:56:16

ctomni231 (50 views)
2014-07-18 06:55:21

Zero Volt (45 views)
2014-07-17 23:47:54

danieldean (36 views)
2014-07-17 23:41:23

MustardPeter (39 views)
2014-07-16 23:30:00

Cero (54 views)
2014-07-16 00:42:17

Riven (54 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!