Java 三叉戟:试图淡化一个JLabel,得到错误
[编辑:现在,我已经用swing Timer手动推出了我自己的解决方案,它似乎可以满足我的需要。我仍然关心性能,如果有人能为下面的问题提供解决方案,我愿意放弃我的解决方案,转而支持Trident。在网上四处看看,我想知道Trident是否仍然“活着”作为一个项目?注意,我也尝试了TimingFramework,但没有看到与我自己的代码相比的优势。] 我是后端Java编程的老手,但对GUI方面缺乏经验,而且对Trident库完全陌生。我们的要求是拥有大量独立淡入淡出的标签,因此我希望使用库来实现这一点,而不是试图手工编写所有代码 下面的测试代码再现了这个问题。TTGui运行并调用一个方法来添加一个标签,上面写着“HELLO WORLD”。该方法要求Trident为标签的alpha设置动画。TTLabel子类JLabel来实现这一点(以及其他原因) 主要类别:Java 三叉戟:试图淡化一个JLabel,得到错误,java,animation,user-interface,awt,trident,Java,Animation,User Interface,Awt,Trident,[编辑:现在,我已经用swing Timer手动推出了我自己的解决方案,它似乎可以满足我的需要。我仍然关心性能,如果有人能为下面的问题提供解决方案,我愿意放弃我的解决方案,转而支持Trident。在网上四处看看,我想知道Trident是否仍然“活着”作为一个项目?注意,我也尝试了TimingFramework,但没有看到与我自己的代码相比的优势。] 我是后端Java编程的老手,但对GUI方面缺乏经验,而且对Trident库完全陌生。我们的要求是拥有大量独立淡入淡出的标签,因此我希望使用库来实现这
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上的第一篇文章,请告诉我是否有办法改进这个问题。我最终使用swing定时器手动推出了一个解决方案,目前为止效果良好。幸运的是,我的要求非常有限。我是在回答这个问题,而不是删除它,以防对其他人有所帮助。super.paintComponent(g);清除所有以前的绘画啊,谢谢——我应该在方法的开头而不是结尾调用它吗?我的猜测是我应该在画画之前设置阿尔法,但就像我说的,我在这方面是新手。。。