Java Timing Framework中的PropertySetter不适用于我自己的属性

时间:2011-12-04 13:15:37

标签: java swing animation

我有一个使用http://java.net/projects/timingframework/的Timing Framework的小班。这是代码:

class Kula extends JPanel {
private Animator an;
private int xp;
private int yp;
private Color kolor;

public Kula(int x, int y) {
    this.xp = x;
    this.yp = y;
    this.kolor = Color.RED;

    TimingTarget tt = PropertySetter.getTarget(this, "kolor", Color.RED, Color.BLUE);
    an = new Animator.Builder().setDuration(2, TimeUnit.SECONDS).addTarget(tt).build();
}

public Color getKolor() {
    return kolor;
}

public void setKolor(Color kolor) {
    this.kolor = kolor;
    repaint();
}

public Animator getAnimator() {
    return this.an;
}

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(kolor);
    g.fillOval(xp, yp, 20, 20);
}
}

正如您所看到的,它绘制了一个具有特定颜色和位置(xp和yp)的椭圆。 它还有一个动画师。在构造函数中,我创建了Animator,但在创建TimingTarget以使用线性Interpolator(默认)更改kolor变量之前。我有getter和setter但是当我开始我的动画师时,我得到一个例外:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: (Timing Framework #31) An unexpected exception occurred when reflectively invoking the method setKolor on swingtest.GUI$Kula[,0,0,798x533,alignmentX=0.0,alignmentY=0.0,border=,flags=0,maximumSize=,minimumSize=,preferredSize=].
    at org.jdesktop.core.animation.timing.PropertySetter$PropertySetterTimingTarget.valueAtTimingEvent(PropertySetter.java:298)
    at org.jdesktop.core.animation.timing.KeyFramesTimingTarget.timingEvent(KeyFramesTimingTarget.java:51)
    at org.jdesktop.core.animation.timing.Animator.notifyListenersAboutATimingSourceTick(Animator.java:934)
    at org.jdesktop.core.animation.timing.Animator.timingSourceTick(Animator.java:1124)
    at org.jdesktop.core.animation.timing.TimingSource$1.run(TimingSource.java:183)
    at org.jdesktop.swing.animation.timing.sources.SwingTimerTimingSource$1.actionPerformed(SwingTimerTimingSource.java:75)
    at javax.swing.Timer.fireActionPerformed(Timer.java:312)
    at javax.swing.Timer$DoPostEvent.run(Timer.java:244)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:705)
    at java.awt.EventQueue.access$000(EventQueue.java:101)
    at java.awt.EventQueue$3.run(EventQueue.java:666)
    at java.awt.EventQueue$3.run(EventQueue.java:664)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:675)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
Caused by: java.lang.IllegalAccessException: Class org.jdesktop.core.animation.timing.PropertySetter$PropertySetterTimingTarget can not access a member of class swingtest.GUI$Kula with modifiers "public"
    at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:95)
    at java.lang.reflect.AccessibleObject.slowCheckMemberAccess(AccessibleObject.java:261)
    at java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:253)
    at java.lang.reflect.Method.invoke(Method.java:594)
    at org.jdesktop.core.animation.timing.PropertySetter$PropertySetterTimingTarget.valueAtTimingEvent(PropertySetter.java:296)
    ... 21 more

当我改变这条线时,有趣的是:

  

TimingTarget tt = PropertySetter.getTarget(this,“kolor”,Color.RED,Color.BLUE);

  

TimingTarget tt = PropertySetter.getTarget(this,“background”,Color.RED,Color.BLUE);

一切都好。我的面板背景变化正确。

为什么我的PropertySetter不能与kolor变量一起使用?

P.S。在波兰语中“Kula”表示球体,“kolor”表示颜色

1 个答案:

答案 0 :(得分:1)

PropertySetter通过反射访问目标的属性。只有当类和属性都具有 public 范围时,才有可能。

  • 无法访问:“kolor”是包范围类Kula中的公共方法
  • accessible:“background”是公共类Component
  • 中的公共方法

解决方案是让您的课程公开。