Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (523)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (592)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Graphics2d Paint multiples problem  (Read 1127 times)
0 Members and 1 Guest are viewing this topic.
Offline JayTech

Junior Devvie


Medals: 1



« Posted 2012-02-01 08: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 Overlord »


Medals: 832
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2012-02-01 09: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

Junior Devvie


Medals: 1



« Reply #2 - Posted 2012-02-01 21: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! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 832
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2012-02-01 21: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

Junior Devvie


Medals: 1



« Reply #4 - Posted 2012-02-01 21:54:43 »

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

Junior Devvie


Medals: 1



« Reply #5 - Posted 2012-02-01 21: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 Kernel


Medals: 202



« Reply #6 - Posted 2012-02-01 22: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

Junior Devvie


Medals: 1



« Reply #7 - Posted 2012-02-01 22: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 Kernel


Medals: 202



« Reply #8 - Posted 2012-02-01 23: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

Junior Devvie


Medals: 1



« Reply #9 - Posted 2012-02-02 00: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! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline gerard_pp

Junior Devvie


Medals: 1



« Reply #10 - Posted 2012-02-02 17: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]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

SHC (24 views)
2014-11-25 12:00:59

SHC (23 views)
2014-11-25 11:53:45

Norakomi (22 views)
2014-11-25 11:26:43

Gibbo3771 (22 views)
2014-11-24 19:59:16

trollwarrior1 (36 views)
2014-11-22 12:13:56

xFryIx (74 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50

digdugdiggy (46 views)
2014-11-12 21:10:15

digdugdiggy (41 views)
2014-11-12 21:09:33

kovacsa (68 views)
2014-11-07 19:57:14
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!