简单动画的Java代码仅在Windows上运行

时间:2012-10-22 14:13:23

标签: java macos swing animation paint

在这个弹跳球动画的简单代码示例中:

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机器,两者都会绘制球和背景(经过漫长的等待),但不会继续动画。

在这里是否存在某种特定于平台的代码? 谢谢!

2 个答案:

答案 0 :(得分:3)

在UI线程中调用

paint()方法,它应该尽可能快地返回。

那么你将把动画代码放在哪里?答案很简单:你需要把代码放到一个单独的线程中。

对于Windows和OS X之间的区别,我只能说它应该与他们如何安排线程或类似的东西有关。

答案 1 :(得分:2)

永远不要在任何阻止或可能触发重绘请求的paint方法中执行任何操作。

你应该总是打电话给super.paintXxx,这些方法在后台做了很多工作,通常情况要好得多。

您不需要(极少数情况下)从顶级容器扩展,例如JAppletJFrame。您最好创建自定义容器(例如JPanel)并将组件添加到其中(或执行自定义绘图)。除了双缓冲支持外,您还可以灵活选择部署。

不要自欺欺人,你不要控制油漆过程,由重漆经理做出决定,但你可以“鼓励”更新。当人们开始玩动画时,这会造成最大的痛苦。

您应该在事件调度线程(EDT)的影响之外修改动画的“状态”,或者在paint上下文之外修改动画的“状态”。

你的问题很简单,一个简单的javax.swing.Timer就可以解决它。更复杂的动画可能需要“动画”线程。

public class GraphicsMovement extends JApplet {

    @Override
    public void init() {
        setLayout(new BorderLayout());
        add(new AnimatedPane());
    }

    @Override
    public void start() {
    }

    public class AnimatedPane extends JPanel {

        private Timer timer;
        private boolean colorSwitch = false;

        private int yOffset = 0;
        private int direction = 1;

        public AnimatedPane() {
            timer = new Timer(10, new ActionListener() {
                public void actionPerformed(ActionEvent e) {
//                    colorSwitch = !colorSwitch;
                    yOffset += direction;
                    if (yOffset > 100) {
                        direction = -1;
                        yOffset = 100;
                    } else if (yOffset < 0){
                        direction = 1;
                        yOffset = 0;
                    }
                    repaint();
                }
            });
            timer.setRepeats(true);
            timer.setCoalesce(true);
            timer.start();

            setBackground(new Color(140, 214, 225));
        }

        @Override
        protected void paintComponent(Graphics page) {
            super.paintComponent(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.

            if (colorSwitch) {
                page.setColor(new Color(140, 214, 225));
            } else {
                page.setColor(Color.YELLOW);
            }
            page.fillOval(100, 55 + yOffset, 100, 100);  //draws a yellow oval
        }
    }
}

我关注的是10毫秒的“延迟”,这足以让我面对健康:P

你可能会发现阅读...

有些兴趣

相关问题