Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
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  
  Issues with drawing layers of Graphics G and Graphics G2D  (Read 4789 times)
0 Members and 1 Guest are viewing this topic.
Offline digdugdiggy

Junior Devvie


Medals: 1



« Posted 2014-02-24 23:10:25 »

Hello!
I'm working on my first real game. It is tile-based, and I am only using a library to play mp3's at the moment.

I added a lighting effect to my game with G2D RadialGradientPaint and setComposite, and I am having troubles drawing anything on top of that layer in regular graphics g.

This class draws a light circle around the player which gets darker towards the edges and allows me to adjust the radius, simulating night/day vision distance somewhat.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
public void render(Graphics g) {  
        x = TileGame.getPlayer().getX() + 16;
        y = TileGame.getPlayer().getY() + 16;
       
        Graphics2D g2d = (Graphics2D) g;
        java.awt.geom.Point2D center = new java.awt.geom.Point2D.Float((float)x, (float)y);
       
        float[] dist = {0.0f, 1.0f};
        Color[] colors = {new Color(0.0f, 0.0f, 0.0f, 0.0f), Color.BLACK};
        RadialGradientPaint p = new RadialGradientPaint(center, currentRadius, dist, colors);
        g2d.setPaint(p);
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, .6f));
       
        g2d.fillRect(64, 64, 640, 640);
       
        g2d.dispose();
    }


In my main game loop, I have multiple things being rendered in a specific order.

1  
2  
3  
4  
5  
6  
                .  .  .  .
                levelManager.render(g);              
                ui.render(g);
                crops.render(g);
                player.render(g);                
                light.render(g);


My issue is this: In the above snippet, I want to draw UI.render() after the light.render() effect (which uses g2d). For some reason, if I put them in this order:

1  
2  
3  
4  
5  
6  
                .  .  .  .
                levelManager.render(g);  
                crops.render(g);
                player.render(g);                
                light.render(g);
                ui.render(g);

None of the UI gets rendered, even ones which weren't within the original  g2d.fillRect(64, 64, 640, 640) area. It seems like I need to cast my g2d back into g? Not sure how to solve this, I would appreciate some assistance.
Offline StumpyStrust
« Reply #1 - Posted 2014-02-24 23:40:17 »

Tip:

This should be how rendering methods work for objects to make sure you are not changing the graphics object passed in.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
public void render(Graphics g)
{
Graphics g2d = g.createGraphics(); //cast this to g2d
.
.
.
//render stuff

g2d.dispose();

}

What ever you call on graphics objects carry over to anything else using them like objects should. So if you say set a certain color or alpha composite then the next call to the object will have those applied. To keep from always having to track that, simply never change the "base" graphics object and just create a new one from it when you render.

Offline digdugdiggy

Junior Devvie


Medals: 1



« Reply #2 - Posted 2014-02-25 00:38:07 »

I'm trying what you suggested, but getting an error.

g.createGraphics(); :: cannot find symbol :: method createGraphics :: location variable g of type Graphics

What you're saying is that when I do "(graphics2D) g" cast, it actually changes the original g?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline digdugdiggy

Junior Devvie


Medals: 1



« Reply #3 - Posted 2014-02-25 00:41:16 »

Ok, so I found that my graphics g has a "create()" method, which returns a copy of the current graphics context.

1  
2  
3  
4  
public void render(Graphics g) {
       
        Graphics preG2D = g.create();
        Graphics2D g2d = (Graphics2D) preG2D;


This has fixed my original problem, at least I think so. What do you think?
Offline StumpyStrust
« Reply #4 - Posted 2014-02-26 15:52:31 »

Yup but you can make that one step like so

1  
2  
3  
4  
5  
6  
Graphics2D g2d = (Graphics2D)g.create();

draw stuff


g2d.dispose();

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.

rwatson462 (30 views)
2014-12-15 09:26:44

Mr.CodeIt (22 views)
2014-12-14 19:50:38

BurntPizza (43 views)
2014-12-09 22:41:13

BurntPizza (77 views)
2014-12-08 04:46:31

JscottyBieshaar (38 views)
2014-12-05 12:39:02

SHC (52 views)
2014-12-03 16:27:13

CopyableCougar4 (50 views)
2014-11-29 21:32:03

toopeicgaming1999 (116 views)
2014-11-26 15:22:04

toopeicgaming1999 (106 views)
2014-11-26 15:20:36

toopeicgaming1999 (32 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26: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
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!