使用JComponents的滑动效果菜单

时间:2018-06-01 23:05:52

标签: java swing animation

我试图用JLabels和计时器做一些滑动效果。 我只想使用两个定时器(IN和OUT)来管理多个组件的效果。 问题是,只有当我不能快速从一个JLabel移动到另一个JLabel并且我不知道如何管理这个东西时,一切正常。

Gif showing the problem

这是我的代码:

public class Sliders extends JFrame {

private JPanel contentPane;
JLabel label,label_1;
static RainDrop frame;
 javax.swing.Timer  in,out;
/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                frame = new RainDrop();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public Sliders() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    label = new JLabel("");
    label.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseEntered(MouseEvent a1) {
            setIN(2,0,label);
                System.out.println("ENTRATO");
                checkloop_out_mag(-270,label);

        }
        @Override
        public void mouseExited(MouseEvent a2) {
            in.stop();
            setOUT(-2,0,label);
            System.out.println("USCITO");                       
        }
    });
    label.setBackground(Color.ORANGE);
    label.setOpaque(true);
    label.setBounds(-270, 0, 337, 44);
    contentPane.add(label);

    label_1 = new JLabel("");
    label_1.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseEntered(MouseEvent b1) {
                setIN(2,44,label_1);
                System.out.println("ENTRATO");
                checkloop_out_mag(-270,label_1);
        }
        @Override
        public void mouseExited(MouseEvent b2) {
            in.stop();
            setOUT(-2,44,label_1);
            System.out.println("USCITO");                   
        }
    });
    label_1.setOpaque(true);
    label_1.setBackground(Color.GREEN);
    label_1.setBounds(-270, 44, 337, 44);
    contentPane.add(label_1);
}



public void setIN(int x,int y,JLabel label) {


    in = new javax.swing.Timer(2, new ActionListener() {


                @Override
                public void actionPerformed(ActionEvent e) {



                     label.setLocation(label.getBounds().x+x,y);

                     System.out.println("SPOSTO");

                     System.out.println("CONTROLLO");
                     checkloop_in_magequals(0,label); 

                }
            });
            in.setRepeats(true);
            in.start();



}

public void setOUT(int x,int y,JLabel label) {


    out = new javax.swing.Timer(2, new ActionListener() {


                @Override
                public void actionPerformed(ActionEvent e) {



                     label.setLocation(label.getBounds().x+x,y);

                     System.out.println("SPOSTO");

                     System.out.println("CONTROLLO");
                       checkloop_out_equals(-270,label);


                }
            });
            out.setRepeats(true);
            out.start();



}

public void checkloop_out_equals(int z,JLabel label) {
     if (label.getBounds().x==z){
            out.stop();
            System.out.println("STOP");
     }
}

public void checkloop_out_mag(int z,JLabel label) {
     if (label.getBounds().x>z){
            out.stop();
            System.out.println("STOP");
     }
}


public void checkloop_in_magequals(int z,JLabel label) {
 if (label.getBounds().x>=z){
        in.stop();
        System.out.println("STOP");
     }
 }
}

有没有办法只用两个定时器来修复代码?或者每个JComponent需要两个计时器吗?

1 个答案:

答案 0 :(得分:1)

首先,使用动画框架,如TridentThe Timing FrameworkThe Universal Tween Engine,它们提供了大量的功能,否则需要进行大量的编码才能完成

动画是一个复杂的主题,所以我只是要介绍一些主要的基础知识。动画是随着时间的推移变化的幻觉(告诉你它将是基本的)。

你问题的结果归结为两件基本的事情:

  1. 你需要一个中央时钟"这可以提供" ticks"和(相对)定期间隔
  2. 一个足够麻醉,足够分离,不关心"什么"它的动画,只有在给定持续时间和范围的情况下,它才能计算出每个" tick"
  3. 所需的属性。

    要理解的一个重要概念是,动画应该在一段时间内播放,而不是在值之间播放。主要原因是灵活性。更改动画的持续时间以便更改速度更加容易,并且它允许系统“放弃”#34;框架相对简单。请记住,当我说"动画是随时间变化的幻觉" - 这就是我所说的。

    简单......

    让我们从一些基础知识开始......

    public class Range<T> {
        private T from;
        private T to;
    
        public Range(T from, T to) {
            this.from = from;
            this.to = to;
        }
    
        public T getFrom() {
            return from;
        }
    
        public T getTo() {
            return to;
        }
    
        @Override
        public String toString() {
            return "From " + getFrom() + " to " + getTo();
        }
    
    }
    

    Range描述了一些可以用&#34; start&#34;来衡量的东西。 (from)值和&#34;目标&#34; (to)值。这允许使用提供许多重要的值,但最值得注意的是,&#34;范围&#34;我们想要执行的动画。

    在过去,我已经将这样的概念用于各种价值观,包括PointRectangle甚至Color

    public interface AnimationPropertiesListener<T> {
        public void stateChanged(AnimationProperties<T> animator);
    }
    
    public interface AnimationProperties<T> {
        public Range<T> getRange();
        public T getValue();
        public boolean tick();
    
        public void setDuration(Duration duration);
        public Duration getDuration();
    }
    
    public abstract class AbstractAnimationProperties<T> implements AnimationProperties<T> {
    
        private Range<T> range;     
        private LocalDateTime startTime;
        private Duration duration = Duration.ofSeconds(5);
        private T value;
        private AnimationPropertiesListener<T> listener;
    
        public AbstractAnimationProperties(Range<T> range, AnimationPropertiesListener<T> listener) {
            this.range = range;
            this.value = range.getFrom();
    
            this.listener = listener;
        }
    
        public void setDuration(Duration duration) {
            this.duration = duration;
        }
    
        public Duration getDuration() {
            return duration;
        }
    
        public Range<T> getRange() {
            return range;
        }
    
        @Override
        public T getValue() {
            return value;
        }
    
        @Override
        public boolean tick() {
            if (startTime == null) {
                startTime = LocalDateTime.now();
            }
            Duration duration = getDuration();
            Duration runningTime = Duration.between(startTime, LocalDateTime.now());
            Duration timeRemaining = duration.minus(runningTime);
            if (timeRemaining.isNegative()) {
                runningTime = duration;
            }
            double progress = (runningTime.toMillis() / (double) duration.toMillis());
            value = calculateValue(progress);
    
            listener.stateChanged(this);
    
            return progress >= 1.0;
        }
    
        public abstract T calculateValue(double progress);
    
    }
    

    &#34;动画属性&#34;是我们努力实现的潜在力量。它负责计算动画想要运行的时间量,动画运行的时间量,并提供计算指定动画Range值的结果值的方法。 (注意 calculateValue可能是Range的属性,但我只是把它放在原来的位置)

    好的,这一切都很好,但我们实际上需要一个中央时钟来提供所有的时间表并通知属性

    public enum Animator {
    
        INSTANCE;
    
        private Timer timer;
    
        private List<AnimationProperties> properies;
    
        private Animator() {
            properies = new ArrayList<>(5);
            timer = new Timer(5, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Iterator<AnimationProperties> it = properies.iterator();
                    while (it.hasNext()) {
                        AnimationProperties ap = it.next();
                        if (ap.tick()) {
                            it.remove();
                        }
                    }
                    if (properies.isEmpty()) {
                        timer.stop();
                    }
                }
            });
        }
    
        public void add(AnimationProperties ap) {
            properies.add(ap);
            timer.start();
        }
    
        public void remove(AnimationProperties ap) {
            properies.remove(ap);
            if (properies.isEmpty()) {
                timer.stop();
            }
        }
    
    }
    

    好的,这是一个非常简单的实现。它只是一个非常快的Swing Timer。当AnimationProperties可用时,它会继续循环,直到所有AnimationProperties tick方法返回true(已完成),此时它停止(因此除非{背景中的事情)

    好的,但这对我们有什么帮助呢?

    基本上,我们要做的就是在一段时间内从给定值计算组件的新width到给定值。由于width被定义为int,我们可以基于上面的抽象/泛型类创建一系列具体类,例如......

    public class IntRange extends Range<Integer> {
    
        public IntRange(Integer from, Integer to) {
            super(from, to);
        }
    
        public Integer getDistance() {
            return getTo() - getFrom();
        }
    }
    
    public class IntAnimationProperties extends AbstractAnimationProperties<Integer> {
    
        public IntAnimationProperties(IntRange animationRange, IntRange maxRange, Duration duration, AnimationPropertiesListener<Integer> listener) {
            super(animationRange, listener);
    
            int maxDistance = maxRange.getDistance();
            int aniDistance = animationRange.getDistance();
    
            double progress = Math.min(100, Math.max(0, Math.abs(aniDistance/ (double)maxDistance)));
            Duration remainingDuration = Duration.ofMillis((long)(duration.toMillis() * progress));
            setDuration(remainingDuration);
        }
    
        @Override
        public Integer calculateValue(double progress) {
            IntRange range = (IntRange)getRange();
            int distance = range.getDistance();
            int value = (int) Math.round((double) distance * progress);
            value += range.getFrom();
            return value;
        }
    
    }
    

    这里需要注意的是,IntAnimationProperties还需要一个&#34;最大范围&#34;值。这是动画可能会运行的总可用范围。这用于计算当前&#34;进展&#34;值&#34;动画范围&#34;是在。

    考虑当用户退出时,如果一个面板扩展了一半会发生什么?通常情况下,它必须使用整个持续时间来动画回半个范围。

    相反,此实现计算&#34;剩余的&#34;在上面的例子中,移动到期望点所需的持续时间是正常持续时间的一半。

    实施例...

    因此,基于上述情况,我们最终会得到像......

    这样的东西

    Slidy

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.time.Duration;
    import java.time.LocalDateTime;
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.Timer;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class Test {
    
        public static void main(String[] args) {
            new Test();
        }
    
        public Test() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                        ex.printStackTrace();
                    }
    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class TestPane extends JPanel {
    
            public TestPane() {
                setLayout(null);
                Slider slider1 = new Slider();
                slider1.setBackground(Color.BLUE);
                slider1.setLocation(0, 44);         
                add(slider1);
    
                Slider slider2 = new Slider();
                slider2.setBackground(Color.MAGENTA);
                slider2.setLocation(0, 88);
                add(slider2);
            }
    
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(200, 200);
            }
    
        }
    
        public class Slider extends JPanel {
    
            private AnimationProperties<Integer> ap;
    
            private IntRange maxRange = new IntRange(44, 150);
            private Duration duration = Duration.ofSeconds(5);
    
            public Slider() {
                setSize(44, 44);
    
                addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseEntered(MouseEvent e) {
                        animateTo(150);
                    }
    
                    @Override
                    public void mouseExited(MouseEvent e) {
                        animateTo(44);
                    }
    
                    public void animateTo(int to) {
                        if (ap != null) {
                            Animator.INSTANCE.remove(ap);
                        }
                        IntRange animationRange = new IntRange(getWidth(), to);
                        ap = new IntAnimationProperties(animationRange, maxRange, duration, new AnimationPropertiesListener<Integer>() {
                            @Override
                            public void stateChanged(AnimationProperties<Integer> animator) {
                                setSize(animator.getValue(), 44);
                                repaint();
                            }
                        });
                        Animator.INSTANCE.add(ap);
                    }
    
                });
            }
    
        }
    
        public enum Animator {
    
            INSTANCE;
    
            private Timer timer;
    
            private List<AnimationProperties> properies;
    
            private Animator() {
                properies = new ArrayList<>(5);
                timer = new Timer(5, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        Iterator<AnimationProperties> it = properies.iterator();
                        while (it.hasNext()) {
                            AnimationProperties ap = it.next();
                            if (ap.tick()) {
                                it.remove();
                            }
                        }
                        if (properies.isEmpty()) {
                            timer.stop();
                        }
                    }
                });
            }
    
            public void add(AnimationProperties ap) {
                properies.add(ap);
                timer.start();
            }
    
            public void remove(AnimationProperties ap) {
                properies.remove(ap);
                if (properies.isEmpty()) {
                    timer.stop();
                }
            }
    
        }
    
        public interface AnimationProperties<T> {
            public Range<T> getRange();
            public T getValue();
            public boolean tick();
    
            public void setDuration(Duration duration);
            public Duration getDuration();
        }
    
        public interface AnimationPropertiesListener<T> {
            public void stateChanged(AnimationProperties<T> animator);
        }
    
        public class Range<T> {
            private T from;
            private T to;
    
            public Range(T from, T to) {
                this.from = from;
                this.to = to;
            }
    
            public T getFrom() {
                return from;
            }
    
            public T getTo() {
                return to;
            }
    
            @Override
            public String toString() {
                return "From " + getFrom() + " to " + getTo();
            }
    
        }
    
        public abstract class AbstractAnimationProperties<T> implements AnimationProperties<T> {
    
            private Range<T> range;
    
            private LocalDateTime startTime;
    
            private Duration duration = Duration.ofSeconds(5);
    
            private T value;
    
            private AnimationPropertiesListener<T> listener;
    
            public AbstractAnimationProperties(Range<T> range, AnimationPropertiesListener<T> listener) {
                this.range = range;
                this.value = range.getFrom();
    
                this.listener = listener;
            }
    
            public void setDuration(Duration duration) {
                this.duration = duration;
            }
    
            public Duration getDuration() {
                return duration;
            }
    
            public Range<T> getRange() {
                return range;
            }
    
            @Override
            public T getValue() {
                return value;
            }
    
            @Override
            public boolean tick() {
                if (startTime == null) {
                    startTime = LocalDateTime.now();
                }
                Duration duration = getDuration();
                Duration runningTime = Duration.between(startTime, LocalDateTime.now());
                Duration timeRemaining = duration.minus(runningTime);
                if (timeRemaining.isNegative()) {
                    runningTime = duration;
                }
                double progress = (runningTime.toMillis() / (double) duration.toMillis());
                value = calculateValue(progress);
    
                listener.stateChanged(this);
    
                return progress >= 1.0;
            }
    
            public abstract T calculateValue(double progress);
    
        }
    
        public class IntRange extends Range<Integer> {
    
            public IntRange(Integer from, Integer to) {
                super(from, to);
            }
    
            public Integer getDistance() {
                return getTo() - getFrom();
            }
        }
    
        public class IntAnimationProperties extends AbstractAnimationProperties<Integer> {
    
            public IntAnimationProperties(IntRange animationRange, IntRange maxRange, Duration duration, AnimationPropertiesListener<Integer> listener) {
                super(animationRange, listener);
    
                int maxDistance = maxRange.getDistance();
                int aniDistance = animationRange.getDistance();
    
                double progress = Math.min(100, Math.max(0, Math.abs(aniDistance/ (double)maxDistance)));
                Duration remainingDuration = Duration.ofMillis((long)(duration.toMillis() * progress));
                setDuration(remainingDuration);
            }
    
            @Override
            public Integer calculateValue(double progress) {
                IntRange range = (IntRange)getRange();
                int distance = range.getDistance();
                int value = (int) Math.round((double) distance * progress);
                value += range.getFrom();
                return value;
            }
    
        }
    
    }
    

    但那真的很慢☹️

    好的,两个地方你改变了持续时间。 AbstractAnimationProperties的默认Duration5秒,而Slider的默认Duration5秒。在您的情况下,更改Slider s Duration可能就是您要开始的地方

    哦,等等,你想要&#34;地役权&#34; (慢/慢)到?那么,去看看How can I implement easing functions with a thread,然后去看看我之前链接的动画框架,因为他们已经这样做了

    现在,如果您真的想要手动执行此操作 - 您可以查看this gist这是一个&#34;时间线&#34;基于支持地役权的实施