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

JGO Wizard


Medals: 16
Projects: 19


Mojang Specifications


« Reply #30 - Posted 2006-10-18 13:44:59 »

Miners4k allocates the graphics objects once, and never clears them up. I've never heard of any bugs related to that.

The javadoc you quoted doesn't say you HAVE to dispose it, only that there's no point in disposing graphics objects you didn't create yourself. (Ie the only time you should call it is when you created it)

Play Minecraft!
Offline woogley
« Reply #31 - Posted 2006-10-18 13:51:52 »

Miners4k allocates the graphics objects once, and never clears them up. I've never heard of any bugs related to that.
I've done the same for every game I've made, the only time I heard of the bug is from oNyx/kevglass. have you tested all your games on a mac? (I know I haven't, heh)

(Ie the only time you should call it is when you created it)

yes, that's what I've said like 5 times in this thread. so if you create a Graphics on each frame, you should dispose of it since the next frame is going to create yet another one, and so on, and so forth..
Offline Markus_Persson

JGO Wizard


Medals: 16
Projects: 19


Mojang Specifications


« Reply #32 - Posted 2006-10-18 20:35:23 »

Ah, I see. I misread what you said all those times. :-)

Since I can't find any mention of it in the javadocs, I'm going to keep doing what I (and you) do, and will consider any bad behavior from it to be a bug in the java implementation.

Play Minecraft!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline zeroone
« Reply #33 - Posted 2006-10-18 21:09:30 »

So, is the final verdict to ignore the possibility that getGraphics() can return null, to create the Graphics objects only once on application start-up and to never dispose them?  That works for me even under OS X.  I never tried createBufferStrategy() though.
Offline woogley
« Reply #34 - Posted 2006-10-18 21:20:33 »

getGraphics() can definitely return null, that is in the javadocs. what's not in the javadocs is the exception you were talking about, as well as the "policy" of g.dispose()
Offline Abuse

JGO Knight


Medals: 14


falling into the abyss of reality


« Reply #35 - Posted 2006-10-18 22:06:10 »

Component.getGraphics()

"Creates a graphics context for this component. This method will return null if this component is currently not displayable."

Component.isDisplayable()

"... A component is made displayable either when it is added to a displayable containment hierarchy or when its containment hierarchy is made displayable. A containment hierarchy is made displayable when its ancestor window is either packed or made visible.....A containment hierarchy is made undisplayable when its ancestor window is disposed."

Component.setVisible(boolean)

"Shows or hides this component depending on the value of parameter b."

Window.dispose()

"Releases all of the native screen resources used by this Window, its subcomponents, and all of its owned children. That is, the resources for these Components will be destroyed, any memory they consume will be returned to the OS, and they will be marked as undisplayable."



Ergo, getGraphics() when invoked on a Window / Frame that has had setVisible(true) / show() called on it will never return null, until dispose() is called on the Window / Frame.

What is with all this talk of ultra-defensive programming!
We're talking about 4K games here  Roll Eyes

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline woogley
« Reply #36 - Posted 2006-10-18 22:07:48 »

We're talking about 4K games here  Roll Eyes

indeed, where hackery is an art - not bad design Tongue
Offline zeroone
« Reply #37 - Posted 2006-10-18 22:17:28 »

Does pack(), without a prior call to setVisible(true), make a JFrame "displayable"?

[edit] Nevermind.  I see above that pack() is sufficient.

[2nd edit] So, we don't need to check for null, but do we need to call getGraphics() once per game loop?
Offline woogley
« Reply #38 - Posted 2006-10-19 02:34:42 »

if you're pulling getGraphics() from the JFrame itself, I would check for null, because I've had it become null for reasons I can't explain. pulling from a JPanel hasnt had that problem for me, though. do what works for you, and cross your fingers. play around, test it out, experience it for yourself Wink
Offline Markus_Persson

JGO Wizard


Medals: 16
Projects: 19


Mojang Specifications


« Reply #39 - Posted 2006-10-19 10:43:10 »

I wouldn't bother checking for null either, for exactly the reasons abuse posted above.

Play Minecraft!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline zeroone
« Reply #40 - Posted 2006-10-19 13:28:41 »

Any final verdict on if we should request a new Graphics object once per game loop (and dispose it once per game loop) vs. just obtaining one at application start-up?
Offline woogley
« Reply #41 - Posted 2006-10-19 13:48:06 »

fact: when a window is minimized (in RedHat5, on my bro's computer at work), Graphics will return null until it is once again visible on the screen. I'm not sure if redhat is destroying the window or what, that'd seem dumb, but it is redhat. :\

fact: oNyx says he held a Graphics for an entire game session and it ate up RAM on a Mac

this shows you can't just trust the java implementation you're working with. I think this is why the javadoc adds "when a component is not displayable." different OSs handle windows differently, it seems. though I can't imagine why redhat would destroy a freakin window, and recreate it from scratch when it is maximized... I'll have to try some more redhat versions to see if they bothered to change that o_O

edit: just because I was curious, in Redhat5 I also tried holding onto the Graphics the entire time (since getGraphics() would return null when iconified), and after the window minimized, the VM crashed (without the hs_err_* file thing). :/
Offline zeroone
« Reply #42 - Posted 2006-10-19 14:04:02 »

I see.  Well then I better create a new Graphics object once per game loop and check for null.  But, it's probably okay to hold onto a Graphics object created from a BufferedImage as the visibility rules don't apply to it.
Offline woogley
« Reply #43 - Posted 2006-10-19 14:06:28 »

from a 4K standpoint, I'd say "f**k it" and do what you want, since most of it is hackery anyway, and *most* people are using Windows to run the games (as far as I know)

But, it's probably okay to hold onto a Graphics object created from a BufferedImage as the visibility rules don't apply to it.

as far as I know, but then again if the user is on a mac, they might have the RAM consumption problem oNyx had

edit:
moral of the story is this. while you develop you game, post it here so we can all test it and give you suggestions and such. Graphics is one of the easier problems to deal with, there are tons of other things that can happen. so, post at this forum, and we'll all test Wink
Pages: 1 [2]
  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.

PocketCrafter7 (14 views)
2014-11-28 16:25:35

PocketCrafter7 (10 views)
2014-11-28 16:25:09

PocketCrafter7 (11 views)
2014-11-28 16:24:29

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

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

toopeicgaming1999 (17 views)
2014-11-26 15:20:08

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

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

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

Gibbo3771 (28 views)
2014-11-24 19:59:16
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!