简体   繁体   English

如何绘制和重绘/更新JPanel的内容(而不是背景)组件?

[英]Ways to Draw and Redraw / update just the Content (not the background) components of JPanel?

I am creating a graph plotter that plots (draws) datapoints & lines based on periodically updating data points. 我正在创建一个图形绘图仪,根据周期性更新的数据点绘制(绘制)数据点和线。

I am using a JPanel inside a JFrame as the drawing canvas. 我在JFrame中使用JPanel作为绘图画布。

The JPanel contains the line axis & other info (scales etc) as background . JPanel包含线轴和其他信息(比例等)作为背景 It also contains the updating data points as the content . 它还包含更新数据点作为内容

I would like to update the content of the JFrame periodically, after the new data is fetched. 我想在获取新数据后定期更新JFrame的内容。 The data points fetching part is complete & works fine. 获取部分的数据点已完成且工作正常。

This is my code: http://pastebin.com/SAEjNT1R , http://pastebin.com/WvPTyEfR (The panel class). 这是我的代码: http//pastebin.com/SAEjNT1R,http : //pastebin.com/WvPTyEfR (面板类)。

Main: 主要:

Panel object class: 面板对象类:

This the the OraclePinger package (not really required, in case u wanna run) : At pastebin Wdmd3q1t (Connector class), MycAgyu3 (Target class) 这是OraclePinger包(不是真的需要,如果你想运行):在pastebin Wdmd3q1t(连接器类),MycAgyu3(目标类)
Originally by Oracle - modified by me : docs.oracle.com/javase/1.4.2/docs/guide/nio/example/Ping.java 最初由Oracle修改 - 由我修改:docs.oracle.com/javase/1.4.2/docs/guide/nio/example/Ping.java

I am just confused with the JPanel drawing part. 我只是对JPanel绘图部分感到困惑。 I would like to be able to draw the background once, then periodically refresh/clear & redraw the contents. 我希望能够绘制一次背景,然后定期刷新/清除和重绘内容。

---Say if it's not possible to separate the background & content while refreshing.. that we only can clear all Components in the panel. ---说如果刷新时无法分离背景和内容......我们只能清除面板中的所有组件。 Can I at least make a call to something like panel.clearAllComponents() ; 我至少可以打电话给panel.clearAllComponents() ; panel.draw(TheBackGround+Content) (repeatedly) from the Main panel.draw(TheBackGround + Content) (重复)来自Main

How can I solve this ? 我怎么解决这个问题?

Thanks 谢谢

I can't look at links due to work related firewall restrictions but regardless, since you're asking volunteers for free advice, you really should be posting pertinent code here so as to make it as easy as possible for us to help you. 由于工作相关的防火墙限制,我无法查看链接,但无论如何,因为您要求志愿者提供免费建议,您真的应该在这里发布相关代码,以便我们尽可能地帮助您。

That being said, draw the invariant parts of your graph on a BufferedImage that is shown in the JPanel's paintComponent method, and then draw the changing parts in the JPanel's paintComponent method, possibly by looping through an ArrayList of data points. 话虽如此,在JPanel的paintComponent方法中显示的BufferedImage上绘制图形的不变部分,然后在JPanel的paintComponent方法中绘制变化部分,可能是通过循环数据点的ArrayList。

If you are using JFreeChart , here's a related example that uses a javax.swing.Timer to pace the animation. 如果您正在使用JFreeChart ,这是一个使用javax.swing.Timer来调整动画的相关示例 Note that JFreeChart updates the entire plot on each increment by design. 请注意, JFreeChart按设计更新每个增量的整个图。

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

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