【问题标题】:Sliding JPanel with Universal Tween Engine使用通用补间引擎滑动 JPanel
【发布时间】:2016-12-04 02:11:31
【问题描述】:

这几天我一直在尝试创建一个 JPanel,它从侧面飞进来。我找到了 Universal Tween Engine,也看了一些演示,但由于某种原因,我无法在我自己的代码中使用它。为了简单起见,让我们尝试在 JFrame 上将 JPanel(包含 JLabel 中的图像)从 (0,0) 移动到 (600,0)。这是我到目前为止所得到的,也是我用这个框架实际移动东西最接近的,它所做的一切就是让 JPanel 在第一个滴答声左右跳到它的目的地。它应该很简单,但我一定错过了一些东西......

SlideTest.java - 创建 UI,初始化 Thread + Tween

public class SlideTest {
    TweeningPane p;
    public TweenManager tweenManager;

    public static void main(String[] args) {
        new SlideTest();
    }

    public SlideTest() {
        try {
            setupGUI();
        } catch (IOException e) {
            e.printStackTrace();
        }
        tweenManager = new TweenManager();
        AnimationThread aniThread = new AnimationThread();
        aniThread.setManager(tweenManager);
        aniThread.start();
        Tween.to(p, 1, 10.0f).target(600).ease(Quad.OUT).start(tweenManager);
    }

    public void setupGUI() throws IOException {
        JFrame f = new JFrame();
        f.setSize(800, 600);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        p = new TweeningPane();
        JLabel l = new JLabel(new ImageIcon("E:/Pictures/Stream/aK6IX4V.png"));
        f.setLayout(null);
        p.add(l);
        p.setBounds(0, 0, 300, 300);
        f.add(p);
        f.setVisible(true);
    }
}

AnimationThread.java - 线程,应该让我的 TweenManager 尽可能/经常更新

public class AnimationThread extends Thread {
    TweenManager tm;

    public void setManager(TweenManager tweenmanager) {
        this.tm = tweenmanager;
    }

    public void run() {
        while (true) {
            //System.out.println("MyThread running");
            tm.update(MAX_PRIORITY);
            try {
                sleep(40);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

TweeningPane.java - 我的对象(JPanel),我想跨 JPanel 移动

public class TweeningPane extends JPanel implements TweenAccessor<JPanel> {

    private static final long serialVersionUID = 1L;

    @Override
    public int getValues(JPanel arg0, int arg1, float[] arg2) {

        return (int) arg0.getBounds().getX();
    }

    @Override
    public void setValues(JPanel arg0, int arg1, float[] arg2) {
        arg0.setBounds((int) arg2[0], 0, 300, 300);

    }

}

【问题讨论】:

    标签: animation frameworks jpanel tween universal


    【解决方案1】:

    我终于明白了。正如我所料,我只是以错误的方式使用框架。我不确定是否需要所有这些步骤,但这是我为了使其工作而经历的:(供将来参考)

    我必须将我的访问器注册到引擎:

    Tween.registerAccessor(TweeningPane.class, new TweeningPane());
    

    线程本身现在看起来像这样;我必须将经过的时间作为参数提供给经理的更新方法。

    public void run() {
        long ms1 = System.currentTimeMillis();
        while (true) {
            //System.out.println("MyThread running");
            tm.update((System.currentTimeMillis() - ms1) / 1000f);
            try {
                Thread.sleep(40);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多