Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (739)
Games in Android Showcase (224)
games submitted by our members
Games in WIP (820)
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  
  Semi-transparent Image painting?  (Read 8630 times)
0 Members and 1 Guest are viewing this topic.
Offline Javaholic

Senior Newbie





« Posted 2003-05-07 14:48:02 »

I have been looking around for a long time, but I can't seem to find out how to draw an Image semi-transparently onto a Graphics object.  Could someone help me out?  I don't mean gifs or pngs, I mean taking an image and making it all semi-transparent.
Offline Abuse

JGO Ninja


Medals: 60


falling into the abyss of reality


« Reply #1 - Posted 2003-05-07 15:55:07 »

1  
2  
3  
Graphics2D g2d = (Graphics2D)g;
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,0.5f));
g2d.drawImage(img,x,y,null);
Offline Javaholic

Senior Newbie





« Reply #2 - Posted 2003-05-07 20:17:10 »

Thanks.  I would have never found that on my own.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline mill

Junior Devvie




popcorn freak


« Reply #3 - Posted 2003-05-08 07:12:15 »

isn't it faster if you change the bits in your image to make it translucent and then draw that image instead of using composite on the fly?

if it is, you can make all the translucent images at startup

Offline Abuse

JGO Ninja


Medals: 60


falling into the abyss of reality


« Reply #4 - Posted 2003-05-08 09:09:55 »

if your not going to be changing the level of translucency, then yeah I guess it would be a little quicker.

To Pre-generate the alpha there are 2 ways of going it, the complex way (1), and the dead ez way (2) Smiley

1) the complex, horribly ugly way...

1  
2  
3  
4  
5  
6  
7  
public BufferedImage makeTranslucent(BufferedImage source, float alpha)
{
   WritableRaster wr = source.getAlphaRaster();
   //use setSamples to alter the alpha component for each pixel.
   //^^ that bit is harder than it seems :P
   return source;
}


2) the incredibly simple way...

1  
2  
3  
4  
5  
6  
7  
8  
public BufferedImage makeTranslucent(BufferedImage source, float alpha)
{
   Graphics2D g2d = source.createGraphics();
   g2d.setComposite(AlphaComposite.SRC,alpha);
   g2d.drawImage(source,0,0,null);
   g2d.dispose();
   return source;
}
Offline mill

Junior Devvie




popcorn freak


« Reply #5 - Posted 2003-05-08 13:14:10 »

here's what i use...

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
public Image[] makeTranslucentImages(Image[] src) {
            Image[] transImages = new Image[src.length];
            int width = src[0].getWidth(null);
            int height = src[0].getHeight(null);
            for (int i = 0; i < src.length; i++) {
                  int[] pixels = new int[width * height];
                  PixelGrabber pg = new PixelGrabber(src[i], 0, 0, width, height, pixels, 0, width);
                  try {
                        pg.grabPixels();
                  }
                  catch (InterruptedException e) {
                        logger.error(getClass(), "Error grabbing pixels.");
                  }

                  for (int k = 0; k < pixels.length; k++) {
                        int alpha = (pixels[k] >> 24) & 0xff;
                        if (alpha != 0)
                              pixels[k] = 0x4F000000 | (pixels[k] & 0x00FFFFFF);
                  }
                  transImages[i] = Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(width, height, pixels, 0, width));
            }
            return transImages;
      }

Offline Abuse

JGO Ninja


Medals: 60


falling into the abyss of reality


« Reply #6 - Posted 2003-05-08 16:10:27 »

damn 1.1 is ugly Cheesy
Offline mill

Junior Devvie




popcorn freak


« Reply #7 - Posted 2003-05-08 16:15:08 »

sure is Smiley


Offline Javaholic

Senior Newbie





« Reply #8 - Posted 2003-05-08 17:33:03 »

What I was looking for was transparent for game menus mostly, although I could use it in applications.  While we are on the subject, could someone explain to me how to edit an image pixel-by-pixel?  I have had to resort to draw a 1 by 1 rectangle, and that likely is not very efficient.  Or is it?
Offline Abuse

JGO Ninja


Medals: 60


falling into the abyss of reality


« Reply #9 - Posted 2003-05-08 18:21:27 »

for per-pixel rendering in java1.1,
you have java.awt.image.PixelGrabber.

For per-pixel rendering in 1.2 onwards, you have BufferedImage/DataBufferInt, which is much more efficient.

1  
2  
3  
BufferedImage bi = new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);
DataBufferInt dbi = (DataBufferInt)(bi.getRaster().getDataBuffer());
int [] pixels = dbi.getData();
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline swpalmer

JGO Coder


Exp: 12 years


Where's the Kaboom?


« Reply #10 - Posted 2003-05-09 01:49:39 »

Or perhaps an ImageProducer or MemoryImageSource if you need 1.1 compatibility.. depends exactly what you mean by "edit"  the two classes I just mentioned can be used in conjunction with data from the PixelGrabber, or you can generate the entire image from scratch.

The best solution of course is to let 1.1 die the death that is long overdue and use Java 2 APIs.

Offline Javaholic

Senior Newbie





« Reply #11 - Posted 2003-05-09 12:43:26 »

How do you set a color with a plain-old integer?  What's all this 0x00FFFFFF and 0x4F000000 business?
Offline Abuse

JGO Ninja


Medals: 60


falling into the abyss of reality


« Reply #12 - Posted 2003-05-09 13:21:24 »

its the ARGB integer packed 32bits per pixel color format.

0xFF000000 masks out the alpha (A)
0x00FF0000 masks out the Red (R)
0x0000FF00 masks out the Green (G)
0x000000FF masks out the Blue (B)

hence, a fully opaque white pixel is represented as 0xFFFFFFFF
(which, if interpreted as a twos compliment numeric value, has the value of -1)
Offline GergisKhan

Junior Devvie




&quot;C8 H10 N4 O2&quot;


« Reply #13 - Posted 2003-05-09 16:45:56 »

Wait... transparent is 00 and opaque is FF?

Ooops, gotta fix a filter.

gK

"Go.  Teach them not to mess with us."
          -- Cao Cao, Dynasty Warriors 3
Offline DarkwingGT

Senior Newbie




Java games rock!


« Reply #14 - Posted 2003-05-12 12:51:01 »

A quick side note is that by grabbing the pixel data for the BufferedImage it can no longer be accelerated.
Offline Javaholic

Senior Newbie





« Reply #15 - Posted 2003-05-12 12:56:46 »

Cool.  Thanks for all the help.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 
Ecumene (55 views)
2017-09-30 02:57:34

theagentd (78 views)
2017-09-26 18:23:31

cybrmynd (186 views)
2017-08-02 12:28:51

cybrmynd (184 views)
2017-08-02 12:19:43

cybrmynd (191 views)
2017-08-02 12:18:09

Sralse (201 views)
2017-07-25 17:13:48

Archive (757 views)
2017-04-27 17:45:51

buddyBro (891 views)
2017-04-05 03:38:00

CopyableCougar4 (1440 views)
2017-03-24 15:39:42

theagentd (1323 views)
2017-03-24 15:32:08
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

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
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!