0

I have some java code that I use for animation of zooming in 3 contexts: 1. alt-scroll, 2. a static 150ms animation (a loop with a 10ms sleep) upon button press, and 3. via a click-hold of a button done by a loop in a thread (a loop with a 100ms sleep which continues to zoom as long as the mouse button is held down).

Each mechanism calls the same method for a zoom increment. Inside that method, zooming is managed on each axis independently (i.e. zoom on x and zoom on y).

However, while mechanisms #1 & #2 work great, I'm having trouble with #3. Sometimes the repaints are being done mid-stream, meaning the repaint is sometimes triggered between each of the dimension updates, which causes the animation of the zoom to look jerky.

I've tried a number of variations of different ways to control repainting to varying levels of success.

  1. Tried setting -Dswing.volatileImageBufferEnabled=false on the command line - did not work at all
  2. Tried calling paintImmediately in various places - did not work at all
  3. Tried using a combination of RepaintManager.currentManager(c).markCompletelyClean(c) and RepaintManager.currentManager(c).markCompletelyDirty(c) - while this was able to prevent repainting, I couldn't figure out how to get it to paint at each zoom increment reliably.
  4. Tried using setVisible(boolean) - this worked to stop the jerky repaints between updates in each dimension, but caused the panel to "flash" on every repaint

Perhaps I just haven't figured out the proper order to call things in. Does anyone know how I can manage this? I suspect the reason why this is happening is because the updates are being generated from a thread whereas the other mechanisms are happening serially, but I know of no other way to detect a click-hold event on a button press using swing.

hepcat72
  • 890
  • 4
  • 22
  • 3
    Can you post an [MCVE](http://stackoverflow.com/help/mcve) that reproduces the problem? – copeg May 06 '15 at 20:03
  • *"the repaint is sometimes triggered between each of the dimension updates, which causes the animation of the zoom to look jerky"* This sounds like maybe your painting causes a side-effect. Having extra repaints triggered by the system should not change the animation. It should just draw the same frame twice or draw an interim frame. Make sure your animation doesn't rely on side-effects in `paint`/`paintComponent`. – Radiodef May 06 '15 at 20:08
  • Something is calling notifyObservers after each dimension update, though I don't really understand from the trace where it is coming from (looks like it's core code, but I'm somewhat new to java). Zooming a single dimension is handled by other buttons, but the buttons I'm working on zoom in both dimensions and it uses 2 calls to the same code in succession. Those notifyObserver calls lead to the repaint between dimension updates. So perhaps you're right and I'm relying on a side-effect of merged repaints in #1 & #2? Does that corroborate your insight @Radiodef? – hepcat72 May 06 '15 at 20:38
  • I'm not sure I have the expertise to produce an MCVE @copeg. I'm a perl guy who started helping with tickets on this java project and I've been brushing up some pretty dusty java skills from undergrad. – hepcat72 May 06 '15 at 20:40
  • I'm less familiar with the observer stuff but if your observers are getting notified outside your control then yes, the same thing would apply. It's just whether the system calls are causing your animation state to change. A more idiomatic way to do animation would be to use a `javax.swing.Timer` (such as I showed [here](http://stackoverflow.com/a/29837148/2891664) though the discussion over there is largely unrelated) and update the state more directly so you have tighter control. If I'm misunderstanding what you mean by 'jerky', then give a more specific description of the behavior I guess. – Radiodef May 06 '15 at 20:58
  • Actually, I just tried using java.util.Timer and it worked about the same as the thread implementation I tried. Still jerky (i.e. you end up seeing x zoom out of sync with y zoom - they should happen together, but they appear separate). If that's still unclear, I could post a dropbox link to a screen recording... – hepcat72 May 06 '15 at 21:04

0 Answers0