Skip to main content

[timingframework] end behaviour

8 replies [Last post]
fester
Offline
Joined: 2006-11-06
Points: 0

Hi,

since there is no seperate forum for the timingframework, I hope to have my question answered here.

I want to animate the icon in a JLabel. For that I defined the following:

<br />
final int origGap = dndHeaderLabel.getIconTextGap();<br />
PropertySetter target = new PropertySetter( dndHeaderLabel, "iconTextGap", origGap, origGap + 30 ){<br />
public void end()<br />
{<br />
    super.end();<br />
    dndHeaderLabel.setIconTextGap( origGap );<br />
}<br />
};<br />
animator = new Animator( 2000, 10, Animator.RepeatBehavior.REVERSE, target );<br />
animator.setEndBehavior( Animator.EndBehavior.RESET );<br />
animator.start();<br />

I was hoping that the override of PropertySetter would not be needed, that I could just set the EndBehaviour to RESET and this would set the property to the original value when the animation is done (or stopped via stop()). Is this a bug?

Question 2:
Increasing the iconTextGap moves the text to the right, but I want to move the icon to the left. Any idea on how this can be done?

regards,

Wim

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
mattnathan
Offline
Joined: 2005-02-20
Points: 0

> Question 2:
> Increasing the iconTextGap moves the text to the
> right, but I want to move the icon to the left. Any
> idea on how this can be done?

Have you tried setting the horzontalAlignment property on the label to RIGHT, I'm not sure if this will give you the required result or not, but it will make the icon move instead of the text.

fester
Offline
Joined: 2006-11-06
Points: 0

Just tried it, but the icon does not move, still the text moves. Thanks for the suggestion anyway.

What I have is this JLabel:

[code]
+------+---------------------+
| | |
| icon | Drag and drop here |
| | |
+------+---------------------+
[/code]

I want the icon to do something when a drop can happen, so I thought of moving the icon (Which is an arrow pointing to text) repeatetly a bit to the left and back again.

Other suggestions are always welcome :)

regards,

Wim

Message was edited by: fester

mattnathan
Offline
Joined: 2005-02-20
Points: 0

The reason the text still moves is because the label is being layed out at its preferred size positioned at the top-left.

Given what your trying to to do, however, I would suggest creating a custom icon which has properties that can handle the movement. Something like this:
[code]
public class DropIcon implements Icon {
private float position = 0f;
private int offset = 30;
private Icon base;
public DropIcon(Icon base, int offset) {
this.icon = icon;
this.offset = offset;
}
public void paintIcon(Graphics g, JComponent c, int x, int y) {
base.paintIcon(g, c, x + (int) (position * offset), y);
}
public int getIconWidth() {
return base.getIconWidth() + offset;
}
public int getIconHeight() {
return base.getIconHeight();
}
public void setPosition(float position) {
this.position = position;
}
}
[/code](The code above hasnt been tested and may not compile)

Now when animating you can use:
[code]
Icon dropIcon = new DropIcon(new ImageIcon(imageURL), 30);
final JLabel label = new JLabel(dropIcon, "Drop Here");
Animator a = new Animator(1000, INFINATE, LOOP, new PropertySetter(icon, "position", 0f, 1f));
a.addTimingTarget(new TimingAdapter() {
public void timingEvent(float percentage) {
label.repaint();
}
});
a.start();
[/code](Again not sure if this will compile or not, was written from memory)

This should be the most effecient way to achieve this as changing the text-icon gap will result in validation which will re-layout the container. From experience I've found that it's generally not a good idea to force layout for this kind of animation.

Hope this helps :)

Matt

fester
Offline
Joined: 2006-11-06
Points: 0

Yes! Got it working!

There are indeed some minor compilation issues in your code, but the basic idea is good.

Thank you very much!

regards,

Wim

Romain GUY

Instead of increasing the gap value, decrease it. Replace origGap +
30 by origGap - 30 for instance.

The reset behavior works, if I recall correctly, only with repeated
animations but I might be wrong.

Also instead of overriding end() in PropertySetter, you should rather
add a TimingListener or a TimingTarget to your animator and implement
end() here.

On 23 nov. 06, at 12:22, jdnc-interest@javadesktop.org wrote:

> Hi,
>
> since there is no seperate forum for the timingframework, I hope to
> have my question answered here.
>
> I want to animate the icon in a JLabel. For that I defined the
> following:
>
> [code]
> final int origGap = dndHeaderLabel.getIconTextGap();
> PropertySetter target = new PropertySetter( dndHeaderLabel,
> "iconTextGap", origGap, origGap + 30 ){
> public void end()
> {
> super.end();
> dndHeaderLabel.setIconTextGap( origGap );
> }
> };
> animator = new Animator( 2000, 10, Animator.RepeatBehavior.REVERSE,
> target );
> animator.setEndBehavior( Animator.EndBehavior.RESET );
> animator.start();
> [/code]
>
> I was hoping that the override of PropertySetter would not be
> needed, that I could just set the EndBehaviour to RESET and this
> would set the property to the original value when the animation is
> done (or stopped via stop()). Is this a bug?
>
> Question 2:
> Increasing the iconTextGap moves the text to the right, but I want
> to move the icon to the left. Any idea on how this can be done?
>
> regards,
>
> Wim
> [Message sent by forum member 'fester' (fester)]
>
> http://forums.java.net/jive/thread.jspa?messageID=178230
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: jdnc-unsubscribe@jdnc.dev.java.net
> For additional commands, e-mail: jdnc-help@jdnc.dev.java.net
>

--
Romain GUY
http://jroller.com/page/gfx
http://www.progx.org

---------------------------------------------------------------------
To unsubscribe, e-mail: jdnc-unsubscribe@jdnc.dev.java.net
For additional commands, e-mail: jdnc-help@jdnc.dev.java.net

fester
Offline
Joined: 2006-11-06
Points: 0

Hi Romain,

1) decreasing the gap moves the text to the left instead of to the right, under the icon. The icon does not move. Maybe I should change the position of the label at the same rate as I move the text to the right to have the visual effect of a fixed text with a moving icon?

2) I could not find a TimingListener interface (using version 0.5) or an addTimingTarget() method on Animator. Are you referring to a different version?

regards,

Wim

grimlock81
Offline
Joined: 2005-02-04
Points: 0

> 1) decreasing the gap moves the text to the left
> instead of to the right, under the icon. The icon
> does not move. Maybe I should change the position of
> the label at the same rate as I move the text to the
> right to have the visual effect of a fixed text with
> a moving icon?
You may have to animate the icon by creating a new image for each update. Or perhaps overriding the paint behaviour in JLabel?
>
> 2) I could not find a TimingListener interface (using
> version 0.5) or an addTimingTarget() method on
> Animator. Are you referring to a different version?
TimingListener was removed in version 0.5. So overriding PropertySetter seems to be the right way.
>
> regards,
>
> Wim

chet
Offline
Joined: 2003-08-07
Points: 0

> TimingListener was removed in version 0.5. So overriding PropertySetter seems to be the right way.
>

Indeed - I nuked TimingListener because it overlapped too much with TimingTarget. It should be sufficient (and hopefully easier) to implement a TimingTarget and override end(), or extend TimingTargetAdapter (which has stubs for begin/end/repeat/timingEvent) and just override that single method.