我该如何让 JFrame 的绘画自动更新?
目前,我有一个包含 JPanel
的 JFrame
。 JPanel
是我的程序的绘画。 Paint 意味着我所有的 Graphics g
.drawString 东西。 现在,它仅在用户与 JFrame 交互时更新显示,但我希望它能够不断更新 (repaint()
) 本身,而不使用 while 循环(也CPU 使用率高)。
有人知道我该怎么做吗?
Currently, I have a JFrame
that contains a JPanel
. The JPanel
is the paint for my program. Paint meaning all of my Graphics g
.drawString things.
Right now, it only updates the display whenever the user interacts with the JFrame
, but I want it to continuously update (repaint()
) itself WITHOUT using a while loop (too much CPU usage).
Anyone know how I could do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要做的是,只要您知道面板上的图像部分发生了变化,就通知 swing 组件。正如您所说,执行此操作的正常方法是调用
repaint()
并且如果您知道“无效”的矩形,您也可以指出这一点。根据导致
JPanel
内容的事件,您需要更改应用程序设计,以便应用程序的表示部分“侦听”底层数据的变化,并在这些变化发生时重新绘制。还有另一种名为
paintImmediately()
的方法,它可能适用于某些情况,但您必须更多地描述您的场景以及在哪些情况下需要连续重新绘制。What you need to do is to inform the swing component whenever you know that part of the image on your panel changed. The normal way to do this is to, as you said, call
repaint()
and if you know the rectangle that was 'invalidated' you can also indicated that.Depending on the events that cause the contents of the
JPanel
, you need to change your application design so that the presentation part of your application 'listens' to changes in the data underneath and repaints when these changes occur.There is another method called
paintImmediately()
which might do for certain situations, but you have to describe a little more your scenario and in which cases are you needing to repaint continuously.您正在尝试从服务器“拉”更新,然后您可以使用 SwingWorker 查询服务器以获取更新并然后将更新发布到 GUI。您可能希望 SwingWorker 进入睡眠状态,这样它就不会连续轮询服务器。
阅读 Swing 教程中有关 工作线程和 SwingWorker 的部分了解更多信息。
也许更好的设计是服务器应该将更新“推送”到客户端,而客户端应该侦听更改,然后重新绘制自身。也许所有关于套接字部分会对您有所帮助。
编辑:
这是一些我已经 5 年多没有看过的旧代码。 “客户端”是一个简单的文本窗格,它将输入的每个字符发送到“服务器”。然后,服务器将每个字符发送到连接到该服务器的任何其他客户端。这个想法是服务器拥有最新的文档。当一个客户端发送更改时,所有其他客户端都会收到该更改的通知。这样所有客户端始终包含相同的数据。您的代码应该更简单,因为您只会向服务器发送完整的消息。要运行代码,请打开 dos 窗口并输入:
这将启动一个侦听端口 1234 的服务器
然后打开另一个窗口并键入
这将创建 3 个连接到服务器的客户端框架。输入任一帧都会更新服务器。
您可以在此处获取 zip 文件:
https://www.camick.com/java/来源/echo.zip
It sounds like you are trying to "pull" updates from the server, then you can use a SwingWorker to query the server for updates and then pulish the updates to the GUI. You would want the SwingWorker to sleep so it doesn't continuously poll the server.
Read the section from the Swing tutorial on Worker Threads and SwingWorker for more information.
Maybe a better design is for the server should "push" updates to the client and the client should listen for changes and then repaint itself. Maybe the All About Sockets section will help you out.
Edit:
Here is some old code I havent looked at in over 5 years. The "client" is a simple text pane which sends each character typed to the "server". The server then sends each character to any other client that is connected to the server. The idea it that the server has the most up to date Document. As one client sends a change all other clients are notified of the change. This way all clients always contain the same data. Your code should be simpler since you will only send complete messages to the server. To run the code open a dos window and type:
This will start a server that listens to port 1234
Then open another window and type
This will create 3 client frames that connect to the server. Typing in either of the frames will update the server.
You can get the zip file here:
https://www.camick.com/java/source/echo.zip