I am having problems converting a colored image with some transparent pixels to grayscale. I have searched and found related questions on this website but nothing I have be
While I was writing this you got your "how it is done" answer, so I'll just add some explanations on what is happening and why you did not succeed.
You probably know when an image is represented in ARGB (TYPE_INT_ARGB) transparency is stored in the fourth byte (the A - Alpha channel). A = 0 transparent, A = 255 opaque.
The TYPE_BYTE_GRAY representation is just a single byte with the luminance component, there is no Alpha channel in this representation. When you draw the image into the new buffer, the luminance is computed from the RGB channels and stored. The Alpha channel is dropped.
The easiest way to achieve what you are doing is to compute the luminance yourself and store it in the three RGB channels of a new ARGB image copying the Alpha channel from the original to the now gray-scale image.
The luminance is the Y component in the YCbCr image representation. Wikipedia has details on how to compute it (it's just a weighted average of R, G and B).
The problem with this approach is that you lose hardware acceleration. Luckily Java provides classes to translate color spaces which (depending on platform) should be hardware accelerated, and more robust since they don't depend on the input image being ARGB. @MadProgrammer's detailed answer shows how to do this.