【问题标题】:Trident: trying to fade a JLabel, getting errorTrident:尝试淡化 JLabel,出现错误
【发布时间】:2014-06-29 14:28:06
【问题描述】:

[编辑:目前,我已经用摇摆计时器手动推出了自己的解决方案,这似乎可以满足我的需求。我仍然担心性能,如果有人可以提出以下解决方案,我愿意放弃我的解决方案以支持 Trident。在网上环顾四周,我想知道 Trident 作为一个项目是否仍然“活着”?注意,我也尝试了 TimingFramework,但与我自己的代码相比没有看到优势。]

我是后端 Java 编程的老手,但对 GUI 方面缺乏经验,并且对 Trident 库完全陌生。我们的要求是拥有大量独立淡入淡出的标签,因此我热衷于使用库来实现这一点,而不是尝试手动编写所有代码。

下面的测试代码重现了这个问题。 TTGui 运行并调用一个方法来添加一个标有“HELLO WORLD”的标签。该方法要求 Trident 为标签的 alpha 设置动画。 TTLabel 子类化 JLabel 来实现这一点(以及其他原因)。

主类:

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();                
    }

}

标签子类:

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);  
    }

}

我现在的问题是每次 Trident 调度事件时都会重复出现错误:

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

我知道paintComponent 的内容可能不正确,但它可以编译并且在调试模式下(在Eclipse 中)该方法似乎从未被调用过。因此,如果您对我所拥有的内容有任何建议,我将不胜感激,但目前的主要问题是错误。

这是我在 StackOverflow 上的第一篇文章,如果有什么方法可以改善这个问题,请告诉我。

【问题讨论】:

  • 除了 super.paintComponent(g);清除所有以前的绘画
  • 啊,谢谢——我应该在方法的开头而不是结尾调用它吗?我的猜测是我应该在绘画之前设置 alpha,但就像我说的那样我是新手......

标签: java animation user-interface awt trident


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2018-04-28
    • 1970-01-01
    • 2019-07-25
    • 1970-01-01
    • 1970-01-01
    • 2021-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多