r/javahelp • • May 05 '22

Unsolved Creating a GUI in java

I’ve been out of the programming game for a while, when I was in college i made GUIs with JavaFX. I was wanting to start to make a game just for a hobby, but then I found out the newest JDK no longer supports JavaFX. Was there a better one introduced or did they fall back onto AWT?

19 Upvotes

20 comments sorted by

View all comments

14

u/dionthorn this.isAPro=false; this.helping=true; May 05 '22

They made it open source and decoupled it from the JDK

https://openjfx.io/

I have a few game projects using modern java and JFX maven will make it very easy to get it working.

https://github.com/dionthorn/LifeSimRPG

1

u/thisismyaccountsmile May 05 '22

But is that the best option? Like did the decouple it because it was out of date and shouldn’t be used?

5

u/dionthorn this.isAPro=false; this.helping=true; May 05 '22

It is updated to jdk 18 by openjfx you can scope out their GitHub.

https://github.com/openjdk/jfx

Best GUI is based on your needs. Just need a basic GUI use Swing/AWT, need a little more control use JFX want 3D use LWJGL

4

u/hwaite May 05 '22

Just need a basic GUI use Swing/AWT

Why? For a greenfield project, there's no good reason not to use JavaFX. Especially if OP already has experience with it.

2

u/dionthorn this.isAPro=false; this.helping=true; May 05 '22 edited May 05 '22

No dependency/module management, especially when working from cmd line like OP it can be a headache until you figure it out, also swing awt is good enough for intellij I'm sure it's good enough for a basic GUI.

I really appreciate the JavaFX maven plugin from the openJFX folks though makes the dependency management alot easier.

3

u/thisismyaccountsmile May 05 '22

Thanks for your help

1

u/thisismyaccountsmile May 05 '22

Also is that something I can run from my cmd line? I used to use eMacs and just run things from cmd line, can I do that with that?

3

u/dionthorn this.isAPro=false; this.helping=true; May 05 '22

Check the openjfx.io website for install instructions you can do it from command line but java modules makes it a slight headache if you can manage maven itd be easier.

2

u/mk_gecko Intermediate Brewer May 05 '22

Yes.

I can write swing code from linux cmd line using vim and then compile and run it. I haven't tried it with the MigLayout library though.

You'll need Xwindows to run it though. This is built into Linux; for windows, get XMing.

1

u/mk_gecko Intermediate Brewer May 05 '22

Use Swing. If you want more control over component placement , use MigLayout .

tag /u/thisismyaccountsmile

2

u/dionthorn this.isAPro=false; this.helping=true; May 05 '22 edited May 05 '22

I found it difficult to gain per pixel control over canvas and Image objects for games in Swing it just felt easier in JavaFX.

https://github.com/dionthorn/2DTacticalRPG/blob/master/src/main/java/org/dionthorn/tacticalrpg2d/render/TileSet.java

And yes this is certainly over kill for what I'm using it for and no I am not a pro. >.> The pixel reader and pixel writer. I'm sure there is a way in Swing but I like this method.

In my render util I make heavy use of the Graphics Context from canvas

https://github.com/dionthorn/2DTacticalRPG/blob/master/src/main/java/org/dionthorn/tacticalrpg2d/render/RenderUtil.java

1

u/mk_gecko Intermediate Brewer May 06 '22

What you're doing is complicated. I do different things. It's not hard to control pixels on JPanels. You just have to make sure you're using the Jpanel size and not the JFrame size.
And sprites work well too.

The best and fastest graphics come from drawing to a buffered image and then drawing the image on the JPanel.