Results 1 to 3 of 3
  1. #1
    mrhid6 is offline Member
    Join Date
    Sep 2011
    Posts
    37
    Rep Power
    0

    Default DrawImage with transparency ?

    Hello there,
    i have some code that uses drawimage and paints a png image on the jpanel i was wonder/hoping if i can make the drawimage have transparency and i would like to change it again in the program

    Java Code:
    Image aimimage = new ImageIcon("images/aim.png").getImage();
    g.drawImage(aimimage,0,0,null);

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: DrawImage with transparency ?

    Compositing Graphics (The Java™ Tutorials > 2D Graphics > Advanced Topics in Java2D)

    Notes:
    1. Don't load the image nor perform any disk I/O in a painting method override.
    2. Don't needlessly construct an ImageIcon if all you need is an Image. Load the Image using ImageIO or Toolkit.

    db

  3. #3
    Join Date
    Aug 2011
    Location
    Pretoria, South Africa
    Posts
    27
    Blog Entries
    3
    Rep Power
    0

    Default Re: DrawImage with transparency ?

    I hope this can help you.

    Java Code:
    import java.awt.Graphics;
    import java.awt.Window;
    import java.lang.reflect.Method;
    import javax.swing.ImageIcon;
    import javax.swing.JWindow;
    
    @SuppressWarnings("serial")
    public class JIcon extends JWindow {
    	public ImageIcon  iconImg;
    	
    	public JIcon(ImageIcon icon, float transLvl) {	
    		this.iconImg = icon;
    		this.setSize(icon.getIconWidth(),icon.getIconHeight());
    		
    		try {		
    			Class<?> awtUtilitiesClass = Class.forName("com.sun.awt.AWTUtilities");
    			Method method = awtUtilitiesClass.getMethod("setWindowOpacity", Window.class, float.class);	
    			method.invoke(null, this, transLvl);
    		} catch (Exception exc) {
    			exc.printStackTrace();
    		}
    		
    		this.setVisible(true);
    	}
    	
    	public void paint(Graphics g) {
    		g.drawImage(iconImg.getImage(),0,0,this);
    	}
    }

Similar Threads

  1. Transparency problem
    By Lacrim in forum Java Applets
    Replies: 1
    Last Post: 07-27-2011, 01:34 PM
  2. drawImage im 13 plz help
    By pseudohawtcakes in forum New To Java
    Replies: 3
    Last Post: 05-17-2011, 08:07 PM
  3. Filling with transparency
    By zirbinator in forum Java 2D
    Replies: 3
    Last Post: 02-09-2011, 02:36 AM
  4. ImageIcon Transparency
    By Lingerz in forum New To Java
    Replies: 2
    Last Post: 06-04-2010, 02:53 PM
  5. Setting an image's transparency?
    By aaroffl in forum AWT / Swing
    Replies: 1
    Last Post: 12-03-2008, 12:01 PM

Posting Permissions

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