Hi !
Featured games (84)
games approved by the League of Dukes
Games in Showcase (604)
Games in Android Showcase (171)
games submitted by our members
Games in WIP (654)
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  
  transparent colors  (Read 1979 times)
0 Members and 1 Guest are viewing this topic.
Offline PRW56

Senior Newbie

« Posted 2012-07-11 10:45:31 »

You know how every image in java (or anywhere else) has to be square? You have some kind of background in the places where you didn't draw (white or otherwise), so how do you turn that transparent/opaque in java? I tried drawing the image in photoshop with an opaque background, and it was still drawn white. Then google showed me this (found at

public Image transformGrayToTransparency(BufferedImage image)   
        ImageFilter filter = new RGBImageFilter()    
            public final int filterRGB(int x, int y, int rgb)      
                return (rgb << 8) & 0xFF000000;      
        ImageProducer ip = new FilteredImageSource(image.getSource(), filter);    
        return Toolkit.getDefaultToolkit().createImage(ip);  

What I really don’t understand is lines 5-8, namely the return statement, because 1) I don’t understand its logic or the purpose of that hexadecimal number, and 2) how is rgb 1 integer? If there is a simpler way to do what I need without the use of this code, please tell me.
Offline ra4king

JGO Kernel

Medals: 389
Projects: 3
Exp: 5 years

I'm the King!

« Reply #1 - Posted 2012-07-11 13:16:03 »

That looks.....weird. Ignore that way. But to answer your questions:
Read this to understand why 'rgb' is 1 integer.
Read this to understand bitwise operations.

If you create a PNG in any image editing tool and make the background transparent (alpha == 0), there shouldn't be anything extra to do in the code other than the BufferedImage. That is all.

Offline Roquen
« Reply #2 - Posted 2012-07-11 13:20:58 »

Every image square?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline PRW56

Senior Newbie

« Reply #3 - Posted 2012-07-11 16:05:08 »

ok fine rectangles, anyway ra4king you were right. I never knew that saving as a jpg ment you had no alpha layer, which is why photoshop didn't work... ty
Offline StumpyStrust
« Reply #4 - Posted 2012-07-12 04:08:23 »

Hehe I was going to say that jpegs don't save transparency but was not quit sure if you were asking about loading transparent images or using code to make transparent images.

@Roquen I thought that when it comes down to it most images you load are square?  Huh or rectangular. Can we load circles and trapezoids?  Grin

Offline Roquen
« Reply #5 - Posted 2012-07-20 11:53:15 »

There are techniques for mapping non-flat surfaces.  Notable for mapping a sphere, so while physical storage is indeed a rectangle, it is designed for giving (approximately) uniform texel coverage for mapping to a sphere.  So, it depends on what perspective you're taking.
Offline jonjava
« Reply #6 - Posted 2012-07-20 12:57:26 »

What I really don’t understand is lines 5-8, namely the return statement, because 1) I don’t understand its logic or the purpose of that hexadecimal number, and 2) how is rgb 1 integer? If there is a simpler way to do what I need without the use of this code, please tell me.

1) It's just another way for programmers to write data in our pretty editors that the compiler can read. In the end, the compiler rewrites everything into bytes, i.e, one's and zero's (1110001010101010101010).

2) An image is represented by a bunch of pixels. A pixel is most of the time a combination of 3 colors: red, green, blue.

class Pixel {
  byte red; // a byte is basically an int but it can only store values from 0-255. ( an int is made up of 4 bytes = 32bits )
  byte green;
  byte blue;

If you're wondering why we only need 255 different values for our 3 colors it's because together they can represent 255*255*255 = 16 581 375 (16½ million) different colors. Some research suggest the human eye can only distinguish around 3 million different colors so for most purposes it's more than enough.

And because an int is made up of 4 bytes you can fit the 3 bytes that represents the colors: red, green and blue in it. There's still room for 1 more byte which is often used to store an alpha value ( transparency ).

Let's explain the hexadecimal thing a bit more. In java ( and pretty much all other programming languages ) you can tell the compiler that you're writing a number in Hexadecimal by the prefix '0x'. You need to tell this to the compiler so that it knows how it's supposed to convert it into bytes. Here's a Hex/Dec/bit converter:

Hexadecimal is very cool because it lets us (programmers) use 1 character (0-F) to represent 4 bits at a time. Because 1 byte is 8 bits, you need 2 hex characters to represent a byte.


Here we have 8 Hex numbers, or 4 bytes, or 1 int. The value of the Hex "FF000000" as an int would be the number "4278190080". And by the same value I mean that they are made up of the exact same bytes (in this case, they are both made up of exactly "11111111000000000000000000000000"). The cool thing about Hex is that it allows us (programmers) to easily see how the data is represented as bytes. Good luck trying to figure out how the number "4278190080" looks in bytes without a calculator.

"FF000000" and "4278190080" are just another way for PROGRAMMERS to represent bytes. The compiler converts them into bytes and it all ends up the same no matter if you represent it as an int or a Hex number.

The << is a bit shift operator: , i.e, you can move bits to the left or right. So for example taking the bits "11110001" ( == 241 (decimal) == F1 (hex )) and shift them to the left 2 times with << would make the bits "11000100" ( the left most bits got pushed off and new 0 bits came in from the right ). You can also use the <<< operator if you want the bits that are pushed off to the left to appear to the right instead. So "11110001" <<< 2 would become "11000111".

public final int filterRGB(int x, int y, int rgb)       
                return (rgb << 8) & 0xFF000000;      

So what happens here, is first of all the rgb parameter is shifted to the left 8 bits. Let's pretend rgb = 2852978687, which in Hex would be "AA0CFFFF" and in bits "10101010000011001111111111111111". So shifting it left 8 bits would leave us with "00001100111111111111111100000000".

After that we use the bitwise and operator ( single '&' ). Which wikipedia explains quite well . The bits on the right of the operator is called the "bit mask".


Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

SHC (28 views)
2015-08-01 03:58:20

Jesse (20 views)
2015-07-29 04:35:27

Riven (40 views)
2015-07-27 16:38:00

Riven (22 views)
2015-07-27 15:35:20

Riven (25 views)
2015-07-27 12:26:13

Riven (15 views)
2015-07-27 12:23:39

BurntPizza (36 views)
2015-07-25 00:14:37

BurntPizza (46 views)
2015-07-24 22:06:39

BurntPizza (31 views)
2015-07-24 06:06:53

NoxInc (37 views)
2015-07-22 22:16:53
List of Learning Resources
by gouessej
2015-07-09 11:29:36

How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30

Intersection Methods
by Roquen
2015-05-29 08:19:33

List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21 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!