Results 1 to 6 of 6
  1. #1
    warchieflll is offline Member
    Join Date
    Dec 2010
    Posts
    55
    Rep Power
    0

    Default Transparant ImgIcon at background

    Hello all!

    I've a JPanel with a background. I want to add some transparants pictures on it (to start: a tree). The picture is a PNG, and I'm sure it is a transparant picture. Altough it still draws it as not-transparant:

    JPanel of background:
    Java Code:
      public LevelPaneel()
      {
        setLayout(null);
        addMouseListener(this);
        addMouseMotionListener(this);
        
        backgr = new ImageIcon("pics/achtergrond.png");
        background = backgr.getImage();
      }
    
    ...
    
      public void paintComponent(Graphics g)
      {
    	    int width = getWidth();
    	    int height = getHeight();
    
    	    g.drawImage(background, 0, 0, this.getWidth(),this.getHeight(), this);
      }
    And here the part of the tree: (note: the first three lines of 'SpelElement' I don't use anymore, but needs to be there, because when I delete it it won't show the tree at all...)

    Java Code:
    public class SpelElement extends JPanel
    {
      protected int breedte = 120, hoogte = 145;
      protected String tekst;
      protected Color kleur;
      protected ImageIcon boom, muur;
    
      public SpelElement(int x, int y)
      {
        tekst = "Boom";
        kleur = Color.GREEN;
        setBounds(x - (breedte / 2), y - (hoogte / 2), breedte, hoogte);
    
    	int MARGE = 0; // afstand vanaf de bovenrand
    	int KNOP_BREEDTE = 120; // breedte in pixels
    	int KNOP_HOOGTE = 145; // breedte in pixels
    	int KNOP_AFSTAND = 0; // verschuiving
    	int X_LOCATIE = 0; // linkerkant van de knop
    	
        JLabel imgLabel;
    	boom = new ImageIcon("pics/boom.png");
    	imgLabel = new JLabel(boom);
    	imgLabel.setBounds(X_LOCATIE, KNOP_AFSTAND + MARGE, 
    	KNOP_BREEDTE, KNOP_HOOGTE);
    	add(imgLabel);
    
    	System.out.println(imgLabel);
    	repaint();
      
      }
    Hope ya could help me out!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    For better help, post a very simple compilable and runnable program along with an image or a link to the image so we can see the problem on our own computers. The simpiler and shorter the code, the better your chances of getting decent help. Much luck!

  3. #3
    warchieflll is offline Member
    Join Date
    Dec 2010
    Posts
    55
    Rep Power
    0

    Default

    That's the problem... In a simple program I get it to work...

  4. #4
    rdjava's Avatar
    rdjava is offline Member
    Join Date
    Jan 2011
    Posts
    38
    Rep Power
    0

    Default

    My GameEngine performs this task simply. Here is the code.
    You can also cause the image to become translucent too.

    private BufferedImage mbt(BufferedImage image,
    final Color color){
    BufferedImage dimg = new BufferedImage(image.getWidth(),
    image.getHeight(), BufferedImage.TYPE_INT_ARGB);

    Graphics2D g = dimg.createGraphics();
    g.setComposite(AlphaComposite.Src);
    g.drawImage(image, null, 0, 0);
    g.dispose();
    for(int i = 0; i < dimg.getHeight(); i++) {
    for(int j = 0; j < dimg.getWidth(); j++) {
    if(dimg.getRGB(j, i) == color.getRGB()) {
    dimg.setRGB(j, i, 0x00FFFFFF);
    }
    }
    }
    return dimg;
    }

    private BufferedImage ti(BufferedImage image, float transperancy) {

    // Create the image using the
    BufferedImage aimg = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TRANSLUCENT);
    // Get the images graphics
    Graphics2D g = aimg.createGraphics();
    // Set the Graphics composite to Alpha
    g.setComposite(AlphaComposite.getInstance(AlphaCom posite.SRC_OVER, transperancy));
    // Draw the LOADED img into the prepared reciver image
    g.drawImage(image, null, 0, 0);
    // let go of all system resources in this Graphic
    g.dispose();
    // Return the image
    return aimg;
    }
    Last edited by rdjava; 01-07-2011 at 01:56 AM.

  5. #5
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Quote Originally Posted by warchieflll View Post
    And here the part of the tree: (note: the first three lines of 'SpelElement' I don't use anymore, but needs to be there, because when I delete it it won't show the tree at all...)
    This sounds to me like a build issue, where some classes were built using an older class file of SpelElement, and so when you change it, something is going haywire in the JVM. If that's what's going on, it might also be the reason your images are not properly transparent.

    Another possibility is that you are not setting 'setOpaque(false)' for whatever component the snipped 'paintComponent' you have in your OP belongs to. If your component is opaque, then it's still going to be visible if you draw an invisible image over it.

  6. #6
    warchieflll is offline Member
    Join Date
    Dec 2010
    Posts
    55
    Rep Power
    0

    Default

    Hehe, the 'setOpaque(false)' did the trick!! Thanks very much :)

    Ps. The three-lines-problem was indeed a build-issue. It works fine now :)

Similar Threads

  1. How to Run a .exe in background???
    By jazz2k8 in forum New To Java
    Replies: 9
    Last Post: 01-21-2011, 11:27 AM
  2. want to run my midlet in background!
    By jackofall in forum CLDC and MIDP
    Replies: 1
    Last Post: 03-31-2009, 07:14 PM
  3. Background in JFrame ( GUI).
    By Twister03 in forum AWT / Swing
    Replies: 2
    Last Post: 03-12-2009, 03:24 AM
  4. Background image
    By leiferouis in forum New To Java
    Replies: 9
    Last Post: 03-08-2009, 05:49 PM
  5. ImageIcon background
    By Yannick in forum New To Java
    Replies: 11
    Last Post: 02-12-2009, 04:13 PM

Tags for this Thread

Posting Permissions

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