Using Graphics2D instead of Nifty GUI

Hello, everyone. I’m sure this is a totally noobish question, but please bear with me…



I would like to use Graphics2D to draw stuff on the screen, instead of Nifty GUI. Is there a way to do this in JME?

If so, can you please show me how to go through with this? In particular, how to get the Graphics2D object to work with JME.



Thanks in advance, guys.

First off…if you are planning to go to Android at any time Graphics2d isn’t supported on Android.



Other than that you would need to convert the graphics 2d image into a texture, put the texture on a quad, and put the quad in the GUI node. I’ve seen some discussion of wrapping Java BufferedImages into textures from time to time but I’m not sure off hand what is available.



Be aware that graphics 2d is VERY slow though.

1 Like

Thanks for the response, @zarch. Guess I’ll abandon the Graphics2D idea for now since it’s that slow and does not work on Android…



The reason I wanted to stop using NiftyGUI is that I want to draw many lines of text on the screen and specifying all the layers and parameters in the xml file is getting annoying… What’s more, I haven’t found a way to resize the nifty elements in order to match different screen resolutions.



Nevertheless, could you please explain in a little more detail the part of “converting the graphics 2d image into a texture”? I’m a bit confused, but I would still like to know how it works.





Thanks a lot for your time!

I don’t know if it’s the most efficient path but one approach is to make a BufferedImage (for which you can get a Graphics2D object from) and then pass it to: http://hub.jmonkeyengine.org/javadoc/com/jme3/texture/plugins/AWTLoader.html#load(java.awt.image.BufferedImage,%20boolean)



…to get a JME Image to pass to a texture.



You can also look at that source and take apart the method to figure out how to go more directly from the BufferedImage data to an Image and maybe avoid recreating it every time it changes. That’s a little more involved.

@silentsword said:
The reason I wanted to stop using NiftyGUI is that I want to draw many lines of text on the screen and specifying all the layers and parameters in the xml file is getting annoying... What's more, I haven't found a way to resize the nifty elements in order to match different screen resolutions.


you can easily do both of those things using NiftyGUI -- rolling your own (advanced) GUI using textured quads is going to be tough, i promise. for the first issue, look into designing a basic 'many lines of text' framework (creating with xml vs. java doesn't make the slightest difference) and then look into dynamically updating the elements in that framework from java. for the second issue, design your elements using percentages rather than literals.

if you have trouble with anything, provide the details and i'm sure you'll have an answer almost immediately ;)

Also, if it’s just text then you can use BitmapText directly on the guiNode… depending on what you want out of it that may be the simplest approach depending on how fancy you are trying to be.

1 Like

Forget G2D! I’ve made class (based on another class) that does almost everything g2d does (though floodfill is kinda slow, but other than that it’s fine) that let you paint on textures in very graphics2dish way: http://hub.jmonkeyengine.org/groups/contribution-depot-jme3/snippets/55/. The only thing it’s missing is drawing text and bitmaps other than pixel by pixel, but other than that it should be fine (and as already mentioned, you can use BitmapText for that).

You know you can build Nifty screens in code (if you don’t like XML) and you can tell Nifty to word wrap etc as well? Nifty also gives you access to a lot of nice effects and suchlike that add a lot more polish to the game. (For example text fading in/out or sliding in rather than just appearing.



I’d recommend taking the time to learn Nifty TBH.



In Graphics2D if all you are doing is compositing text and images then it’s not terrible performance (still not fast) if you try and do anything on a per-pixel basis or add any nice effects though then it will really struggle… and as already mentioned it just doesn’t exist at all in Android.

@silentsword said:
What's more, I haven't found a way to resize the nifty elements in order to match different screen resolutions.


Stop looking. GUI Scaling isn't part of Nifty. It might be in the next version but... *shrug* That's not helping.

Although, I should specify that you could scale things up/down, but you'd have to do it manually.

Actually, there is an option in Nifty to design screens at a certain screen resolution and have it scale them to whatever screen res is being used. I’m not quite sure what/how it does as I’ve not used it but I know it is in there.

For most cases I use the relative positional stuff to code for any size screen (for example position something at y=75% and it is always 3/4 of the way down the screen, etc)

Yeah, nifty has GUI scaling since a few months now.

Thanks everyone for the replies. You guys rule! :slight_smile:



Considering all the options and the stuff I need to implement, I’ve decided to use BitmapText to display the text and NiftyGUI to display the images. I’ll use the inset parameter to scale the images for different resolutions and just scale the BitmapText accordingly.



Thank you again for your time, guys!

Note: JME also has Picture for displaying images if it works out better for you with the BitmapText.

1 Like
@normen said:
Yeah, nifty has GUI scaling since a few months now.


I must've missed that update. Good to know if the question pops up again.
@madjack said:
I must've missed that update. Good to know if the question pops up again.


Note: what zarch is talking about is whole screen rescaling. I only mention it because that is different than another topic where resizing a single text element was being discussed.
@pspeed said:
Note: what zarch is talking about is whole screen rescaling. I only mention it because that is different than another topic where resizing a single text element was being discussed.


Don't worry, I got that. :)