protected void paintComponent(Graphics g)

2019-08-15 00:47发布

问题:

We can override the paintComponent(Graphics g) method of JComponent.

It is called automatically to refresh the screen.

What can I do that is called regularly, for example every 100 ms.

Is this possible?

回答1:

Maybe your purpose is refreshing the component every certain ms?

If so, there's a few options available.
The use of Timer is a first option. Example here.
The option most people use is Thread animation. Here is an example.
Also there's a timing framework available to download. I can't find an available link, but just search for it on the internet.

Hope this helps.



回答2:

You can request that the component repaint itself with a call to repaint(). You most certainly do not want to call repaint every 1 ms though; no monitor in the world could display at 1000 Hz, and no human eye would distinguish that either. Furthermore the repaint calls would be coalesced into a few separate ones and you would not achieve that number of repaints.

What are you trying to achieve? I assume it's some sort of animation; if so you should start googling for "java 2d swing animation" or some variant of that. Try this 2d java tutorial for starters.