我想用Java制作简单的动画。我读了几篇文章并做到了:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.util.Random;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class MovingCircle extends JComponent implements ActionListener {
private static final String APP_NAME = "Animation";
private static final Color BACKGROUND_COLOR = Color.BLACK;
private static final Color CIRCLE_COLOR = Color.WHITE;
private static final int CIRCLE_DIAMETER = 20;
private static final int WINDOW_WIDTH = 300;
private static final int WINDOW_HEIGHT = 300;
private static int x;
private static int y;
private static int yMax;
private static Timer timer;
private static final int DELAY = 1;
private final Random random = new Random();
private static final Ellipse2D ELLIPSE2D = new Ellipse2D.Double(x, y, CIRCLE_DIAMETER, CIRCLE_DIAMETER);
private static RenderingHints renderingHints;
public MovingCircle(Color color, int delay) {
setDoubleBuffered(true);
setPreferredSize(new Dimension(WINDOW_WIDTH, WINDOW_HEIGHT));
timer = new Timer(delay, this);
}
public void start() {
timer.start();
}
public void stop() {
timer.stop();
}
@Override
public void actionPerformed(ActionEvent arg0) {
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHints(renderingHints);
if (y < yMax) {
y++;
} else {
y = 0;
x = random.nextInt(WINDOW_WIDTH);
}
ELLIPSE2D.setFrame(x, y, CIRCLE_DIAMETER, CIRCLE_DIAMETER);
g2d.setColor(CIRCLE_COLOR);
g2d.fill(ELLIPSE2D);
}
private static MovingCircle movingCircle;
public static void main(String[] args) {
// System.setProperty("sun.java2d.opengl", "True");
// Toolkit.getDefaultToolkit().sync();
ELLIPSE2D.setFrame(x, y, CIRCLE_DIAMETER, CIRCLE_DIAMETER);
renderingHints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
renderingHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame jFrame = new JFrame(APP_NAME);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
jFrame.setResizable(true);
jFrame.setLocationByPlatform(true);
JPanel jPanel = new JPanel();
jPanel.setDoubleBuffered(true);
jPanel.setBackground(BACKGROUND_COLOR);
movingCircle = new MovingCircle(CIRCLE_COLOR, DELAY);
jPanel.add(movingCircle);
jFrame.getContentPane().add(jPanel);
jFrame.setVisible(true);
// jFrame.setResizable(false);
yMax = jPanel.getHeight();
movingCircle.start();
}
});
}
}
然而,我在不同的操作系统上启动它并且感到惊讶 - 它的作用并不相同!我试图打开和关闭两个选项:
System.setProperty("sun.java2d.opengl", "True");
Toolkit.getDefaultToolkit().sync();
这里我对具有大约3.5 GHz的双核CPU和大致相同的其他参数的计算机的观察: 1. Linux - 动画可以快速均匀地运动,当两个选项都关闭或只包含sync()时,CPU加载约5%。当两者都包括在内时,有制动和加速,CPU为15-20%。 Windows - 动画工作流畅,均匀,但速度极慢! CPU负载为0-1% 3. OS X - 动画加速和制动,任何选项的CPU负载为30-100%。
我的目的: 1.一个人物的快速运动。即没有制动和加速,没有间隙。 2.所有平台上的运动速度相同。 3.处理器负载低。
我怎么能实现它?我很震惊为什么这些电脑很好地复制电影或游戏,以及这种原始动画如此糟糕?!也许我做错了什么?
感谢您的关注!