Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
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 1632 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 http://stackoverflow.com/questions/665406/how-to-make-a-color-transparent-in-a-bufferedimage-and-save-as-png):

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
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: 350
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 ImageIO.read(URL) the BufferedImage. That is all.

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

Every image square?  Err...no.
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.

1  
2  
3  
4  
5  
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: http://www.easycalculation.com/hex-converter.php

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.

1  
0xFF000000

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: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html , 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".

1  
2  
3  
4  
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 http://en.wikipedia.org/wiki/Bitwise_operation#AND . The bits on the right of the operator is called the "bit mask".

"00001100111111111111111100000000"
"11111111000000000000000000000000"
=
"00001100000000000000000000000000"

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.

Longarmx (39 views)
2014-10-17 03:59:02

Norakomi (29 views)
2014-10-16 15:22:06

Norakomi (24 views)
2014-10-16 15:20:20

lcass (28 views)
2014-10-15 16:18:58

TehJavaDev (57 views)
2014-10-14 00:39:48

TehJavaDev (56 views)
2014-10-14 00:35:47

TehJavaDev (47 views)
2014-10-14 00:32:37

BurntPizza (64 views)
2014-10-11 23:24:42

BurntPizza (36 views)
2014-10-11 23:10:45

BurntPizza (78 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!