Improving SwingRepaintTimeline
- From: Rob Eden <rob@starlight-systems.com>
- To: dev@trident.kenai.com
- Subject: Improving SwingRepaintTimeline
- Date: Thu, 3 Jun 2010 13:28:39 -0500
- Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:sender:date:x-google-sender-auth:message-id:subject :from:to:content-type; b=g87ZgddDj8BO4sPuWtd6q9NrZyjkBX+er8ISwCmuI2myK6Cy/Qldjc+Tw0HdFC4Zbl zCy7JHwAJBIS/G4cKuS9So1a23Phj1XNtUwiRxst5NxszQdU1tfXbUTp73u33D5G1UAf XD13kDlnkUoJ/XvjAcmgVXHAWh2VL74dNZPjk=
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 |
| Kirill Grouchnikov | 06/04/2010 | |
| Rob Eden | 06/04/2010 | |
| Kirill Grouchnikov | 06/04/2010 | |
| Rob Eden | 06/04/2010 | |
| Kirill Grouchnikov | 06/04/2010 | |
| Rob Eden | 06/04/2010 | |
| Kirill Grouchnikov | 06/04/2010 | |
| Andres Almiray | 06/04/2010 | |
| Kirill Grouchnikov | 06/04/2010 | |
| Andres Almiray | 06/05/2010 |





