我已经写了以下代码
import java.awt.BorderLayout;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
final public class Test
{
JFrame frame;
DrawPanel drawPanel;
boolean up = false;
boolean down = true;
boolean left = false;
boolean right = true;
private int timeStep = 0;
private int ballYTravel = 100;
private int BALL_NUM = 24;
public static void main(String... args)
{
new Test().go();
}
private void go()
{
frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
drawPanel = new DrawPanel();
frame.getContentPane().add(BorderLayout.CENTER, drawPanel);
frame.setResizable(false);
frame.setSize(800, 600);
frame.setLocationByPlatform(true);
frame.setVisible(true);
moveIt();
}
class DrawPanel extends JPanel
{
private static final long serialVersionUID = 1L;
public double getY(int i, int t) {
return 200 + ballYTravel / 2 * (Math.sin(timeStep * (i / 200 + 0.08)));
}
public void paintComponent(Graphics g)
{
for (int k = 0; k < BALL_NUM; k++ ) {
g.fillRect(100 + 20 *k , (int) getY(k, timeStep), 6, 6);
}
timeStep++;
}
}
private void moveIt()
{
while (true)
{
try
{
Thread.sleep(10);
}
catch (Exception e)
{
e.printStackTrace();
}
frame.repaint();
}
}
}
它运行并动画,但它的动画方式与我引用它的Javascript代码的动画方式相同http://codepen.io/anon/pen/ZYQoQZ
理解为什么感激的任何帮助
答案 0 :(得分:5)
您的音译揭示了几个问题:
应在event dispatch thread上仅构建和操作Swing GUI对象。
如果您真的要覆盖getPreferredSize()
,请不要使用setSize()
。
调用pack()
让容器采用其首选尺寸。
使用javax.swing.Timer
来调整动画速度。
修改后的代码,合并@ Mad的fix并使用drawOval()
:
import java.awt.BorderLayout;
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;
final public class Test {
JFrame frame;
DrawPanel drawPanel;
private int timeStep = 0;
private int ballYTravel = 100;
private int BALL_NUM = 24;
public static void main(String... args) {
new Test().go();
}
private void go() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
drawPanel = new DrawPanel();
frame.add(BorderLayout.CENTER, drawPanel);
frame.setResizable(false);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
Timer t = new Timer(10, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
drawPanel.repaint();
}
});
t.start();
}
});
}
private class DrawPanel extends JPanel {
public double getY(int i, int t) {
return 200 + ballYTravel / 2 * (Math.sin(t * (i / 200d + 0.08)));
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (int k = 0; k < BALL_NUM; k++) {
g.drawOval(100 + 20 * k, (int) getY(k, timeStep), 8, 8);
}
timeStep++;
}
@Override
public Dimension getPreferredSize() {
return new Dimension(700, 500);
}
}
}
答案 1 :(得分:5)
有(可能)两个基本问题......
getY
中,您忽略参数t
并使用timeStep
代替,而从技术上讲,这可能不会产生巨大的差异,它是一个区域关注i/200
会产生int
结果,您真正需要double
。将其更改为i/200d
例如......
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;
final public class Test {
private int timeStep = 0;
private final int ballYTravel = 100;
private final int BALL_NUM = 24;
public static void main(String... args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new DrawPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
class DrawPanel extends JPanel {
private static final long serialVersionUID = 1L;
public DrawPanel() {
new Timer(10, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
timeStep++;
repaint();
}
}).start();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 200);
}
public double getY(int i, int t) {
return 100 + ballYTravel / 2 * (Math.sin(t * (i / 200d + 0.08)));
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (int k = 0; k < BALL_NUM; k++) {
g.fillRect(10 + 20 * k, (int) getY(k, timeStep), 6, 6);
}
}
}
}
你也打破了油漆链,从长远来看会导致你的问题,确保你打电话给super.paintComponent
......
有关详细信息,请参阅...