One idea to use transparency is to draw a loaded image to a BufferedImage and then filter out a color that you want to be transparent.
//Filters a color out of an image (replaces with transparency) public static BufferedImage filterImage(Image i, Color c) { final int WIDTH = i.getWidth(null); final int HEIGHT = i.getHeight(null); final int COLOR = c.getRGB(); final Color CLEAR = new Color(0,0,0,0); GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice(); GraphicsConfiguration gc = gd.getDefaultConfiguration(); //The new image with the color filtered out BufferedImage temp; temp = gc.createCompatibleImage(WIDTH,HEIGHT,Transparency.BITMASK); //Gets the graphics of the temporary image Graphics2D g = temp.createGraphics(); //Copies the old image to this one g.drawImage(i,0,0,null); //Gets rid of extra resources used on the graphics obj. g.dispose(); //Find all COLOR pixels and make them transparent for(int x=0; x<WIDTH; x++){ for(int y=0; y<HEIGHT; y++) if(temp.getRGB(x,y) == COLOR) temp.setRGB(x,y,CLEAR.getRGB()); } return temp; }
Now you have the image just display it with
target.drawImage(img,positionX,positionY,null);
Also, another way to load images (from Brackeen's "Developing Games in Java") is to use
Image img = (new ImageIcon(file)).getImage();
It handles waiting for the image to finish loading and supports (as far as i can tell) .png and .gif.
(ImageIcon is in javax.swing.*)