Forums Register Login

What is Java Swing good for?

+Pie Number of slices to send: Send
I was trying to make a game with solely Java Swing, but then after one week of tinkering, I notice that there is a lot of drawbacks.

My question is: what is Java swing meant for?
+Pie Number of slices to send: Send
Although a lot of people think JavaFX is better, Swing® works perfectly well for what it is designed for, viz desktop GUI applications. Please explain what “drawbacks” you found and what problems you encountered. Also tell us what sort of game you are writing. You shou‍ld be able to get Swing® to work for your game.

Moving to our GUIs forum.
+Pie Number of slices to send: Send
 

Frankie Law wrote:I notice that there is a lot of drawbacks.


Can you give some specifics on what drawbacks you encountered?
+Pie Number of slices to send: Send
I was trying to incorporate frames rates into the game along with JComponent, like JButton, JPanel, etc.
In C/C++ I would just erase the whole canvas and then redraw everything based on frames.

I am guess Swing doesn't use this line of logic to update its object.  
So I kept running in problems with Component wouldn't draw at all or if I add one component, the other disappear although they are clearly added in a sequence that can be seen.

Or I could be using Swing entirely incorrect way. LOL
+Pie Number of slices to send: Send
Start with a simple GUI program before you try something with lots of redraws:
Create a JFrame.
Create a JPanel and add it to the JFrame.
Create your buttons, etc., and add them to the JPanel using the appropriate layout manager.
Pack the JFrame.
Make the JFrame visible.
1
+Pie Number of slices to send: Send
Swing uses "layout managers" to add components to a panel. Depending on the layout manager you use the components will be displayed differently.

By default the content pane of the frame uses a BorderLayout, so that is a good place to start.

I would guess you want a main JPanel where you do your custom painting. You can add this to the "CENTERr of your frame.

Then you might have another panel containing the other Swing components and you add this panel to the "PAGE_END" of the frame.

Read the section from the Swing tutorial on Layout Managers. You might want to start with the section on using a BorderLayout for working examples to get you started.

The tutorial also has a section on "Custom Painting" you should read.
+Pie Number of slices to send: Send
I understand the JFrame stuff.  Didn't think about using a Layout Manager since all the components are automatically resize according to the layout.

So I had been using null for layout and try to setLocation for everything.

Probably not the best way to do, but that's the only way that I can get a image moving around.

2
+Pie Number of slices to send: Send
 

Frankie Law wrote:Or I could be using Swing entirely incorrect way. LOL



Yes, I think that's the main drawback you're encountering. It's not really fair to complain about something when you haven't really made an effort to learn how to use it. Now you haven't posted code for anything you've tried so far, but my guess is that you are going down the same road that many other people have gone down. You're struggling to make things work when what you're trying to do is completely at odds with how Swing actually works -- again, that's my impression.

But quite likely you didn't realize that there would be some learning involved before you could write working code. I think it would be a good idea for you to work your way through the Swing tutorial -- when you see the tutorial you'll also see that there's a lot to be learned. I know that instant gratification is a powerful need, though, so if you want to go the route of throwing together some non-working code and asking people here to point you in the right direction to get out of the mess, we do that a lot too. There are different ways to learn, after all.
+Pie Number of slices to send: Send
 

Paul Clapham wrote:

Frankie Law wrote:Or I could be using Swing entirely incorrect way. LOL



Yes, I think that's the main drawback you're encountering. It's not really fair to complain about something when you haven't really made an effort to learn how to use it. Now you haven't posted code for anything you've tried so far, but my guess is that you are going down the same road that many other people have gone down. You're struggling to make things work when what you're trying to do is completely at odds with how Swing actually works -- again, that's my impression.

But quite likely you didn't realize that there would be some learning involved before you could write working code. I think it would be a good idea for you to work your way through the Swing tutorial -- when you see the tutorial you'll also see that there's a lot to be learned. I know that instant gratification is a powerful need, though, so if you want to go the route of throwing together some non-working code and asking people here to point you in the right direction to get out of the mess, we do that a lot too. There are different ways to learn, after all.



Wow, I actually felt low ball reading this.  This is like saying to a baby "why can't you still not walk"
Sorry for even asking the question.  Never coming back here again.
+Pie Number of slices to send: Send
Null layouts are quite tricky to get to work - for laying out components using a LayoutManager is a better approach.

If you need to do the drawing in your code - e.g., if you have shapes that move around- use a Canvas in a JPanel.
+Pie Number of slices to send: Send
If you're doing graphicky game stuff, like a side scroller or platformer for example, the usual technique is to simply draw (as Tim suggests) onto the canvas, rather than trying to use the pre-built Swing classes.  Those classes are intended for producing applications for the main part.

So you'd have a game loop, and redraw the bits of the screen that have changed.
1
+Pie Number of slices to send: Send
 

I actually felt low ball reading this



Well, that is your problem, not the problem with the answer.

The tone of your question was that Swing was full of "drawbacks".

The answer was simply stating that Swing is different from C/C++.

The answer continued to suggest you need to do some basic reading first BEFORE passing judgement.

The answer then gave you a link to the Swing tutorial so that you could learn the Swing basics.

What you choose to do with the information provided is up to you but in the future questions should be asked with facts about the problem and leave your emotions out of the question.

+Pie Number of slices to send: Send

I have to admit, that when I first started with Windowing systems, it was with SunView, and later with XView*. And I absolutely loved them. So, when OpenLook lost to Motif, and I have to learn yet another Windowing environment, I found tons of "drawbacks" with them too. And then Sun released NeWS, and later TnT running on that of that, that was, admittedly ... well ... just weird.

Anyway, I guess in that sense, every Windowing environment has "drawbacks" -- with it doing stuff differently from your favorite Windowing environment. Those of us, who have had longer professional careers, I guess, tend to not have strong expectations, and hence, don't get as disappointed as easily.

Henry

* Okay, technically, Windows 1.0 for Dos 3.1 (I think) was my first Windowing environment... but that was so bad, I mentally black that out even today...
1
+Pie Number of slices to send: Send
In response to OP.

I once wanted to create a game (engine, and kinda both) using Swing and a JPanel.
https://www.youtube.com/watch?v=2FPLi-DgnBk - A Video of the work when I got somewhere with it

I have since moved on from that project (actually, I am rebuilding it in JavaFX : https://github.com/RepComm/NetGame ), but I do have another project I started after it using Swing as well.

https://www.youtube.com/watch?v=Hopa2_2izxk - A Video of an actual 'game' I was working on, it reuses some code from the above video, but is meant to simulate Minecraft redstone in 2d (with javascript)
https://drive.google.com/open?id=0B1BB8VZQZJnDYUdaOEktcnRnem8 - Link to the source, and build of the video at that time.
It renders using Swing as well.

It took quite a bit of trial and error to figure out how to force Swing to render when I wanted it to, but All the above links are my successes.
Please note that they are buggy according to my standards now, as they were me learning how to even get it to work.

I'm a huge fan of immediate-mode rendering (and OpenGL version > 2 ES removes that set of features), and that is why I used Swing at all.
Now I'm using JavaFX, and it certainly does let me mix immediate mode with scene graph well.

I don't mind if you use any of the code in that 7zip project, I figure someone else can learn with it too.
+Pie Number of slices to send: Send
Welcome to the Ranch
Enjoyed watching the little spot chasing the mouse.
+Pie Number of slices to send: Send
It seems that you are more advanced at games creating, compared to me. Where you have begun learning?
+Pie Number of slices to send: Send
Begun? I actually began with basic modding of a game called Starwars Battlefront (2004), and wanted to try making my own games with the early versions of GameMaker (over at YoYogames.com) back in the days before Studio launched (version 7), when I was 13 year old.

Then I ran into Unity3D around the next year, but I was more of a 3d modeller than a programmer in C#.
A major help for me when developing a game is actually designing so mock-up art before hand, that way I had something to try and implement.
Then the programming just became the struggle to make the art functional, instead of working with no real goal.

I'm a senior in highschool now, but I'm still learning things about Java. I've drawn a lot of inspiration from Unity3d's way of scenegraphing/gui/input, but I don't like C# or C++ much.

My current game project has the goal of actually making a lower learning curve for early programmers, but making developing happen
while you are in the game, that way you don't have to stop programming to test and see your work (and others can see it too, because all the scripting and art is streamed to clients over the server).

That is the goal, it's definitely not ready for use yet though. I just got vector art to stream and recreate over the network yesterday, and my anti-listener input system isn't
fully functional yet.

Campbell Ritchie wrote:Welcome to the Ranch


Thanks!
+Pie Number of slices to send: Send
 

Henry Wong wrote:...when I first started with Windowing systems, it was with SunView, and later with XView*. And I absolutely loved them. So, when OpenLook lost to Motif, ...



Heh, I was a fan of XView, too, and wasn't happy to see it lose.
Where does a nanny get ground to air missles? Protect this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 6299 times.
Similar Threads
want to get into Android, but quickly became frustrated with what's out there...
Passed SCJD 380/400 = 95%
Hello, Android Book Question
As requested, my certification story
Swing or SWT? (like this is new)
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 00:37:25.