Efficiency in custom Map Editor - Swing or Graphics drawing?

Beginning a project on a game, and I decided I wanted to use my own in-house tools for development. And with that comes a Map Editor.

Since I will most likely be doing a lot of drawing with Graphics when I start to make the game itself, I thought this question was important to ask before I actually get to that part.

Currently, I am using Swing to make my Map Editor look pretty. I’ve got a JMenuBar, JToolBar, JSplitPanes, JScrollPanes, you name it. I’m actually pretty familiar with Swing and how to make it look nice.

So you can visualize it easier, here it is.

What you’re looking at is:

  • A JTabbedPane within my main Application’s JFrame, which has in it,
  • a JScrollPane, in order to allow proper viewing based on the zoom level of the
  • Custom Canvas JPanel set as the viewport, which has a specific LayoutManager, in order for the
  • Regular JPanel within the custom Canvas to be centered correctly, which contains
  • A GridBagLayout of JLabels.

Each JLabel has an icon based on whatever tile it should be. “Empty tiles” still have icons, it’s just that solid darker gray color, created by an ImageFactory class which stores all my image manipulation methods.


Okay, so if you’re still with me, I’d like to actually get around to asking my question.

My question: is using Swing components like this the most efficient way of doing this?

The reason I ask is, I’ve had to do a lot of setting and re-setting of each of those JLabels’ icon images. I’ve got one Class called ImageFactory that manipulates the images. The icons get changes based on Mouse input (clicking, enter, exit) or some other criteria, as such:

  • If no tile is selected to be ‘stamped,’ there will just be an alpha composite of a color painted over the JLabel’s icon to denote that it is being hovered, and will revert upon a MouseExit
  • If there is a tile on the active Stamp tool, that tile’s image will replace the JLabel’s icon -temporarily- to denote that it is being hovered, and will revert upon a MouseExit
  • If there is a tile on the active Stamp tool, and a JLabel is clicked, that JLabel’s icon will be set to the active Stamp tile “permanently” unless reverted
  • If the option to have Grid Lines is toggled on, each JLabel’s icon will be processed through the ImageFactory to have dashed lines running along the top and left edges (forms a grid, as seen in the picture)
  • If the Zoom level is changed, each JLabel’s preferredSize is set to accomodate, and the icon has to be resized in the ImageFactory (grid lines applied depending)

As you can see, this seems to be a lot of Icon changing/setting, and I don’t actually use any Graphics drawing (like I will within the game), except within the ImageFactory class (in order to manipulate the icons).

Is this the fastest or most efficient way to handle these things to achieve the desired outcome?

Would it be faster if I manually overrode the subclassed JPanel’s paintComponent method, and drew the stuff myself?

I know this still may seem trivial since I’ve got the thing working how I want. I’m just worried about making it as memory-efficient and CPU-efficient as possible. Even though the setting and re-setting of each JLabel’s icon happens pretty quickly, I need to be in this mindset because creating a game that is wasteful of resources is just bad practice, and I’ve never really had to make anything where I had to be nit-picky about these things.

You could use a table of BufferedImage in one JPanel (instead of the jLabels), and compare speeds, or you could just create one large BufferedImage in your JPanel and just draw onto it in memory.

[quote]My question: is using Swing components like this the most efficient way of doing this?
[/quote]
“Don’t fix it if it ain’t broken.” :point:
This goes especially for ‘temporary’ tools such as a game-specific map editor.
Yes, the performance is probably not as good as J2D, but then again, J2D isn’t really that much of a step up performance-wise.

Use the “correct,” “performant,” way (J2D) for the actual game, and use what works (you already have it) for the tool.

Don’t worry about efficiency too much while it’s still in early development, especially since this is just a tool and not the actual game. Anyways, I don’t know too much about this, but I highly doubt overriding it would be more efficient than setting it.

@Simba Productions -
That’s completely off topic. He’s asking about efficiency.

Imho i thought drawing onto a buffered image and cut all the extra JPanels would save memory and make the program run faster, which (using Java2D) is a more efficient way of “doing this”. due to the fact that its a big change from what he is doing now it could be a little too much of a change but my answer answers the question, i believe, so it isn’t “completely” off topic.

Whole point of using a JPanel is for the Swing functionality. Using a BufferedImage instead is like writing your own GUI system.

ah Ok i see where you are coming from, you are right in your own sense, but i hope you see where i’m coming from too. Also using more concrete JLabels/JPanels or a table of bufferedimages would work almost the same way which is why i mentioned it. i didnt mean cut all the scrollpane and all.

“Don’t fix it if it ain’t broken.” :point:
This goes especially for ‘temporary’ tools such as a game-specific map editor.
Yes, the performance is probably not as good as J2D, but then again, J2D isn’t really that much of a step up performance-wise.

Use the “correct,” “performant,” way (J2D) for the actual game, and use what works (you already have it) for the tool.
[/quote]
I started with what I knew, and using LayoutManagers to get desired positioning was my initial motivation. It’s a fairly simple tool so far, runs very smoothly on my current machine (not sure about lower end, for example my desktop is from 2008 and it may run slower on that).

Regardless, I appreciate the reply.

This is what I figured when I first started to worry about it, but I just wanted to make sure. One of the machines I will be developing on is 5 years old, so if I take my 2-year old Laptop’s processing speed for granted, I’ll be limiting myself later when I need to use Old Faithful!

What do you think about managing Lists of Arrays, or bunches of Maps? I’ve tried to be ‘considerate’ and set things to null when not being used, clearing out old Lists/Maps, and using smaller data types when possible (such as short instead of int, float instead of double). Is that being too cautious for a tool like this? I feel like the practice is needed anyways!

The way I look at it:

Your map editor is essentially an augmented version of your game. Your map editor should use the same classes and augment them where required. I.e your game map would be used to represent your map in the editor.

So you shouldn’t be rendering your game map any differently than how you’re rendering it in your editor. Otherwise you’re reimplementing the wheel everywhere you go and updating your game map becomes hell.

Because most games typically have a minimal UI - it makes sense for there to exist another window to perform a lot of the interaction with the world that require a UI. Hmm, I’ll post a screen shot of my editor to better illustrate the lay out.

Basically 95% of the editor is the game, and there is a small addition to get access to controls you wouldn’t normally have via game (but are implemented via the game) that provide an interface to this. I.e, the map editor doesn’t implement anything - it just allows for a more powerful interface:

This is bad for me. I haven’t begun making the game engine at all yet.

My idea was to have maps prepared when I being writing the engine, and have it parse the output from the editor. Since it’s just simple 2D tiles for now, I didn’t see that as being much of a problem.

The problem arises on my older machine when I’ve got more than one map open, and when I’ve played around with zooming and toggling the grid visibility.

I’m trying to optimize the way the Swing components and the BufferedImage chunks are referenced between each other. At the moment, I’ve got maybe 6 or 7 ArrayLists and HashMaps that make referencing certain Tiles at certain locations at certain indexes, which makes it VERY easy, but I feel like I need to devise a better way to do it. There is very noticeable input lag when interacting with the Tiles on my older machine, but not my newer one (which I believe to be caused either by the way I reference each Image and JLabel, or by the way I’ve manipulated their ImageIcons on Mouse Events). Either way, I’d like to make my program more efficient.

Hmm, well it should’nt be that big of an issue for a tile-based game - just keep in mind that later (when you get the engine all setup etc) and the map becomes more complex, it would be easier for you to switch over (eventually) to using your Game as a library for your map editor rather than working from scratch.

If you haven’t worked out the engine yet - don’t worry about it.

Eventually when you get the engine going your game’s world will implement all sorts of optimizations that you can start using from within your editor that should make your life easier when you get to that stage.

EDIT
That said, the answer to your question is both. Use swing for the UI and graphics drawing for your map.

You can also use LibGDX for UIs. Then you don’t need to worry about performance as much.

Keep in mind Spine was built with LibGDX and Scene2D:
http://esotericsoftware.com/