Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  Graphics2d Paint multiples problem  (Read 529 times)
0 Members and 2 Guests are viewing this topic.
Offline JayTech

Jr. Member
**

Posts: 59
Medals: 1



« on: 2012-02-01 03:41:22 »

One class Sprite has :
 
1  
2  
3  
4  
5  
6  
7  
public Graphics2D myColor(Graphics x){
   
    Graphics2D p= (Graphics2D)x;
     p.setColor(Color.RED;
   
    return p;
    }

Board Class has:
 
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
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);


  
Offline Riven
« League of Dukes »

JGO Kernel
*****

Posts: 5867
Medals: 255


Hand over your head.


« Reply #1 on: 2012-02-01 04:30:00 »

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).

Hi, appreciate more people! Σ ♥ = ¾

Learn how to award medals... and work your way up the social rankings
Offline JayTech

Jr. Member
**

Posts: 59
Medals: 1



« Reply #2 on: 2012-02-01 16:43:35 »

So how do I paint multiple seperate objects a different color? I'm new to Graphics g and 2d. Could use some help XD.
Games published by our own members! Go get 'em!
Offline Riven
« League of Dukes »

JGO Kernel
*****

Posts: 5867
Medals: 255


Hand over your head.


« Reply #3 on: 2012-02-01 16:45:49 »

1  
2  
3  
4  
5  
g.setColor(Color.RED);
g.fillRect(...); // red rectangle

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

Hi, appreciate more people! Σ ♥ = ¾

Learn how to award medals... and work your way up the social rankings
Offline JayTech

Jr. Member
**

Posts: 59
Medals: 1



« Reply #4 on: 2012-02-01 16:54:43 »

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

Jr. Member
**

Posts: 59
Medals: 1



« Reply #5 on: 2012-02-01 16:59:42 »

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
 
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
@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.
Offline sproingie

JGO Strike Force
***

Posts: 894
Medals: 55



« Reply #6 on: 2012-02-01 17:13:48 »

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.
Offline JayTech

Jr. Member
**

Posts: 59
Medals: 1



« Reply #7 on: 2012-02-01 17:36:46 »

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.
Offline sproingie

JGO Strike Force
***

Posts: 894
Medals: 55



« Reply #8 on: 2012-02-01 18:30:28 »

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:
1  
        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.
Offline JayTech

Jr. Member
**

Posts: 59
Medals: 1



« Reply #9 on: 2012-02-01 19:42:08 »

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
Games published by our own members! Go get 'em!
Offline gerard_pp

JGO n00b
*

Posts: 31
Medals: 1



« Reply #10 on: 2012-02-02 12:25:39 »

One class Sprite has :
 
1  
2  
3  
4  
5  
6  
7  
public Graphics2D myColor(Graphics x){
   
    Graphics2D p= (Graphics2D)x;
     p.setColor(Color.RED;
   
    return p;
    }



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

Warning: Plz, excuse my bad english...
Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.1 seconds with 19 queries.