在这个弹跳球动画的简单代码示例中:
import javax.swing.JApplet;
import javax.swing.JFrame;
import java.awt.*;
public class GraphicsMovement extends JApplet
{
public static void pause()
{
try {
Thread.sleep(10);
} catch(InterruptedException e) {
}
}
public static void main(String args[])
{
JApplet example = new GraphicsMovement();
JFrame frame = new JFrame("Movement");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(example);
frame.setSize(new Dimension(500,300)); //Sets the dimensions of panel to appear when run
frame.setVisible(true);
}
public void paint (Graphics page)
{
int width = getWidth(); // width = the width of the panel which appears when run
int height = getHeight(); // height = the height of the panel which appears when run.
//Changes background color to a blueish color
page.setColor(new Color (140,214,225));
page.fillRect(0,0,width,height);
for(int i = 0; i <= 5; i++)
{
for (int j = 0; j <= 100; j++)
{
page.setColor(Color.YELLOW);
page.fillOval(100,55 + j,100,100); //draws a yellow oval
pause();
page.setColor(new Color (140,214,225));
page.fillOval(100,55 + j,100,100); //draws a blueish oval over the yellow oval
}
for (int k = 100; k >= 0; k--)
{
page.setColor(Color.YELLOW);
page.fillOval(100,55 + k,100,100); //draws a yellow oval
pause();
if (k != 0)
{
page.setColor(new Color (140,214,225)); //draws a blueish oval over the yellow oval
page.fillOval(100,55 + k,100,100);
}
}
}
}
}
动画画得很好,可以在 Windows 机器上运行(使用 JCreator),但不会在用 IntelliJ 或 Eclipse 编译的 Mac OS X 上运行。在两台不同的 OS X 机器上尝试过,两者都会绘制球和背景(经过长时间的等待),但不会继续动画。
我在这里缺少某种特定于平台的代码吗?谢谢!