title: JavaFX canvas - draw only when the canvas is visible
body: <p>I'm considering porting our internal automotive/measuring application from Swing to JavaFX, mainly because of the better look and multitouch support, but I can't find a way to render custom components only when they are visible.</p>
  
  <p>For motivation, imagine a screen with 10 tabs, and inside each tab is a plot displaying some live data being measured. At any time, only one plot can be seen. The amount of data is large, the computer has enough power to render one plot at a time, but not all of them simultaneously.</p>
  
  <p><strong>Swing version</strong></p>
  
  <p>Now in the Swing version, the implementation of this behavior was straightforward. Each plot is a custom <em>JComponent</em> with overriden <em>paintComponent</em> method that does all the plot drawing. <em>paintComponent</em> gets called in two important cases:</p>
  
  <ul>
  <li>When the component becomes visible, e.g. user selects the tab with the plot</li>
  <li>When the live data change, my code calls a <em>repaint()</em> on the component, that in turn results in <em>paintComponent</em> being called <strong>if</strong> that component is visible (or <strong>when</strong> it becomes visible again).</li>
  </ul>
  
  <p>It is more complicated than this (see <a href="http://www.oracle.com/technetwork/java/painting-140037.html" rel="nofollow">http://www.oracle.com/technetwork/java/painting-140037.html</a> for details), but it works roughly like this. The important thing is that only the plots that are visible are also being processed and redrawn..</p>
  
  <p><strong>Porting to JavaFX?</strong></p>
  
  <p>Now I'm trying to port this behaviour to JavaFX, the natural choice being <em>Canvas</em>. But the <em>paintComponent</em> callback mechanism is gone, you just draw directly into the <em>Canvas.getGraphicsContext2D</em>. </p>
  
  <p>If I understand correctly, calling draw operations on this context doesn't really perform any immediate drawing, it just pushes the operations on a stack that gets processed later by the Prism subsystem when the Prism thread runs (is this correct?). So, probably (I'm just guessing, I'm pretty new to JavaFX), there would be no actual painting at all if Prism discovers that the Canvas is not visible (another tab is selected, it is obscured by another Node, lies outside the visible portion of Screen, ...), is this correct? That would be nice.</p>
  
  <p>But even if this is this case, it doesn't help me, because I don't want to even <em>start</em> the rendering until I know that the Canvas is visible. Even preprocessing the live data for plotting can take quite a lot of CPU power, perhaps even more than the rendering itself - imagine interpolating thousands of data points to plot just a few lines. So, after the live data changes, I'd like to first <strong>test</strong> the actual visibility of each plot (<em>Canvas</em>) before starting its redraw. I also need to be notified when it <strong>becomes</strong> visible. Is this somehow possible with JavaFX? Or am I taking a bad route altogether with Canvas and there is a better way?</p>
  
  <p>P.S. Please, don't confuse this visibility with <em>Node.visibleProperty</em>, that one does something different, basically switches off the Node from rendering.</p>