简体   繁体   English

通过Java UDP网络发送JPanel

[英]Send JPanel through Java UDP Networking

I have a very basic video game written in Java where all the graphics are displayed in a JPanel . 我有一个用Java编写的非常基本的视频游戏,其中所有图形都显示在JPanel I wish to use UDP Networking to implement a multiplayer feature to this game. 我希望使用UDP网络为该游戏实现多人游戏功能。 To do, one of the basic things I need to be able to do is to send and receive a JPanel with the correct graphics between a client and server using UDP. 为此,我需要做的基本事情之一是使用UDP在客户端和服务器之间发送和接收具有正确图形的JPanel

I have looked through other questions and tutorials using Java UDP Networking to get an idea of how UDP works, but I am still not quite sure how I would implement a program that sends a JPanel with the proper graphics inside it from client to server and vice versa using UDP? 我已经使用Java UDP网络浏览了其他问题和教程,以了解UDP的工作原理,但是我仍然不确定如何实现一个程序,该程序将JPanel内部具有适当的图形从客户端发送到服务器,反之亦然反之亦然使用UDP吗?

Is there any advice that someone could give me? 有没有人可以给我的建议? Maybe there is a question incredibly similar to this that I just missed when researching? 也许有一个与我在研究时遗漏的问题非常相似的问题?

Thank you! 谢谢!

To do, one of the basic things I need to be able to do is to send and receive a JPanel with the correct graphics between a client and server using UDP. 为此,我需要做的基本事情之一是使用UDP在客户端和服务器之间发送和接收具有正确图形的JPanel。

This is exactly what I recommend that you don't do. 我正是建议您不要这样做。 Instead, I strongly urge you to send model state, not view display, over your network, and then allow each site to use the model state to construct its own view. 相反,我强烈建议您通过网络发送模型状态,而不是视图显示,然后允许每个站点使用模型状态来构造自己的视图。 Doing it this way will be much more efficient as you'd be sending a much smaller dataset online. 这样一来,这样做的效率将大大提高,因为您将在线发送一个更小的数据集。 But note that if you do this, then you might need to use a more reliable transmission protocol than UDP such as TCP (although not necessarily). 但是请注意,如果执行此操作,则可能需要使用比UDP更可靠的传输协议,例如TCP(尽管不一定)。

Whether you send the data through UDP or TCP will depend on which is more critical for your game: network speed or absolute data integrity. 是否通过UDP或TCP发送数据取决于哪个对您的游戏更关键:网络速度或绝对数据完整性。 The network communication is usually the tightest bottleneck. 网络通信通常是最严格的瓶颈。 Your desire to send a serialized JPanel means that you'd be sending thousands of data bits over the network where only a few bits are needed. 您希望发送序列化的JPanel意味着您将在仅需要几位的网络上发送数千个数据位。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM