gpt4 book ai didi

java - 摆幅限制

转载 作者:行者123 更新时间:2023-11-30 06:34:17 25 4
gpt4 key购买 nike

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

我目前的解决方案有点不尽如人意:我使用一个持续时间较短的 Swing Timer,每当我收到一个事件时我都会restart()。这确保如果事件在 N 毫秒窗口内发生,则不会触发重绘(即重绘将在事件发生并且 N 毫秒“安静”时间段过去后发生)。

我想要确保重绘在第一个事件后最多 N 毫秒被调用,并且重绘周期最多为 N。有什么想法吗? (未记录在已启动的 Timer 上调用 start 的效果)。

最佳答案

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

但是this one enter image description here

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

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);
}
}
}
}

关于java - 摆幅限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7206122/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com