0

[EDIT: For now, I've ended up hand-rolling my own solution with swing timers, which seems to be working OK for my needs. I'm still concerned about performance and would be willing to junk my solution in favour of Trident if anyone can suggest a solution to the below. Looking around online, I'm wondering whether Trident is still "alive" as a project? NB I also tried TimingFramework but didn't see advantages compared to my own code.]

I'm an old hand with back-end Java programming but inexperienced with the GUI side, and completely new to the Trident library. Our requirement is to have a large number of labels that fade in and out independently, so I'm keen to do this with a library rather than trying to hand-code everything.

The test code below reproduces the problem. TTGui runs and calls a method to add a label saying "HELLO WORLD". The method asks Trident to animate the alpha of the label. TTLabel subclasses JLabel to achieve this (and for other reasons).

Main class:

import java.awt.*;
import javax.swing.*;
import org.pushingpixels.trident.Timeline;

public class TTGui extends JFrame
{
    private Container pane;

    public static void main(String[] args){

    TTGui gui = new TTGui();
        gui.addText("HELLO WORLD", 100);

    while (true) {
        }
    }

public TTGui() {
        this.setExtendedState(Frame.MAXIMIZED_BOTH);  
        pane = getContentPane();
        pane.setBackground(Color.WHITE);
        pane.setLayout(null);
        setVisible(true);
    }

    public void addText(String text, int size){
        TTLabel label = new TTLabel(text, size, 50, 50);
        pane.add(label);        
        pane.repaint();
        Timeline rolloverTimeline = new Timeline(label);
        rolloverTimeline.addPropertyToInterpolate("alpha", 0.0, 1.0);           
        rolloverTimeline.setDuration(2500);
        rolloverTimeline.play();                
    }

}

Label subclass:

import java.awt.AlphaComposite;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.SwingConstants;

public class TTLabel extends javax.swing.JLabel {

private float alpha = 0;

    public TTLabel(String str, int size, int x, int y){
        super(str, SwingConstants.RIGHT);
        this.setFont(new Font(this.getName(), Font.PLAIN, size));
        this.setBounds(x, y, this.getPreferredSize().width, this.getPreferredSize().height);
    }

    public float getAlpha(){
        return alpha;
    }

    public void setAlpha(float alpha){
        this.alpha = alpha;
        repaint();
    }

    protected void paintComponent(Graphics g)  
    {  
        Graphics2D g2 = (Graphics2D) g;  
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));  
        super.paintComponent(g);  
    }

}

My problem right now is that I get an error repeated every time Trident dispatches an event:

Exception occurred in updating field 'alpha' of object tridentTest.TTLabel at timeline position 1.0
java.lang.RuntimeException: Unable to set the value of the field 'alpha'
    at org.pushingpixels.trident.TimelinePropertyBuilder$DefaultPropertySetter.set(TimelinePropertyBuilder.java:75)
    at org.pushingpixels.trident.TimelinePropertyBuilder$GenericFieldInfo.updateFieldValue(TimelinePropertyBuilder.java:368)
    at org.pushingpixels.trident.Timeline$Setter.onTimelineStateChanged(Timeline.java:143)
    at org.pushingpixels.trident.Timeline$Chain$1.run(Timeline.java:207)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.lang.IllegalArgumentException
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.pushingpixels.trident.TimelinePropertyBuilder$DefaultPropertySetter.set(TimelinePropertyBuilder.java:73)
    ... 17 more

I'm aware that the content of paintComponent may be incorrect but it compiles and in debug mode (in Eclipse) the method never seems to be called. So if you have advice on what I have there I'd be appreciative, but the main issue right now is the error.

This is my first post to StackOverflow so do let me know if there's a way I can improve this question.

4

1 回答 1

0

我最终使用摇摆计时器手动滚动了一个解决方案,到目前为止效果很好。幸运的是,我的要求非常有限。我正在回答这个问题,而不是删除它,以防它对其他人有帮助。

于 2014-05-17T10:14:44.550 回答