我正在为3D图形编写代码,我希望在用户按下JButton时更新场景图。我正在尝试使用Behavior类,但我找不到有关使用swing事件来唤醒行为的任何信息。我真的很感激任何帮助!谢谢!!
答案 0 :(得分:0)
您可以使用包含Runnables队列的特殊行为对象。然后,您可以将runnable发布到该行为并将其唤醒。您将不得不整理正确的同步,以便当队列中没有更多命令时行为才会进入休眠状态,但它应该可以工作。
使类成为单例,以便能够在BehaviorScheduler中运行Runnable,类似于SwingUtilities.invokeLater()方法。
public class ThreadTransferBehavior extends Behavior {
private final static int POST_ID = 9997;
private final WakeupOnBehaviorPost m_wakeupPost = new WakeupOnBehaviorPost(this, POST_ID);
private final Stack<Runnable> commands;
public synchronized void processStimulus(Enumeration i) {
while(!commands.isEmpty()) commands.pop().run();
wakeupOn(m_wakeupPost);
}
public synchronized void queueCommand(Runnable r) {
commands.push(r);
postId(POST_ID);
}
}