Skip to content
Advertisement

Why does paintComponent get called only on some JPanels?

I’m trying to add a background to different JPanels (henceforth they will be called Window). These Windows are classes I created and make them inherit JPanel. Then depending on the state of the program one Window is set as the content panel of the program’s JFrame. The problem comes when in some Windows the background gets set and in others not. The background setting is performed ussing the paintComponent(Graphics g) method, but despite I’ve tried to fix the bug, I didnt’ success.

Here is the code I think might be useful for those who wanna help:

Main Loop:

JavaScript

Screen class:

JavaScript

Window abstract class:

JavaScript

A Window where the background is correctly set:

JavaScript

A Window where the background isn’t set:

JavaScript

Button is a class of my own.

If anyone wants to test it for his own or see more code here is the github repository.

I tried everything and more. I’ve noticed that the Graphics of MenuWindow aren’t initialized, so the problem could come because that window isn’t rendered. I don’t know.

Advertisement

Answer

This…

JavaScript

is a bad idea. Apart from the fact that a “wild loop” is just generally a bad idea on its own, Swing is also not thread safe and is single threaded.

This means that if this is running within the context of the Event Dispatching Thread, it will block it and prevent from ever been able to process any new events. If it’s not running in the EDT, you’re risking causing any number of graphic glitches or other “dirty thread” issues.

Add into the fact that a “wild loop” like this will also consume the CPU cycles, you’re adding a tremendous performance overhead for little to no gain.

Start by taking a look at Concurrency in Swing

Screen.getInstance().requestFocus(state); is also a hack around the, known, limitations of KeyListener which are better solved through the use the key bindings API

Taking all that into account, Kavaliro should look something more like…

JavaScript

This is also not a good way to make a singleton in Java.

JavaScript

You could take a look at Java Singleton Design Pattern Best Practices with Examples and What is an efficient way to implement a singleton pattern in Java? OR you could just make use of Dependency Injection (and probably some research into dependency injection vs singleton)


Now, as I said, KeyListener is a generally a poor choice of monitoring user keyboard input, this means that TitleWindow should change, to something a little more like…

JavaScript

Now, to make life easier, I wrote a quick “utility” class…

JavaScript

This takes care of a lot of the boiler plate/repeating code, but also provides a means to “limit” the possible inputs to the API. Note the Input enum allows for ENTER and TITLE_TOGGLE as viable options for the keyStrokeFor methods.

Note that TITLE_TOGGLE “hides” the key stroke which is used, but it is pretty self documenting at the same time. There are lots of other ways you could build out these concepts, this is just an example.

And…

JavaScript

would get a very large, no, from me. There is already a button component within the API and you should use it. It even supports things like roll over, for example and example, as well as a verity of other functionalities, which you’re going to be spending a lot of time re-building.

See How to Use Buttons, Check Boxes, and Radio Buttons. And, yes, you can remove the Look And Feel fill background and borders if you really want to.


I don’t know about you, and I don’t use Intellij, but return ImageIO.read(new File(GFX_PATH + name)); broke for me.

Resources like these should really be embedded within the application’s runtime context (tech babble for “included in the Jar”). This will prevent issues with trying to locate the resources at runtime when the “working directory” context is not the same as the location of the res folder.

You should be using…

JavaScript

but how you configure Intellji to include the context of the res folder into your application context is beyond me.


JavaScript

is, well, short sighted. Instead you should be using a CardLayout which will do this for you, and in away which generally will work reliably.

See How to Use CardLayout for more details

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement