Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 22 of 22

Thread: Transparency to bufferedImage

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Transparency to bufferedImage

    Hey, im a complete noob to java, but im trying to learn.

    The problem i have now is that i want to make some pixels transparent in my bufferedimage.
    What i "think" i've done so far is making an applet that paints to a JFrame.
    I start by loading in an image by the imageIO.read() function.
    now i need to make my paint-picture background transparent.

    If anyone could help i'd be very thankful. I appreciate if you can explain it as simple and throughly as possible as i'm kinda new to Java...

    I copy pasted my code so far below



    import java.applet.Applet;
    import javax.swing.*;
    import java.io.*;
    import javax.imageio.ImageIO;
    import java.awt.*;
    import java.awt.Image;
    import java.awt.image.BufferedImage;
     
     
    public class Testing extends Applet{
      BufferedImage image;
      Graphics2D g2d;
      JFrame frame;
     
      public Testing(){}
     
      public void init(){
        image = new BufferedImage(300, 300, BufferedImage.TYPE_INT_ARGB);
        g2d = (Graphics2D) image.createGraphics();
     
        frame = new JFrame();
     
        try{
          image = ImageIO.read(new File("bump.png"));
        }
        catch(IOException e){}
     
        frame.getContentPane().add(this);
        frame.setVisible(true);
        frame.setSize(500,500);   
        paint(g2d);
      }
     
      public void paint(Graphics g){
        Graphics2D g2d = (Graphics2D) g;
        g2d.drawImage(image, 0,0, 100, 100, null);
      }
    }
    Last edited by Serthy; August 17th, 2013 at 05:47 PM. Reason: code tags


  2. #2
    Junior Member
    Join Date
    Aug 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Transparency to bufferedImage

    you had not written main method in your program

  3. #3
    Junior Member
    Join Date
    Aug 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Transparency to bufferedImage

    The program itself is working and displays my picture. I think that the class as it implements "runnable" pretty much replaces the need for a main method.

    Guess i could remake that since i only got it from someone else on the internet.

    But everything works as it should.

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Transparency to bufferedImage

    Quote Originally Posted by Serthy View Post
    I think that the class as it implements "runnable" pretty much replaces the need for a main method.
    The run method is empty, so nothing would happen if/when run was called. The reason it "runs" is because it extends Applet, but that is another story.
    Please use code tags when posting code, help can be found on the Announcements page.
    See the docs on the BufferedImage class for methods to gain access to individual pixels

  5. #5
    Junior Member
    Join Date
    Aug 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Transparency to bufferedImage

    Ahh i see you were right, the run method wasn't necessary at all so i've removed it.

    But if there is anyone out there that could help me understand how to make my picture transparent i'd be glad.
    I appreciate that you're linking me related "things" but as im new to all this it doesn't tell me much. thanks anyway though

  6. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Transparency to bufferedImage

    Quote Originally Posted by Serthy View Post
    I appreciate that you're linking me related "things" but as im new to all this it doesn't tell me much. thanks anyway though
    I think it is less like "related things" and more like "the actual documentation" that goes to the class you are using. It can not get any more related than that.
    If you spend a few minutes reading the documentation for the class you are using you may just find what you are looking for.
    Sorry we do not just provide working solutions here, we would much rather leave you the opportunity of discovery and the experience of problem solving.

  7. #7
    Junior Member
    Join Date
    Aug 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Transparency to bufferedImage

    The thing is that i've been stuck on this problem for over a week now, and i've allready looked at the bufferedimage class (and about 100000 other classed that might be related to the case) over and over again, even before you posted it.
    As i said im kinda new to java, and so far I've been able to more or less fix every problem i've encountered by reading documentations, looking on youtube, reading forums etc..

    But this was the one problem I can't figure out on my own, thats why I'm asking for help. So even if you don't want to post written code, please tell me what i need to do or what to use.

  8. #8
    Junior Member
    Join Date
    Aug 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Transparency to bufferedImage

    bump

  9. #9
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Transparency to bufferedImage

    Quote Originally Posted by Serthy View Post
    please tell me what i need to do or what to use.
    You were already told what to do, read the documentation. You have had > a week to read one page. What did you find?

  10. #10
    Junior Member
    Join Date
    Aug 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Transparency to bufferedImage

    as i said in my post above, i've read the documentation about a million times allready, giving me nothing.
    I see you dont want to help too much but i'd be really glad if i could get just a small hint of what i should do.

    What i've got from the documentation was trying this:
    - changing the type of bufferedimage to TYPE_4BYTE_ABGR
    - seting the color with setRGB(x,y, 0x00000000)

    and some other similar solutions but all i've managed is to make the picture black.

  11. #11
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Transparency to bufferedImage

    What code did you use to make the picture black?
    What happens when you use setRGB?

  12. #12
    Junior Member
    Join Date
    Aug 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Transparency to bufferedImage

    using "for loops" over all pixels and then use setRGB(x,y, 0x00000000)
    but this only makes it black.

  13. #13
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Transparency to bufferedImage

    We can not see what is wrong without the code

  14. #14
    Junior Member
    Join Date
    Aug 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Transparency to bufferedImage

    only difference from the code above is the for-loops


    import java.applet.Applet;
    import javax.swing.*;
    import java.io.*;
    import javax.imageio.ImageIO;
    import java.awt.*;
    import java.awt.Image;
    import java.awt.image.BufferedImage;
    import java.awt.image.*;
     
     
    public class Testing extends Applet{
      BufferedImage image;
     Graphics2D g2d;
      JFrame frame;
     
      public Testing(){}
     
      public void init(){
        image = new BufferedImage(300, 300, BufferedImage.TYPE_4BYTE_ABGR);
        g2d = (Graphics2D) image.createGraphics();
     
        frame = new JFrame();
     
        try{
          image = ImageIO.read(new File("bump.png"));
        }
        catch(IOException e){}
     
     
        for (int i=0; i<image.getWidth(); i++){
          for (int j=0; j<image.getHeight(); j++){
        image.setRGB(i,j,0x00000000);
          }}
     
        frame.getContentPane().add(this);
        frame.setVisible(true);
        frame.setSize(500,500);   
        paint(g2d);
      }
     
      public void paint(Graphics g){
        Graphics2D g2d = (Graphics2D) g;
        g2d.drawImage(image, 0,0, 100, 100, null);
      }
    }

  15. #15
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Transparency to bufferedImage

    The catch block is ignoring the exception that explains the cause of the problem.
    Never ignore exceptions, especially during testing, but that is a different story.

    For now comment out the ENTIRE try/catch block worth of code, then start playing with the numbers in setRGB(). Use some values above 0 so you can see some color, it should work

  16. #16
    Junior Member
    Join Date
    Aug 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Transparency to bufferedImage

    It kind of works... I've commented away the try/catch as u said. Now i can set the transparency of the picture.

    What i've done now to se how it works is painting the image over a rectangle. (g.fillRect(...) )

    Painting my transparent blue image over a red rectangle turned out like this... not entierly correct, but atleast it's transparent.



    pic.jpg


    As you can see the image is not "continuous" (if that's the right word to describe it)

    I havn't changed anything in the code except adding:

    g2d.setColor(new Color(255,0,0));
        g2d.fillRect(0,0,200,200);
        g2d.drawImage(image, 0,0, 100, 100, null);

    to the paint method.
    Also i've changed 0x00000000 to 0x220000FF

  17. #17
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Transparency to bufferedImage

    I did notice some other issues, with the Applet and a JFrame, etc...
    If you need more help with it post the revised code and your new question

  18. #18
    Junior Member
    Join Date
    Aug 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Transparency to bufferedImage

    How can i load my picture to my program if loadImage doesn't work?

  19. #19
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Transparency to bufferedImage

    What do you mean load image doesn't work?

    Again, post the revised code so we can see what is going on. ...and not one snippet at a time, I will not scroll up and down the thread and try to remember where changes were made, post it all in one place.

    image = ImageIO.read(new File("bump.png"));
    Load a ( create a new file ) ? What could you possibly be loading?

  20. #20
    Junior Member
    Join Date
    Aug 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Transparency to bufferedImage

    As you said i've "removed" the try and catch. So now my image is nothing more than a plain (blue) image. So the problem with making it transparent were indeed lying in the loadImage method, or atleast something has to be added after the loadImage, so i can make it transparent again.

    Also, why does the transparent image "split" when im painting it over another painting? It's as if both pictures is painted on the same plane and overlap each other, even though im painting them in order?


    import java.applet.Applet;
    import javax.swing.*;
    import java.io.*;
    import javax.imageio.ImageIO;
    import java.awt.*;
    import java.awt.Image;
    import java.awt.image.BufferedImage;
    import java.awt.image.*;
     
     
    public class Testing extends Applet{
      BufferedImage image;
     Graphics2D g2d;
      JFrame frame;
     
      public Testing(){}
     
      public void init(){
        image = new BufferedImage(300, 300, BufferedImage.TYPE_4BYTE_ABGR);
        g2d = (Graphics2D) image.createGraphics();
     
        frame = new JFrame();
     
      /*  try{
          image = ImageIO.read(new File("bump.png"));
        }
        catch(IOException e){}
      */  
     
        for (int i=0; i<image.getWidth(); i++){
          for (int j=0; j<image.getHeight(); j++){
        image.setRGB(i,j,0x990000FF);
          }}
     
        frame.getContentPane().add(this);
        frame.setVisible(true);
        frame.setSize(500,500);   
        paint(g2d);
      }
     
      public void paint(Graphics g){
        Graphics2D g2d = (Graphics2D) g;
        g2d.setColor(new Color(255,0,0));
        g2d.fillRect(0,0,200,200);
        g2d.drawImage(image, 0,0, 100, 100, null);
      }
    }

    result of the program:
    aa.jpg

  21. #21
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Transparency to bufferedImage

    Work with either the applet or the jframe, you do not need both.
    The Graphics2D object created off the BufferedImage is also unnecessary.
    Decide whether you need to be extending applet or drawing on a jframe, and use the Graphics object provided in the paintComponent method to draw the image

  22. #22
    Junior Member
    Join Date
    Aug 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Transparency to bufferedImage

    Ok thanks for the tips, as i said i'm quite new to java but im here for learning

    I've now removed the Jframe and the Graphics object.
    It solved why the pictures were painted "in" each other.

    (Also i removed the rect in the background and set the background color instead.)

    Now the only problem is when i try to load an image with imageIO.read() i can no longer set the picture to transparent.

    Without loading anything i'm able to set the bufferedImage to any transparency and Color but as soon as i've loaded something in it i can set Color but not transparency.


    import java.applet.Applet;
    import java.io.*;
    import javax.imageio.ImageIO;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.image.BufferedImage;
     
     
    public class Testing extends Applet{
      BufferedImage image;
     
      public Testing(){}
     
      public void init(){
        image = new BufferedImage(300, 300, BufferedImage.TYPE_4BYTE_ABGR);
     
        try{
          image = ImageIO.read(new File("bump.png"));
        }
        catch(IOException e){}
     
     
        for (int i=0; i<image.getWidth(); i++){
          for (int j=0; j<image.getHeight(); j++){
            image.setRGB(i,j,0x2200FFFF);
          }}
     
        setBackground(new Color(255,0,0));
      }
     
      public void paint(Graphics g){
        Graphics2D g2d = (Graphics2D) g;
        g2d.drawImage(image, 0,0, 100, 100, null);
      }
    }


    --- Update ---

    Ok i think i've managed to fix my problem in another way (By reading some more on this awesome forum).
    Instead of overwriting the pixels with transparent pixels i instead load an image already drawn transparent. Would be fun to know how i could overwrite pixels with transparent ones in the future though but for now my problem is solved.

    Thanks for your amazing help

Similar Threads

  1. Resizing an image without losing transparency
    By beansnbacon in forum AWT / Java Swing
    Replies: 1
    Last Post: July 31st, 2013, 07:59 AM
  2. Setting a transparency level of a picture issue
    By Asido in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 18th, 2012, 05:42 AM
  3. Replies: 3
    Last Post: July 17th, 2012, 11:59 PM
  4. Image transparency issue
    By Brt93yoda in forum Java Theory & Questions
    Replies: 6
    Last Post: August 31st, 2010, 02:25 PM
  5. Creating a bufferedimage!
    By Vexst in forum Java ME (Mobile Edition)
    Replies: 0
    Last Post: June 16th, 2010, 08:05 AM

Tags for this Thread