我该如何让 JFrame 的绘画自动更新?

发布于 2024-12-03 09:56:50 字数 253 浏览 2 评论 0原文

目前,我有一个包含 JPanelJFrameJPanel 是我的程序的绘画。 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱她像谁 2024-12-10 09:56:50

您需要做的是,只要您知道面板上的图像部分发生了变化,就通知 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.

情释 2024-12-10 09:56:50

我已经实现了一个功能齐全的互联网中继聊天系统,其中包括绘制以查看您和其他人在说什么

您正在尝试从服务器“拉”更新,然后您可以使用 SwingWorker 查询服务器以获取更新并然后将更新发布到 GUI。您可能希望 SwingWorker 进入睡眠状态,这样它就不会连续轮询服务器。

阅读 Swing 教程中有关 工作线程和 SwingWorker 的部分了解更多信息。

也许更好的设计是服务器应该将更新“推送”到客户端,而客户端应该侦听更改,然后重新绘制自身。也许所有关于套接字部分会对您有所帮助。

编辑:

这是一些我已经 5 年多没有看过的旧代码。 “客户端”是一个简单的文本窗格,它将输入的每个字符发送到“服务器”。然后,服务器将每个字符发送到连接到该服务器的任何其他客户端。这个想法是服务器拥有最新的文档。当一个客户端发送更改时,所有其他客户端都会收到该更改的通知。这样所有客户端始终包含相同的数据。您的代码应该更简单,因为您只会向服务器发送完整的消息。要运行代码,请打开 dos 窗口并输入:

java文档服务器1234

这将启动一个侦听端口 1234 的服务器

然后打开另一个窗口并键入

java DocumentClient 1234 3

这将创建 3 个连接到服务器的客户端框架。输入任一帧都会更新服务器。

您可以在此处获取 zip 文件:

https://www.camick.com/java/来源/echo.zip

I have implemented a fully functional internet relay chat system which involves paint to see what you and other people are saying

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:

java DocumentServer 1234

This will start a server that listens to port 1234

Then open another window and type

java DocumentClient 1234 3

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文