Skip to content
Advertisement

paintComponent() not being called

Here is a little program that should (in theory) draw an image of a ball on screen. The problem is that paintComponent seems to not get called. The program consists of two classes.

JavaScript
JavaScript

As you can see, I tested if paintComponent was called using a console message. Sadly this was not the case. Can someone explain?

Advertisement

Answer

It’s no wonder that paintComponent is not called, because Canvas has no implementation of paintComponent which you can override. With a canvas you have to overwrite paint for your purposes. In your code you use both a JPanel and a Canvas, which is not necessary at all. Use either of the two.

The following is an example with a Canvas:

JavaScript

The annotation Override above the method to be overwritten ensures that the compiler can issue a warning message if the overwritten method does not exist or there is a typo. I hope this helps you further.

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