#1881 Animation and canvas

fraya Thu 19 Apr 2012

Hi, I´m doing a small simulator of Grashof´s law in fantom. The animation is flickering and I wonder if I´m doing it right or I need another technique because is my first fantom program. My code is something like this:

In the Canvas:

override Void onPaint(Graphics g)
{
  g.push
  try
  {
    cleanScreen(g)
    paintGrid(g)
    paintSimulation(g)
  }
  finally
  {
    g.pop
  }
}

// called from button to step to step simulation
Void repaint()
{
  // Calculate rectangle with "what" changes
  Rectangle r ...
  repaint(r)
}

Any advice?

Thanks

brian Thu 19 Apr 2012

I assume you are talking about SWT, not JavaScript?

You probably need to use double buffering. I think there is something built into SWT to do that automatically, but I haven't investigated it. I think some of the SWT wizards here can comment.

To do it manually you would do something like this (although I'm still noticing some flickering - maybe something SWT is doing to clear):

using gfx
using fwt

class Foo : Canvas
{
  static Void main() {
    // open Window
    Window {
      size = Size(400, 400)
      content = Foo()
    }.open
  }

  Image? buf

  override Void onPaint(Graphics gTop)
  {
    if (buf == null || buf.size != this.size)
    {
      echo("-- new buf!")
      buf = Image.makePainted(size) |g|
      {
        buf?.dispose
        w := size.w; h := size.h
        g.brush = Color.white
        g.fillRect(0, 0, w, h)

        g.brush = Color.green
        for (x := 5; x < w; x += 5) g.drawLine(x, 0, x, h)
        for (y := 5; y < h; y += 5) g.drawLine(0, y, w, y)

        g.brush = Color.red
        g.drawRect(5, 5, w-10, h-10)
      }
      gTop.drawImage(buf, 0, 0)
    }
  }
}

go4 Fri 20 Apr 2012

The SWT buildin support double buffer:

int style = SWT.NO_BACKGROUND | SWT.EMBEDDED;
if (((Canvas2)self).buffered()) style |= ***SWT.DOUBLE_BUFFERED***;
Canvas c = new Canvas((Composite)parent, style);

brian Fri 20 Apr 2012

Thanks @go4 - its just a simple style flag.

I added a new doubleBuffered field to Canvas that turns that out, and it does appear to reduce the flickering.

changeset

go4 Fri 20 Apr 2012

:-)

fraya Sat 21 Apr 2012

Thank you very much! It's perfect with the new doubleBuffered field.

Login or Signup to reply.