Graphics2d Paint multiples problem

One class Sprite has :

public Graphics2D myColor(Graphics x){
	
	 Graphics2D p= (Graphics2D)x;
	  p.setColor(Color.RED;
	
	 return p;
	 }


Board Class has:

public void paint(Graphics g) {
        super.paint(g);
		   	 
        
		 
		  bg = new ImageIcon("Trees!.png").getImage();
        
		  
  		  Graphics2D gfx= (Graphics2D)g;  
		  g.drawImage(bg, 0,0,this);	 
		  gfx.setColor(Color.BLUE);
		
		 
		 Rectangle r = new Rectangle(sp.getX()-20, sp.getY()-55,70,20);
		 
		 g.drawString(sp.getMTag(), sp.getX()-20, sp.getY()-10); 
	   
		 sp.myColor(g).fillRect(sp.getX()-20, sp.getY()-55,sp.returnHealth() ,10);
		
		gfx.draw(r);
		 
               
		  g2d.dispose();
	 }
   Why are the two seperate rectangles the same color???
sp.myColor(g).fillRect(sp.getX()-20, sp.getY()-55,sp.returnHealth() ,10);
gfx.draw(r);

Because ‘g’ == ‘gfx’. It’s just another variable pointing to the same object. So if you call setColor(…) on either of them, you’ll have set the color of the (single) object they refer to.

Whether you call ‘g.fillRect(…)’ or ‘gfx.fillRect(…)’, doesn’t really matter, all shapes will be drawn with the color you specified most recently (red).

So how do I paint multiple seperate objects a different color? I’m new to Graphics g and 2d. Could use some help XD.


g.setColor(Color.RED);
g.fillRect(...); // red rectangle

g.setColor(Color.BLUE);
g.fillRect(...); // blue rectangle

Thanks!! Gotcha, after I set a color I have to draw it immediately and not setColor(…) setColor(…) draw(…)) draw(…).

Also, how can I combine a Rectangle + Image + drawString into a single Image? Is that possible? Currently I have to paint 3 seperate things:

Map/Board class

@Override  
	 public void paint(Graphics g) {
        super.paint(g);
		   	 
       
		 
		  bg = new ImageIcon("Trees!.png").getImage();
         Rectangle r = new Rectangle(sp.getX()-20, sp.getY()-55,70,20);
		  Graphics2D g2d = (Graphics2D)g;
  		  Graphics2D gfx= (Graphics2D)g;  
		  g.drawImage(bg, 0,0,this);	 
		  gfx.setColor(Color.BLUE);
		  gfx.fillRect(sp.getX()-20, sp.getY()-55,sp.returnHealth() ,10);//healthbar fill
		  gfx.setColor(Color.RED);
		  gfx.draw(r);//Health bar frame
		  g.drawString(sp.getMTag(), sp.getX()-20, sp.getY()-10); //Monster Name 
	     g2d.drawImage(sp.imgArray().get(sp.getFrame()), sp.getX(), sp.getY(), this);//Monster Sprite
		 // g2d.drawImage(spO.getImage(), spO.getX(), spO.getY(), this);
		  Toolkit.getDefaultToolkit().sync();
               
		  g2d.dispose();
	 }

And once this gets painted I have a monster with a name above its head and a health bar, is there a more clean way to combine these 3 draws into a single image. I mean instead of painting these separately is there any way do it in one call OR am I doing this the right way( I feel that this isn’t though) please correct me if I am wrong.

You can paint them onto an offscreen image then paint that. You do that by creating a BufferedImage, then calling its getGraphics() and passing that to your paint method to paint to that image instead of the screen. Once you’ve “captured” that image, you just draw it like any other image. Google for “java offscreen image” for more info on that.

This approach gets really wasteful of memory, especially when you’re dealing with animated sprites, so use a profiler and make sure the extra memory use is worth any gains in rendering speed.

I mean I just want to get clutter of code away instead of doing calling draw several different times, what’s the most professional approach to doing so, i’m trying to keep this contained mostly in the sprite class instead of calling this in the Board class’s paint, is this possible, thanks.

Just break it out into another method. You should get used to being able to cut any chunk of code and paste it into a method. Most IDEs have an “extract method” refactoring for just this sort of thing. When you’re used to method refactoring, then you can get started on refactoring your object model and moving things into other classes and methods on those classes. I don’t have a ready-made answer there; I’m afraid any code example for this that I paste is going to lose the point, and that you need to arrive at it yourself by way of understanding the structure and flow of your program.

Also, one problem I didn’t notice before, but one you should fix immediately is this one:


        bg = new ImageIcon("Trees!.png").getImage();

You need to move that ImageIcon constructor out of the paint() method and store it in a property, otherwise you are loading the image on every frame.

WOW thank’s for pointing that out no wonder I was noticing a drop in fps!! Thanks, yea i’ll look into refractoring and extracts thanks for the advice =D

Hello
“)” is missing, but i guess you have already fixed that out.