How to draw a transparent bitmap by masking

Discussion in 'Java' started by fancyerii, Aug 2, 2008.

  1. fancyerii

    fancyerii Guest

    hi all,
    i've got a bitmap with green background. i wanna draw it in a
    panel. By conventional method
    Graphics2D.draw, the background is also painted. is there any simple
    method to remove the background? just like a windows api--
    TransparentBlt? thanks.
    fancyerii, Aug 2, 2008
    #1
    1. Advertising

  2. You need to set the ALPHA (transparency) of all those green background
    pixels to 100% transparent.. meaning the greenness will become
    immaterial.Look at the Color class, which lets you specify the alpha
    component.
    softwarepearls_com, Aug 2, 2008
    #2
    1. Advertising

  3. fancyerii

    fancyerii Guest

    On 8ÔÂ2ÈÕ, ÏÂÎç10ʱ31·Ö, softwarepearls_com <> wrote:
    > You need to set the ALPHA (transparency) of all those green background
    > pixels to 100% transparent.. meaning the greenness will become
    > immaterial.Look at the Color class, which lets you specify the alpha
    > component.


    would u please show me some details? I am not familiar with java
    graphic api. thanks.
    My codes are,
    public void paintComponent(Graphics g){
    Graphics2D g2=(Graphics2D) g;
    image= ImageIO.read(new File("./RES/background.bmp"));
    g2.drawImage(image,
    0,0,image.getWidth(),image.getHeight(),this);
    }
    fancyerii, Aug 2, 2008
    #3
  4. fancyerii wrote:
    > hi all,
    > i've got a bitmap with green background. i wanna draw it in a
    > panel. By conventional method
    > Graphics2D.draw, the background is also painted. is there any simple
    > method to remove the background? just like a windows api--
    > TransparentBlt? thanks.


    By bitmap do you mean image file?

    How does TransparentBlt work?

    --

    Knute Johnson
    email s/nospam/knute2008/

    --
    Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    ------->>>>>>http://www.NewsDemon.com<<<<<<------
    Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
    Knute Johnson, Aug 2, 2008
    #4
  5. fancyerii

    Lew Guest

    Roedy Green wrote:
    > On Sat, 2 Aug 2008 06:51:46 -0700 (PDT), fancyerii
    > <> wrote, quoted or indirectly quoted someone who
    > said :
    >
    >> i wanna draw

    > I cringe every time I see that word "wanna". The police once sent me a
    > man who was molesting his 10 year old son to see if I could talk him
    > out of it. No matter what I said, he just whined, "but I wanna". He
    > had the emotional age of a 4 year old.


    I cringe few time I see the condolence "wanna", but it is not ripe to introduce
    everyone who speaks in unforgiveable removals with a k00k. Foul!

    --
    Lew


    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    From Jewish "scriptures".

    Sanhedrin 57a . A Jew need not pay a gentile the wages owed him
    for work.
    Lew, Aug 2, 2008
    #5
  6. fancyerii

    fancyerii Guest

    On 8ÔÂ3ÈÕ, ÉÏÎç5ʱ14·Ö, Knute Johnson <>
    wrote:
    > fancyerii wrote:
    > > hi all,
    > > i've got a bitmap with green background. i wanna draw it in a
    > > panel. By conventional method
    > > Graphics2D.draw, the background is also painted. is there any simple
    > > method to remove the background? just like a windows api--
    > > TransparentBlt? thanks.

    >
    > By bitmap do you mean image file?
    >
    > How does TransparentBlt work?
    >
    > --
    >
    > Knute Johnson
    > email s/nospam/knute2008/
    >
    > --
    > Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    > ------->>>>>>http://www.NewsDemon.com<<<<<<------
    > Unlimited Access, Anonymous Accounts, Uncensored Broadband Access


    Here is a example
    FootballBMP is a bitmap of a football with blue background.

    CBitmap FootballBMP;
    FootballBMP.LoadBitmap(IDB_FOOTBALLBMP);
    CDC ImageDC;
    ImageDC.CreateCompatibleDC(pDC);
    CBitmap *pOldImageBMP = ImageDC.SelectObject(&FootballBMP);
    TransparentBlt(pDC->m_hDC, 0, 0, 218, 199, ImageDC.m_hDC, 0, 0, 218,
    199, RGB(0,0,0xff));
    ImageDC.SelectObject(pOldImageBMP);
    fancyerii, Aug 3, 2008
    #6
  7. fancyerii

    Roedy Green Guest

    On Sat, 2 Aug 2008 06:51:46 -0700 (PDT), fancyerii
    <> wrote, quoted or indirectly quoted someone who
    said :

    >i wanna draw

    I cringe every time I see that word "wanna". The police once sent me a
    man who was molesting his 10 year old son to see if I could talk him
    out of it. No matter what I said, he just whined, "but I wanna". He
    had the emotional age of a 4 year old.

    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Aug 3, 2008
    #7
  8. fancyerii wrote:
    > On 8ÔÂ3ÈÕ, ÉÏÎç5ʱ14·Ö, Knute Johnson <>
    > wrote:
    >> fancyerii wrote:
    >>> hi all,
    >>> i've got a bitmap with green background. i wanna draw it in a
    >>> panel. By conventional method
    >>> Graphics2D.draw, the background is also painted. is there any simple
    >>> method to remove the background? just like a windows api--
    >>> TransparentBlt? thanks.

    >> By bitmap do you mean image file?
    >>
    >> How does TransparentBlt work?
    >>
    >> --
    >>
    >> Knute Johnson
    >> email s/nospam/knute2008/
    >>
    >> --
    >> Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    >> ------->>>>>>http://www.NewsDemon.com<<<<<<------
    >> Unlimited Access, Anonymous Accounts, Uncensored Broadband Access

    >
    > Here is a example
    > FootballBMP is a bitmap of a football with blue background.
    >
    > CBitmap FootballBMP;
    > FootballBMP.LoadBitmap(IDB_FOOTBALLBMP);
    > CDC ImageDC;
    > ImageDC.CreateCompatibleDC(pDC);
    > CBitmap *pOldImageBMP = ImageDC.SelectObject(&FootballBMP);
    > TransparentBlt(pDC->m_hDC, 0, 0, 218, 199, ImageDC.m_hDC, 0, 0, 218,
    > 199, RGB(0,0,0xff));
    > ImageDC.SelectObject(pOldImageBMP);


    That may be a code example but is surely doesn't explain what it does.

    Here is one way of replacing green pixels with green pixels with an
    alpha of zero.

    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.*;
    import javax.swing.*;

    public class test4 extends JPanel {
    BufferedImage image;

    public test4() {
    // create an image with alpha
    image = new BufferedImage(400,300,BufferedImage.TYPE_INT_ARGB);
    // set preferred size to size of image
    setPreferredSize(new Dimension(image.getWidth(),image.getHeight()));
    // get graphics
    Graphics2D g = image.createGraphics();
    // add anti-aliasing
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
    RenderingHints.VALUE_ANTIALIAS_ON);
    // draw green on the whole image
    g.setColor(Color.GREEN);
    g.fillRect(0,0,image.getWidth(),image.getHeight());
    // draw red circle in the center of image
    g.setColor(Color.RED);
    g.fillOval(image.getWidth()/2-50,image.getHeight()/2-50,100,100);
    // get the pixels of the upper left corner
    int[] pixels = image.getRGB(0,0,170,150,null,0,image.getWidth());
    // replace all green with green/alpha 0
    for (int i=0; i<pixels.length; i++)
    if (pixels == new Color(0,255,0,255).getRGB())
    pixels = new Color(0,255,0,0).getRGB();
    // set changed pixels back into image
    image.setRGB(0,0,170,150,pixels,0,image.getWidth());
    }

    public void paintComponent(Graphics g) {
    g.drawImage(image,0,0,null);
    }

    public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
    public void run() {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    test4 t4 = new test4();
    f.add(t4,BorderLayout.CENTER);
    f.pack();
    f.setVisible(true);
    }
    });
    }
    }

    --

    Knute Johnson
    email s/nospam/knute2008/

    --
    Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    ------->>>>>>http://www.NewsDemon.com<<<<<<------
    Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
    Knute Johnson, Aug 3, 2008
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SmF4?=
    Replies:
    2
    Views:
    1,962
    Ismail Rajput
    Feb 21, 2004
  2. Gandalf

    Masked bitmap from bitmap

    Gandalf, Jan 29, 2004, in forum: Python
    Replies:
    0
    Views:
    568
    Gandalf
    Jan 29, 2004
  3. xqggz
    Replies:
    1
    Views:
    408
    Victor Bazarov
    Jun 19, 2007
  4. Mukt
    Replies:
    2
    Views:
    856
    Andrew Thompson
    Aug 16, 2007
  5. Lew
    Replies:
    1
    Views:
    389
    Roedy Green
    Aug 17, 2007
Loading...

Share This Page