I’m using drawLine() and friends to paint a graph onto a JPanel. There are tens of thousands of points to graph, so it takes 3-5 seconds. I want to have a viewport, like the blue one illustrated below, over the graph.
graph http://cl.ly/1qvN/content
I would like this viewport to be updatable, as I have a MouseMotionListener on the JPanel that the graph is on. The problem is, if I redraw the viewport (the square) as it is now, (using drawRect()), I have to redraw the entire graph, which isn’t desirable.
I’ve read some about GlassPanes, which may be useful for solving this problem, but they evidently are a part of JFrames, and not JPanels. (I have this panel along with other components inside of a JFrame already.)
What would be a nice way to handle this, staying in Swing and being efficient?
For what it’s worth, I’m actually coding in Clojure, but that shouldn’t change anything here.
Thank you very much!
Isaac
My approach to this sort of problem was to consider the volatility of the data. Unless the chart is ticking along in real time, the only thing that will change from
painttopaintis the position of the blue box. One way to minimize the paint time is to not repeat work you have already performed. For example:The very first call to
paintComponentinitializes the buffer and does the heavy lifting of painting the graph. Thereafter, the buffer is just painted. If you need to handle the case where the display is redrawn to show a different part of the data, you just need to invalidate the buffer.