r/javahelp • u/thisismyaccountsmile • 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?
14
u/dionthorn this.isAPro=false; this.helping=true; May 05 '22
They made it open source and decoupled it from the JDK
I have a few game projects using modern java and JFX maven will make it very easy to get it working.
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
3
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.
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.
1
u/mk_gecko Intermediate Brewer May 05 '22
Use Swing. If you want more control over component placement , use MigLayout .
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.
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
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.
5
u/wildjokers May 05 '22 edited May 05 '22
JavaFX was decoupled from the JDK so that its development could move at a pace independently from the JDK.
Your options for Java GUI programming are either Swing or JavaFX. Swing is still in the JDK so is very easy to get started with. It also is still a perfectly fine GUI toolkit and has great documentation.
JavaFX takes a bit more to get started with because you have to add it as a dependency and JavaFX getting started docs are completely garbage (makes it appear much harder than it actually is).
Swing uses AWT under the covers but you never want to use AWT directly.
-1
May 06 '22
[removed] — view removed comment
3
u/wildjokers May 06 '22
Oracle still contributes to JavaFX which shows that your statement is false.
0
1
u/BigData-ETL May 07 '22
JavaFx is no longer a part of JDK. You must add it manually. Please find the tutorial How to add JavaFX library to IntelliJ IDEA and Java 11-14?
https://bigdata-etl.com/how-to-add-javafx-library-to-intellij-idea/
•
u/AutoModerator May 05 '22
Please ensure that:
Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
You include any and all error messages in full
You ask clear questions
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.