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; 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.phpHexadecimal 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:
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"