2

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.

The JPanel contains the line axis & other info (scales etc) as background. 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. The data points fetching part is complete & works fine.

This is my code: http://pastebin.com/SAEjNT1R , http://pastebin.com/WvPTyEfR (The panel class).

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)
Originally by Oracle - modified by me : docs.oracle.com/javase/1.4.2/docs/guide/nio/example/Ping.java

I am just confused with the JPanel drawing part. 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.draw(TheBackGround+Content) (repeatedly) from the Main

How can I solve this ?

Thanks

Axel
  • 13,939
  • 5
  • 50
  • 79

2 Answers2

4

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.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
4

If you are using JFreeChart, here's a related example that uses a javax.swing.Timer to pace the animation. Note that JFreeChart updates the entire plot on each increment by design.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045