How to make the image 70% transparent

OP
OP used Ask the Experts™
on
hi all,
       would like to know how to make the .GIF(image only has black and white colur) 70% transparent for black and 100% transparent for white. heres my codes,

public int convertPixelToGray(int rgb)
{int grayShade4, grayShade5, grayShade6;
     int transparency=(rgb >> 24) & 0x50;
     int red  = (rgb >> 16) & 0x50;
     if (red <= 255)
     grayShade4 = 0;
          else
     grayShade4 = 77;
     int green = (rgb >> 8)  & 0x50;
     if (green <= 255)
     grayShade5 = 0;
          else
     grayShade5 = 77;
     int blue = rgb & 0x50;
     if (blue <= 255)
     grayShade6 = 0;
          else
     grayShade6 = 77;
return ( (transparency<< 24)|(grayShade4 << 16)|(grayShade5 << 8)|grayShade6);
}

dont know y, the image cant even be displayed. could anyone help me pls........
         
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Java Developer
Top Expert 2010
Commented:
Use an RGBImageFilter:

class MyFilter extends RGBImageFilter
{
   public int filterRGB(int x, int y, int rgba)
   {
      float alpha = 0xff;
      if (rgba==0)
      {
         alpha *= 0.7;
      }
      return ((rgba&0x00ffffff)|(((int)alpha) << 24));
   }
}  
OP

Author

Commented:
i tried to implement this into my program but the outcome is 2 black box. is it because my program is an applet? i already extends Applet, how to extend RGBImageFilter???

pls pardon me, im still at the learning stage.

thank you.
Mick BarryJava Developer
Top Expert 2010

Commented:
The MyFilter class above already extends RGBFilter.
How are you currently using it?
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

OP

Author

Commented:
i used media tracker to call the image, then i use pixel grabber to convert the pixels into an array. but theres an error telling me that the method cant be applied because the definition is not correct.
Mick BarryJava Developer
Top Expert 2010

Commented:
Whats the exact error and what line does it refer to.
Mick BarryJava Developer
Top Expert 2010

Commented:
and how are you calling it?
OP

Author

Commented:
the error state,

applyAlpha(int[],int) in GrabberImage cannot be applied to (int)pixels[i]= applyAlpha(pixels[i]);
----------------------------------------------------------
i use the following method to call it,

public void init() {
          MediaTracker mt  = new MediaTracker(this);
          URL            url = getClass().getResource("copyright.gif");

          try {
               im = createImage((ImageProducer)url.getContent());
               mt.addImage(im, 0);
               mt.waitForID(0);
          }
          catch(Exception e) { e.printStackTrace(); }

          int[] pixels = new int[w*h];
          PixelGrabber pg = new PixelGrabber(im, 110, 5, w, h,
                                                       pixels, 0, w);
          try {
               pg.grabPixels();
          }
          catch(InterruptedException e) {
               e.printStackTrace();
          }
          for (int i=0; i<pixels.length; i++)
                  pixels[i]= applyAlpha(pixels[i]);
----------------------------------------------------------
and the following to display the image,

public void paint(Graphics g) {
          g.drawImage(im,0,0,this);
                 }
Mick BarryJava Developer
Top Expert 2010

Commented:
a. pixels[i]= applyAlpha(pixels[i]);

You have not supplied the alpha parameter.

b. the original image will still get displayd.
You need to create a new image with the modifid pixels if you want to see the changes.
OP

Author

Commented:
where do i apply the parameter?

Commented:
Dear expert(s),

A request has been made to delete this Q in CS:
http://www.experts-exchange.com/Community_Support/CleanUp/Q_20406038.html

Without a response in 72 hrs, a moderator will finalize this question by:

 - Saving this Q as a PAQ and refunding the points to the questionner

When you agree or disagree, please add a comment here.

Thank you.

modulo

Community Support Moderator
Experts Exchange
Mick BarryJava Developer
Top Expert 2010

Commented:
I disagree.
I have not only explained the problem with the current code, but have also suuplied an alternative filter.
OP

Author

Commented:
but i did not use the code you provided. the second set of codes that i listed was totally from my fren. and it was he who helped me solved the problem.

but thanks for the help, objects.

to moderators,
im willing to give objects the points he deserved. lets closed this with everyone being happy. but please delete this Question. thank you.


Commented:
Hi OP,

It's ofcourse possible that multiple ways of coding lead to the same effect.
I'm glad you support objects comment, but I can't both delete a Q and have it answered.
One of the intentions of EE is to build a knowledge base of answered Q's for people to find a solution. This Q is even offering two !

modulo

Community Support Moderator
Experts Exchange
OP

Author

Commented:
dear moderator,
               i agree with you. but the codes i listed was from my fren whom wishes to have it deleted. i pasted it without asking his consent, its totally my fault. i dont mind the question being available, but would be grateful if you could delete the codes i pasted on 11/21/2002 08:28PM PST


other comments and Q are fine.

hope you understand. thank you.

lastly, how do i award objects points? thank you.

Commented:
I've removed the code as it's already described by it's merits.

For awarding the points you can select the link "accept as answer" you'll see in the comment-line of objects comment.

OK ?

modulo

Community Support Moderator
Experts Exchange
OP

Author

Commented:
ok. thank you very much.
OP

Author

Commented:
thanks for your help, objects.
Mick BarryJava Developer
Top Expert 2010

Commented:
:-)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial