Re: Improving SwingRepaintTimeline

  • From: Kirill Grouchnikov <kirillcool@yahoo.com>
  • To: dev@trident.kenai.com
  • Subject: Re: Improving SwingRepaintTimeline
  • Date: Fri, 4 Jun 2010 13:47:08 -0700 (PDT)
  • Domainkey-signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=Message-ID:X-YMail-OSG:Received:X-Mailer:References:Date:From:Subject:To:In-Reply-To:MIME-Version:Content-Type; b=OXuECWIEdFbJzTEZF4SY7aI/DbXNcvxoxNSKdy/RzVK4q3MycwuPWNNZZsr4MrY4mv7HvOiJqvMOPwwXGl1QOvevxdF0HoQTIeuQ8qLkNAAsXITuJrpeq20LBFgDPZta4Xdh906QXtG1FKYVYFriaXZ8HOuZwIh5w3HmX18wqbY=;

Hi Rob

Thanks for suggesting this. Is it my understanding that you are willing to contribute this code under BSD license?

Kirill


From: Rob Eden <rob@starlight-systems.com>
To: dev@trident.kenai.com
Sent: Thu, June 3, 2010 11:28:39 AM
Subject: Improving SwingRepaintTimeline

Hi Krill -

First off, I'm just getting into Trident and it's a really nice library. Thanks for putting it together!

I've been playing with a setup similar to the one used in the Snake demo app in which there's a master repaint timeline. The thing I'm not crazy about is that it constantly does repaints whether it needs to or not.

So, I created a class to manage only repainting when necessary and I figured I'd see if you'd be interested in it or something similar.

-----------------------------------------------------------

public class CheckedSwingRepaintTimeline extends Timeline {
public CheckedSwingRepaintTimeline(Component mainTimelineComp,
AtomicBoolean needRepaintFlag) {

this(mainTimelineComp, null, needRepaintFlag);
}

public CheckedSwingRepaintTimeline(Component mainTimelineComp, Rectangle toRepaint,
AtomicBoolean needRepaintFlag) {

super(mainTimelineComp);
this.addCallback(new CheckedSwingRepaintCallback(mainTimelineComp, toRepaint,
needRepaintFlag));
}

// The rest is unchanged from SwingRepaintTimeline...
}

public class CheckedSwingRepaintCallback extends TimelineCallbackAdapter {
private Component comp;

private Rectangle rect;

private final AtomicBoolean needRepaintFlag;

public CheckedSwingRepaintCallback(Component comp, AtomicBoolean needRepaintFlag) {
this(comp, null, needRepaintFlag);
}

public CheckedSwingRepaintCallback(Component comp, Rectangle rect,
AtomicBoolean needRepaintFlag) {
if (comp == null) {
throw new NullPointerException("Component must be non-null");
}
this.comp = comp;
this.rect = rect;
this.needRepaintFlag = needRepaintFlag;
}

@Override
public void onTimelinePulse(float durationFraction, float timelinePosition) {
if ( !needRepaintFlag.compareAndSet( true, false ) ) return;

if (this.rect == null)
this.comp.repaint();
else
this.comp.repaint(this.rect.x, this.rect.y, this.rect.width,
this.rect.height);
}

@Override
public void onTimelineStateChanged( Timeline.TimelineState oldState,
Timeline.TimelineState newState, float durationFraction,
float timelinePosition) {

onTimelinePulse( durationFraction, timelinePosition );
}
}

-----------------------------------------------------------

Snake is then patched as follows:

-----------------------------------------------------------

@@ -3,6 +3,7 @@
 import java.awt.*;
 import java.awt.event.MouseEvent;
 import java.awt.event.MouseMotionAdapter;
+import java.util.concurrent.atomic.AtomicBoolean;
 
 import javax.swing.*;
 
@@ -11,6 +12,8 @@
 import org.pushingpixels.trident.swing.SwingRepaintTimeline;
 
 public class Snake {
+ private static final AtomicBoolean needRepaintFlag = new AtomicBoolean(false);
+
  public static class SnakePanelRectangle {
  private Color backgroundColor;
 
@@ -39,6 +42,7 @@
 
  public void setBackgroundColor(Color backgroundColor) {
  this.backgroundColor = backgroundColor;
+ needRepaintFlag.set(true);
  }
 
  public Color getBackgroundColor() {
@@ -66,7 +70,7 @@
  this.setPreferredSize(new Dimension(COLUMNS * (DIM + 1), ROWS
  * (DIM + 1)));
 
- Timeline repaint = new SwingRepaintTimeline(this);
+ Timeline repaint = new CheckedSwingRepaintTimeline(this, needRepaintFlag);
  repaint.playLoop(RepeatBehavior.LOOP);
 
  this.addMouseMotionListener(new MouseMotionAdapter() {

-----------------------------------------------------------

Thanks!

Rob



Improving SwingRepaintTimeline

Rob Eden 06/03/2010

Re: Improving SwingRepaintTimeline

Kirill Grouchnikov 06/04/2010

Re: Improving SwingRepaintTimeline

Rob Eden 06/04/2010

Re: Improving SwingRepaintTimeline

Kirill Grouchnikov 06/04/2010

Re: Improving SwingRepaintTimeline

Rob Eden 06/04/2010

Re: Improving SwingRepaintTimeline

Kirill Grouchnikov 06/04/2010

Re: Improving SwingRepaintTimeline

Rob Eden 06/04/2010

Re: Improving SwingRepaintTimeline

Kirill Grouchnikov 06/04/2010

Re: Improving SwingRepaintTimeline

Andres Almiray 06/04/2010

Re: Improving SwingRepaintTimeline

Kirill Grouchnikov 06/04/2010

Exposing Timeline's mainObject

Andres Almiray 06/05/2010

Re: Exposing Timeline's mainObject

Kirill Grouchnikov 06/06/2010
  • Mysql
  • Glassfish
  • Jruby
  • Rails
  • Nblogo
Terms of Use; Privacy Policy;
© 2010, Oracle Corporation and/or its affiliates
(revision 20120518.3c65429)
 
 
Close
loading
Please Confirm
Close