gpt4 book ai didi

java - Swing 动画优化

转载 作者:行者123 更新时间:2023-12-01 22:06:19 26 4
gpt4 key购买 nike

我一直在使用JComponent 上的Timer 制作一个简单的动画。然而,当我观看动画时,我遇到了令人难以置信的断断续续的运动。我应该采取什么步骤来优化这段代码?

MyAnimationFrame

import javax.swing.*;

public class MyAnimationFrame extends JFrame {
public MyAnimationFrame() {
super("My animation frame!");
setSize(300,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(new AnimationComponent(0,0,50,50));
setVisible(true);
}

public static void main(String[] args) {
MyAnimationFrame f = new MyAnimationFrame();
}
}

AnimationComponent

import javax.swing.*;
import java.awt.*;

public class AnimationComponent extends JComponent implements ActionListener {
private Timer animTimer;
private int x;
private int y;
private int xVel;
private int yVel;
private int width;
private int height;
private int oldX;
private int oldY;

public AnimationComponent(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.height = height;
this.width = width;

animTimer = new Timer(25, this);
xVel = 5;
yVel = 5;

animTimer.start();
}

@Override
public void paintComponent(Graphics g) {
g.fillOval(x,y,width,height);
}

@Override
public void actionPerformed(ActionEvent e) {
oldX = x;
oldY = y;

if(x + width > getParent().getWidth() || x < 0) {
xVel *= -1;
}

if(y + height > getParent().getHeight() || y < 0) {
yVel *= -1;
}

x += xVel;
y += yVel;

repaint();
}
}

不确定这是否重要,但我使用的是 OpenJDK 版本 1.8.0_121。

感谢任何帮助。

最佳答案

与 Yago 进行了一次精彩的讨论后,我发现这个问题涉及多个领域,很大程度上取决于 Java 与操作系统和硬件同步更新的能力,有些事情你可以控制,有些你可以't。

受到 Yago 的示例和我对计时框架如何工作的“内存”的启发,我通过增加帧速率(至 5 毫秒,~= 200fps)并减少变化增量来测试您的代码,这与使用时序框架,但它为您提供了原始设计的灵 active 。

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class Test {

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

public Test() {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame("Test");
frame.add(new AnimationComponent(0, 0, 50, 50));
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class AnimationComponent extends JComponent implements ActionListener {

private Timer animTimer;
private int x;
private int y;
private int xVel;
private int yVel;
private int width;
private int height;
private int oldX;
private int oldY;

public AnimationComponent(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.height = height;
this.width = width;

animTimer = new Timer(5, this);
xVel = 1;
yVel = 1;

animTimer.start();
}

@Override
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g.create();
RenderingHints hints = new RenderingHints(
RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON
);
g2d.setRenderingHints(hints);
g2d.fillOval(x, y, width, height);
}

@Override
public void actionPerformed(ActionEvent e) {
oldX = x;
oldY = y;

if (x + width > getParent().getWidth() || x < 0) {
xVel *= -1;
}

if (y + height > getParent().getHeight() || y < 0) {
yVel *= -1;
}

x += xVel;
y += yVel;

repaint();
}
}
}

如果您需要进一步减慢速度,则进一步减少变化增量,这意味着您必须使用double,这将导致Shape 支持双值的API

你应该使用哪个?这取决于你。计时框架非常适合一段时间内的线性动画,您知道自己想要从一种状态转到另一种状态。这对于游戏之类的东西来说不太好,因为游戏中对象的状态可能会从我的周期改变到另一个周期。我确信你可以做到,但是使用简单的“主循环”概念会容易得多 - 恕我直言

关于java - Swing 动画优化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58686151/

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