Java Swing:如何平滑动画/移动组件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21247776/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Swing: how to smoothly animate/move component
提问by plattnum
I am trying to figure out how to animate a swing component to go from point a to point b. Here is a baby example of code which makes a red JPanel move from left to right :
我想弄清楚如何为摆动组件设置动画,使其从 a 点移动到 b 点。这是一个让红色 JPanel 从左向右移动的代码示例:
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class MovingSquareExample {
private static final JPanel square = new JPanel();
private static int x = 20;
public static void createAndShowGUI(){
JFrame frame = new JFrame();
frame.getContentPane().setLayout(null);
frame.setSize(500,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(square);
square.setBounds(20,200,100,100);
square.setBackground(Color.RED);
Timer timer = new Timer(1000/60,new MyActionListener());
timer.start();
frame.setVisible(true);
}
public static class MyActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent arg0) {
square.setLocation(x++, 200);
}
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
createAndShowGUI();
}
});
}
}
It works fine, it's just that I looks a little choppy. The motion for the analogous example with a draggable square (see Draggable Components in Java Swing) appears much smoother so I believe there should be a way to make this look better. Any suggestions would be much appreciated.
它工作正常,只是我看起来有点波涛汹涌。带有可拖动方块的类似示例(请参阅Java Swing 中的可拖动组件)的运动看起来更加平滑,因此我相信应该有一种方法可以使它看起来更好。任何建议将不胜感激。
回答by Loa
You are entering a tricky area for the Swing library. However, nothing is impossible. You can create such animation using Timer, but I really recommend you do not do it. So you can move components as best as possible, I suggest you make use of the Timing Frameworklibrary.
您正在为 Swing 库进入一个棘手的领域。然而,没有什么是不可能的。您可以使用 Timer 创建此类动画,但我真的建议您不要这样做。所以你可以尽可能地移动组件,我建议你使用Timing Framework库。
But be aware: Move components is not something that should be made without study. Swing layouts were developed so that the components are placed in a specific order. If you manipulate the values ??of dimensions and positioning of components, you will be breaking the functionality of the layouts, and your program is likely to behave in strange ways. I've had cases where I developed an application in Swing without the use of layout. In an operating system, my program seemed to work properly, but porting it to other systems, everything went into disarray. Therefore, you need to stay tuned and perform many tests before launching an application in Swing that has such customizations.
但请注意:移动组件不是不经研究就应该做的事情。开发了 Swing 布局,以便按特定顺序放置组件。如果您操纵组件的尺寸和位置的值,您将破坏布局的功能,并且您的程序可能会以奇怪的方式运行。我曾遇到过在 Swing 中开发应用程序而不使用布局的案例。在操作系统中,我的程序似乎可以正常运行,但是将其移植到其他系统中,一切都变得混乱了。因此,在 Swing 中启动具有此类自定义功能的应用程序之前,您需要保持关注并执行许多测试。
This was one reason that the JavaFX technology came into our hands. With such technology, we can concern ourselves with less stuff (deploy the application in different programs) and do much more (including the one you're having trouble). Consider migrating to this technology. So you see what JavaFX can do, download the demo program Ensemble. If you gain interest in this technology, I suggest you start learning it here. If you don't want to download the demo, you can also find videoson the internet that demonstrate how it works.
这是 JavaFX 技术进入我们手中的原因之一。有了这样的技术,我们可以关注更少的事情(在不同的程序中部署应用程序)并做更多的事情(包括您遇到问题的那个)。考虑迁移到这项技术。所以你看看 JavaFX 能做什么,下载演示程序Ensemble。如果你对这项技术感兴趣,我建议你从这里开始学习。如果你不想下载演示,你还可以找到视频演示它是如何工作在互联网上。
If this alternative is too laborious for you, check out the link I gave you about the Timing Framework library. There you will find examples of Java code that make smooth animations on various Swing things with a high performance. To learn how to use this library, I suggest you to get the book Filthy Rich Clients, written by Chet Haase and Romain Guy. Although the book is out of date and things have been changed in the library code, you can get updated on the library website. As I said earlier, download the library, and also download the code samples. With time, you will end up doing what you want in the best possible way.
如果这个替代方案对你来说太费力,请查看我给你的关于计时框架库的链接。在那里,您将找到 Java 代码示例,这些示例在各种 Swing 事物上以高性能制作流畅的动画。要了解如何使用该库,我建议您阅读Chet Haase 和 Romain Guy 编写的Filthy Rich Clients一书。尽管这本书已经过时并且图书馆代码中的内容已更改,但您可以在图书馆网站上获得更新。正如我之前所说,下载库,并下载代码示例。随着时间的推移,你最终会以最好的方式做你想做的事。
I hope you can accomplish what you want. Good luck. :)
我希望你能完成你想要的。祝你好运。:)
回答by Bytes
Here is a method I made for animating JComponents using timers.
这是我使用计时器为 JComponent 设置动画的方法。
private void animate(JComponent component, Point newPoint, int frames, int interval) {
Rectangle compBounds = component.getBounds();
Point oldPoint = new Point(compBounds.x, compBounds.y),
animFrame = new Point((newPoint.x - oldPoint.x) / frames,
(newPoint.y - oldPoint.y) / frames);
new Timer(interval, new ActionListener() {
int currentFrame = 0;
public void actionPerformed(ActionEvent e) {
component.setBounds(oldPoint.x + (animFrame.x * currentFrame),
oldPoint.y + (animFrame.y * currentFrame),
compBounds.width,
compBounds.height);
if (currentFrame != frames)
currentFrame++;
else
((Timer)e.getSource()).stop();
}
}).start();
}