摆动速度限制 [英] Swing rate limiting

查看:119
本文介绍了摆动速度限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很长的重绘操作,我想限速,即确保它最多每N毫秒调用一次。

I have a long repaint operation that I would like to rate-limit, ie make sure it gets called once every N milliseconds at most.

我目前的解决方案是有些不满意:我使用Swing 计时器,持续时间很短,每当我收到一个事件时,我 restart()。这样可确保在N毫秒窗口内发生事件时不会触发重绘(即重新绘制将在事件发生并且N毫秒安静时间段过后)。

My current solution is somewhat unsatisfactory: I use a Swing Timer with a short duration, which I restart() whenever I get an event. This make sure that a repaint is not triggered if events happen within an N milliseconds window (i.e. the repaint will happen after an event and an N milliseconds "quiet" period has elapsed).

我想要的是确保在第一个事件后最多N毫秒调用重绘,并且重绘时间最多为N.任何想法? (未记录在已启动的计时器上调用 start 的效果)。

What I would like is to make sure the repaint is called at most N milliseconds after the first event and that the repaint period is at most N. Any ideas? (the effect of calling start on an already-started Timer is not documented).

推荐答案

因为我只知道来自Native OS的延迟,如果你将溢出这个刷新率然后来自RepaintManager的错误,顺便说一句你的话题是非常学术的
延迟是一个像素/每秒的最大刷新率

there as are I know only Latency that came from Native OS, if you will overflow this refresh rate then comes Error(s) from RepaintManager, btw your topic is very academic Latency is maximum refresh rate for one pixel / per seconds

this one

或其他应该是测试从RepaintManager直到/ UpTo第一个错误的良好基础(然后可能锁定当前JVM实例)
来自代码

or another should be good bases for testing untill / UpTo first Error(s) comings from RepaintManager (then probably lock current JVM instance) from code

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class AnimationBackground {

    public AnimationBackground() {
        Random random = new Random();
        JFrame frame = new JFrame("Animation Background");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setLayout(new GridLayout(0, 1, 10, 10));
        //frame.setLayout(new GridLayout(0, 3, 10, 10));
        //for (int iPanels = 0; iPanels < 3; iPanels++) {
        for (int iPanels = 0; iPanels < 1; iPanels++) {
            final MyJPanel panel = new MyJPanel();
            panel.setBackground(Color.BLACK);
            for (int i = 0; i < 100; i++) {
                Star star = new Star(new Point(random.nextInt(490), random.nextInt(490)));
                star.setColor(new Color(100 + random.nextInt(155), 100 + random.nextInt(155), 100 + random.nextInt(155)));
                star.setxIncr(-3 + random.nextInt(7));
                star.setyIncr(-3 + random.nextInt(7));
                panel.add(star);
            }
            panel.setLayout(new GridLayout(10, 1));
            JLabel label = new JLabel("This is a Starry background.", JLabel.CENTER);
            label.setForeground(Color.WHITE);
            panel.add(label);
            JPanel stopPanel = new JPanel();
            stopPanel.setOpaque(false);
            stopPanel.add(new JButton(new AbstractAction("Stop this madness!!") {

                private static final long serialVersionUID = 1L;

                @Override
                public void actionPerformed(ActionEvent e) {
                    panel.stopAnimation();
                }
            }));
            panel.add(stopPanel);
            JPanel startPanel = new JPanel();
            startPanel.setOpaque(false);
            startPanel.add(new JButton(new AbstractAction("Start moving...") {

                private static final long serialVersionUID = 1L;

                @Override
                public void actionPerformed(ActionEvent e) {
                    panel.startAnimation();
                }
            }));
            panel.add(startPanel);
            frame.add(panel);
        }
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                AnimationBackground animationBackground = new AnimationBackground();
            }
        });
    }

    class Star extends Polygon {

        private static final long serialVersionUID = 1L;
        private Point location = null;
        private Color color = Color.YELLOW;
        private int xIncr, yIncr;
        static final int WIDTH = 500, HEIGHT = 500;

        Star(Point location) {
            int x = location.x;
            int y = location.y;
            this.location = location;
            this.addPoint(x, y + 8);
            this.addPoint(x + 8, y + 8);
            this.addPoint(x + 11, y);
            this.addPoint(x + 14, y + 8);
            this.addPoint(x + 22, y + 8);
            this.addPoint(x + 17, y + 12);
            this.addPoint(x + 21, y + 20);
            this.addPoint(x + 11, y + 14);
            this.addPoint(x + 3, y + 20);
            this.addPoint(x + 6, y + 12);
        }

        public void setColor(Color color) {
            this.color = color;
        }

        public void move() {
            if (location.x < 0 || location.x > WIDTH) {
                xIncr = -xIncr;
            }
            if (location.y < 0 || location.y > WIDTH) {
                yIncr = -yIncr;
            }
            translate(xIncr, yIncr);
            location.setLocation(location.x + xIncr, location.y + yIncr);
        }

        public void setxIncr(int xIncr) {
            this.xIncr = xIncr;
        }

        public void setyIncr(int yIncr) {
            this.yIncr = yIncr;
        }

        public Color getColor() {
            return color;
        }
    }

    class MyJPanel extends JPanel {

        private static final long serialVersionUID = 1L;
        private ArrayList<Star> stars = new ArrayList<Star>();
        private Timer timer = new Timer(20, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                for (Star star : stars) {
                    star.move();
                }
                repaint();
            }
        });

        public void stopAnimation() {
            if (timer.isRunning()) {
                timer.stop();
            }
        }

        public void startAnimation() {
            if (!timer.isRunning()) {
                timer.start();
            }
        }

        @Override
        public void addNotify() {
            super.addNotify();
            timer.start();
        }

        @Override
        public void removeNotify() {
            super.removeNotify();
            timer.stop();
        }

        MyJPanel() {
            this.setPreferredSize(new Dimension(520, 520));
        }

        public void add(Star star) {
            stars.add(star);
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            for (Star star : stars) {
                g.setColor(star.getColor());
                g.fillPolygon(star);
            }
        }
    }
}

这篇关于摆动速度限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆