gpt4 book ai didi

java - 有没有办法在 Java 中为整个 Jframe 制作动画,使其移动?

转载 作者:行者123 更新时间:2023-11-30 04:29:43 24 4
gpt4 key购买 nike

我想创建一个程序,其中 Jframe 能够自行自由移动。有点像翻译/过渡。

例如,

  1. 点击程序开始。

  2. Jframe 在位置 (0,0) 处生成。

  3. 自动向右移动(动画)100 像素,使新坐标为 (100,0)。

我知道有 setLocation(x,y) 方法可以在程序运行后设置初始位置,但是有没有办法在程序启动后移动整个 Jframe?

最佳答案

基本概念看起来像这样......

public class MoveMe01 {

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

public MoveMe01() {

EventQueue.invokeLater(
new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
}

final JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JLabel("Use the Force Luke"));
frame.pack();
frame.setLocation(0, 0);
frame.setVisible(true);

Timer timer = new Timer(40, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Point location = frame.getLocation();
Point to = new Point(location);
if (to.x < 100) {
to.x += 4;
if (to.x > 100) {
to.x = 100;
}
}
if (to.y < 100) {
to.y += 4;
if (to.y > 100) {
to.y = 100;
}
}

frame.setLocation(to);

if (to.equals(location)) {
((Timer)e.getSource()).stop();
}
}
});
timer.setRepeats(true);
timer.setCoalesce(true);
timer.start();

}
});
}
}

这是一个非常直接的线性动画。您最好研究可用于 Swing 的众多动画引擎之一,这将使您能够根据当前帧更改动画的速度(例如执行慢入和慢出等操作)

我想看看

更新为“可变时间”解决方案

这基本上是如何制作可变时间动画的示例。也就是说,不是有一个固定的运动,你可以调整时间并让动画根据动画的运行时间来计算运动要求...

public class MoveMe01 {

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

// How long the animation should run for in milliseconds
private int runTime = 500;
// The start time of the animation...
private long startTime = -1;

public MoveMe01() {

EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
}

final JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JLabel("Use the Force Luke"));
frame.pack();
frame.setLocation(0, 0);
frame.setVisible(true);

Timer timer = new Timer(40, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {

if (startTime < 0) {
// Start time of the animation...
startTime = System.currentTimeMillis();
}
// The current time
long now = System.currentTimeMillis();
// The difference in time
long dif = now - startTime;
// If we've moved beyond the run time, stop the animation
if (dif > runTime) {
dif = runTime;
((Timer)e.getSource()).stop();
}
// The percentage of time we've been playing...
double progress = (double)dif / (double)runTime;

Point location = frame.getLocation();
Point to = new Point(location);

// Calculate the position as perctange over time...
to.x = (int)Math.round(100 * progress);
to.y = (int)Math.round(100 * progress);
// nb - if the start position wasn't 0x0, then you would need to
// add these to the x/y position above...

System.out.println(to);

frame.setLocation(to);
}
});
timer.setRepeats(true);
timer.setCoalesce(true);
timer.start();

}
});
}
}

关于java - 有没有办法在 Java 中为整个 Jframe 制作动画,使其移动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14950694/

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