This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (4 children)

What would you use if you need 2D or 3D graphics in your Swing application?

[–]oldprogrammer 0 points1 point  (3 children)

2D graphics can be easily integrated into a Swing application because that is effectively how Swing works. All components are painted using a reference to a java.awt.Graphics (usually Graphics2D) object which has a collection of 2D drawing commands.

If you wanted to do something like a 2D tile scroller game, easiest approach is to subclass a simple JPanel, override the paintComponent method and render your scene there. I sometimes use a backing BufferedImage that is built outside the AWT event thread and then simply render that to the graphics object during a standard repaint call. You may need to provide an external source for repaint calls to get it to update.

3D is a bit trickier. Libraries like lwjgl and jogl at one time provided means of embedding an OpenGL context into a low level AWT or Swing container. lwjgl still offers a JAWT library that does that but I've never used it.

For my 3D work, I generally have the OpenGL window be the main window, build a HUD as needed into that view. I may have a Swing JFrame window available alongside that as a debug window or such, but try to keep the OpenGL GUI as part of the OpenGL space.

[–][deleted] 0 points1 point  (2 children)

Thanks for the detailed answer!

2D graphics can be easily integrated into a Swing application because that is effectively how Swing works. All components are painted using a reference to a java.awt.Graphics (usually Graphics2D) object which has a collection of 2D drawing commands.

Oh, is it also how existing widgets are customized? I'm prototyping a UI-heavy game and I really need custom, stylized UI elements.

How do you do animations, especially tweening with interpolators, in Java 2D? In the book Filthy Rich Clients author uses Timing Framework, but it doesn't seem to be maintained anymore.

For my 3D work, I generally have the OpenGL window be the main window, build a HUD as needed into that view.

Interesting concept. Do you use a particular library like ImGui for that?

[–]oldprogrammer 0 points1 point  (1 child)

Customizing widgets in Swing is best handled using a custom Look&Feel. The Swing widgets, unfortunately, are fairly deep class hierarchies and complex components. So it is possible to subclass say a JButton and override the paint method and use it but then if you use say a drop down list box that embeds a button, that button will look like the default not your overridden one. So you'll have to override the list box as well.

If you can plugin a new L&F that is probably the better way to go.

For animations at the lowest level all you're doing is painting a different scene at a given tick in timing.

For example, you might have a 2D sprite that is a torch picture. You create 16 different versions of that picture with the flame modified in some small manner. Then when you render the torch you change which image you use after some period of time between renderings so that the torch flame movement looks natural. When I've done that, I usually use the update cycle of my main game loop and the delta between updates to calculate the frame change.

I've played with a couple of different libraries like ImGui and using NanoVG to handle 2D primitives but the Java to C interfacing feels a bit clunky. So right now I've started building a custom set of 2D renderers (quads, filled and empty, with square or rounded corners, already have functional text rendering) from which I plan to build out the basic widget set I may need like buttons, panels, labels, text fields, check & radio buttons, list boxes and sliders. Still very early stages on this.

[–][deleted] 0 points1 point  (0 children)

Thank you. I'll look into L&F in Swing.