Transparency and the Graphics2D object

Hi all at Java-Gaming!

I am very new here. I am actually quite new to Java as well. I apologize if this topic’s solution exists elsewhere, but I wasn’t entirely sure what I should search.

I am having a problem with Status bars in a little experimental game I am creating. The problem lies in the fact that when you draw a transparent image with a Graphics2D object, the transparent part of the image overwrites any previously drawn rectangles/circles etc. As an example;

http://puu.sh/6Y7ok.png

Rather, the bars should be filled with red and blue respectively. The other aspect I feel confident that I will be able to program, but this part has been bugging me for a while. My current code to resulting in the above is;

	public void draw(Graphics2D g, Color c) {
		
		g.setColor(c); //setting the back color
		g.drawRect(x, y, this.width, this.height); //drawing the backcolor
		
		g.drawImage(image, x, y, null); //overlaying the transparent gif image (BufferedImage image = ImageIO.read(getClass().getResourceAsStream("path");)
		
	}

Any insight, even an alternative way to deal with drawing the Status bars, would be greatly appreciated! :wink:

Try drawing the rectangle after the image :smiley:

  • Dan

This is a very simple solution, but not the best solution. If I do decide to change the image that borders the health pool, then I would have to recode the game to suit that. With drawing the overlay after drawing the health pool, I leave the health bar ever slightly more customizable.
Thank you for your input.

[icode]drawRect()[/icode] will draw an outline of a rectangle. I think you’re looking for [icode]fillRect()[/icode]. :smiley:

… I’m just gonna go over there and cry in the corner. Oh well, we all make mistakes :stuck_out_tongue: Thanks for your help! Sometimes it just takes another person to look at your code, I guess! :slight_smile:

Nice art by the way :smiley:

Not mine :stuck_out_tongue: Can’t take credit for it, unfortunately. I got the background from a tutorial… here!