Results 1 to 5 of 5

Thread: How to draw transparent image in java???

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2003
    Posts
    4

    How to draw transparent image in java???

    Hello,
    Does anyone know how to draw a transparent image in Java. For example I have an image and want to set its black color transparent. I mean that I want black color not to be drawn.

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    I fear color keying is not included in the library.
    Unless you find something on the net (try searching for Java, Graphics and color keying or some such) you'll have to write a class that implements java.awt.Composite and that does the color keying for you.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Alternatively you can write a java.awt.image.ImageFilter that converts the image by setting the alpha for the black areas to 0. However I think this is harder.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4
    Addicted Member Phenix's Avatar
    Join Date
    Sep 2002
    Location
    Near A Cube
    Posts
    228
    I thought it would be as simple as using the alpha in a "new Color" because it does have such a constructor with Alpha, Red, Green, Blue, but I never figured it out.

    If you find out (and again, I think it is as simple as defining a new Color with alpha), please post your solution here.

  5. #5
    Member
    Join Date
    Jul 2002
    Posts
    50
    Here is code for a instantiable class that will help you draw transparent images/shapes onto an offscreen buffer which you can render and return an image. (See file attachment)

    How to use:
    Instantiate it, giving it width and height properties of the final image you wish to create (and optional background colour). If you have loaded a transparent GIF from file and have stored it as an Image type, you can use addImage function and it will render with its transparent properties intact. You can also use addAlphaImage function to control its alpha compositing properties (blending images over each other with translucency). The alpha parameter is a double value between 0.0 and 1.0 (0.0 being completely invisible, 1.0 being completely opaque).

    I wrote this class myself, it's fast and efficient, I have made many 2D java games with it. Hope it'll help you out.

    ps
    Don't forget to set the attachment's extension back to ".java"!
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width