Waz*_*Waz -1 java animation swing awt jframe
我是java GUI的新手,我正在努力学习它.我想自动在屏幕上移动一个圆圈(即不能通过按任何键或执行任何其他操作).我找到了一种通过做一些动作来移动它的方法,但这不是我需要的.请问有谁可以告诉我最简单的方法吗?
我想在以下代码中删除动作侦听器,以便圆圈自动移动:
public class MyFirstGraphics extends JFrame {
int x = 100;
int y = 100;
MyFirstGraphics() {
super("Circle");
setSize(800, 800);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setBackground(Color.pink);
JButton f = new JButton("circle");
f.addActionListener(new re());
add(f, BorderLayout.NORTH);
}
private class re implements ActionListener {
public void actionPerformed(ActionEvent e) {
for (int i = 1; i < 50; i++) {
x++;
y++;
repaint();
}
}
}
public void paint(Graphics g) {
super.paint(g);
g.drawOval(x, y, 100, 100);
}
public static void main(String[] args) {
MyFirstGraphics l = new MyFirstGraphics();
l.setVisible(true);
}
}
Run Code Online (Sandbox Code Playgroud)
让我们从动画是随时间变化的幻觉开始.此外,Swing是一个单线程环境.你不能"阻止"Swing线程(也就是事件调度线程)并让它绘制更新,你需要一些方法来定期更新更新,这样你就可以应用更改,然后重新绘制更改...
所以你的第一个问题就是你的actionPerformed方法......
for (int i = 1; i < 50; i++) {
x++;
y++;
repaint();
}
Run Code Online (Sandbox Code Playgroud)
基本上,唯一可以画的是150x150的球,之间没有其他任何东西都会被画上.
相反,你需要将它改为更像......
public void actionPerformed(ActionEvent e) {
if (x < 150 && y < 150) {
x++;
y++;
} else {
((Timer)e.getSource()).stop();
}
repaint();
}
Run Code Online (Sandbox Code Playgroud)
看一眼:
更多细节
一个基本的"弹性"球的例子

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class BounceyDot {
public static void main(String[] args) {
new BounceyDot();
}
public BounceyDot() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private int x = 0;
private int y = 100;
private int radius = 20;
private int xDelta = 2;
public TestPane() {
Timer timer = new Timer(40, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
x += xDelta;
if (x + (radius * 2) > getWidth()) {
x = getWidth() - (radius * 2);
xDelta *= -1;
} else if (x < 0) {
x = 0;
xDelta *= -1;
}
repaint();
}
});
timer.start();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
g.fillOval(x, y - radius, radius * 2, radius * 2);
}
}
}
Run Code Online (Sandbox Code Playgroud)