Java Swing 动画非常难制作是怎么回事呢?Java Swing 动画相信大家都很熟悉,但是Java Swing 动画非常难制作是怎么回事呢,下面就让小编带大家一起了解吧。
  Java Swing 动画非常难制作,其实就是Sun公司没有制作这个功能,大家可能会很惊讶Java Swing 动画怎么会非常难制作呢?但事实就是这样,小编也感到非常惊讶。
  在后面,小编搬运了一位国外小哥的代码,刚好能解决这个问题。这就是关于Java Swing 动画非常难制作的事情了,大家有什么想法呢,欢迎在评论区告诉小编一起讨论哦!(逃


话会正题,JavaSwing可以使用tween-engine-api来制作组件动画,上代码。

public class TweenImplements implements TweenAccessor<Component> {
    public static final int X = 1;
    public static final int Y = 2;
    public static final int XY = 3;
    public static final int W = 4;
    public static final int H = 5;
    public static final int WH = 6;
    public static final int XYWH = 7;

    @Override

    public int getValues(Component target, int tweenType, float[] returnValues) {
        switch (tweenType) {
            case X:
                returnValues[0] = target.getX();
                return 1;
            case Y:
                returnValues[0] = target.getY();
                return 1;
            case XY:
                returnValues[0] = target.getX();
                returnValues[1] = target.getY();
                return 2;
            case W:
                returnValues[0] = target.getWidth();
                return 1;
            case H:
                returnValues[0] = target.getHeight();
                return 1;
            case WH:
                returnValues[0] = target.getWidth();
                returnValues[1] = target.getHeight();
                return 2;
            case XYWH:
                returnValues[0] = target.getX();
                returnValues[1] = target.getY();
                returnValues[2] = target.getWidth();
                returnValues[3] = target.getHeight();
                return 4;
            default: return -1;
        }
    }



    @Override
    public void setValues(Component target, int tweenType, float[] newValues) {
        switch (tweenType) {
            case X:
                target.setLocation(Math.round(newValues[0]), target.getY());
                break;
            case Y:
                target.setLocation(target.getX(), Math.round(newValues[0]));
                break;
            case XY:
                target.setLocation(Math.round(newValues[0]), Math.round(newValues[1]));
                break;
            case W:
                target.setSize(Math.round(newValues[0]), target.getHeight());
                target.validate();
                break;
            case H:
                target.setSize(target.getWidth(), Math.round(newValues[0]));
                target.validate();
                break;
            case WH:
                target.setSize(Math.round(newValues[0]), Math.round(newValues[1]));
                target.validate();
                break;
            case XYWH:
                target.setBounds(Math.round(newValues[0]), Math.round(newValues[1]), Math.round(newValues[2]), Math.round(newValues[3]));
                target.validate();
                break;
        }

    }

}
public class TweenSystem {
    TweenManager tweenManager=new TweenManager();
    ArrayList<TweenListener>tweenListeners=new ArrayList<>();
    TweenEquation tweenMode= Cubic.INOUT;
    Component component;
    Thread runThread;
    float time=10f;
    int mode=TweenImplements.XY;
    boolean running = true;
    float []arg;

    public void setArg(float... arg){
        this.arg=arg;
    }

    public void setTime(float time) {
        this.time = time;
    }

    public boolean isRunning() {
        return running;
    }

    public void start(){
        load();
        runThread.start();
    }
    public void setTweenMode(TweenEquation mode){
        this.tweenMode=mode;
    }
    public void setComponent(Component component){
        this.component=component;
    }
    public void setMode(int mode){
        this.mode=mode;
    }

    public void stop(){
        List<BaseTween<?>> baseTweens=tweenManager.getObjects();
        for (BaseTween baseTween:baseTweens){
            baseTween.free();
        }
        for (TweenListener tweenListener:tweenListeners){
            tweenListener.stop();
        }
    }

    public void pause(){
        List<BaseTween<?>> baseTweens=tweenManager.getObjects();
        for (BaseTween baseTween:baseTweens){
            baseTween.pause();
        }
    }

    public void addTweenListener(TweenListener... tweenListeners){
        this.tweenListeners.addAll(Arrays.asList(tweenListeners));
    }

    public void removeTweenListener(TweenListener... tweenListeners){
        this.tweenListeners.removeAll(Arrays.asList(tweenListeners));
    }


    protected void load (){
        Tween.registerAccessor(Component.class, new TweenImplements());
        Tween.to(component, mode, time).ease(Cubic.INOUT).target(arg ).start(tweenManager);
        Runnable runnable = new Runnable() {
            public void run() {
                long lastMillis = System.currentTimeMillis();
                long deltaLastMillis = System.currentTimeMillis();
                running=true;
                while (running) {
                    long newMillis = System.currentTimeMillis();
                    long sleep = 15 - (newMillis - lastMillis);
                    lastMillis = newMillis;
                    component.repaint();
                    if (sleep > 1){
                        try {
                            Thread.sleep(sleep);
                        }
                        catch (InterruptedException ex) {
                        }
                    }
                    long deltaNewMillis = System.currentTimeMillis();
                    final float delta = (deltaNewMillis - deltaLastMillis) / 1000f;
                    try {
                        SwingUtilities.invokeAndWait(new Runnable() {
                            public void run() {
                                tweenManager.update(delta);
                                List<BaseTween<?>> baseTweens=tweenManager.getObjects();
                                boolean canStop=true;
                                for (BaseTween baseTween:baseTweens){
                                    if (!baseTween.isFinished())canStop=false;
                                }
                                if (canStop)running=false;
                            }});
                    } catch (InterruptedException ex) {
                    } catch (InvocationTargetException ex) {
                    }
                    deltaLastMillis = newMillis;
                }
            }};
        runThread=new Thread(runnable);
    }
}
public interface TweenListener {
    public void start();
    public void finish();
    public void pause();
    public void stop();
}

导入tween-engine-api并写入上文代码,即可使用。怎么用?看方法名呀(逃
反正来看的都是来“使用”的吧(逃
PS:单位是像素,就是Swing的单位(((

Q.E.D.


在校大学生一名 熟悉java/cpp
正在摸索JavaFX ing......