protected void paintComponent(Graphics g)

我的未来我决定 提交于 2019-12-11 07:07:13

问题


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.



来源:https://stackoverflow.com/questions/5187462/protected-void-paintcomponentgraphics-g

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!