Java Applet动画不起作用(传送)

时间:2016-11-30 03:10:08

标签: java animation applet awt event-dispatch-thread

我正在尝试创建一个简单的动画,我的程序会在按下按钮时从左到右动画指定的形状。但是,当我运行程序并按下动画按钮时,形状从屏幕左侧开始,但是传送到屏幕中间而没有过渡/动画。如果有人可以帮我解释为什么我的paint()方法没有动画以及如何修复它,我们将非常感激。

绘画方法:

public void paint (Graphics g)
{
    g.drawString ("Set Size (1-400)", 640, 80);

    if (animate == true)
    {
        if (time > 0)
        {
            switch (colourNum)
            {
                case 1:
                    g.setColor (Color.red);
                    break;

                case 2:
                    g.setColor (Color.green);
                    break;

                case 3:
                    g.setColor (Color.blue);
                    break;

                case 4:
                    g.setColor (Color.yellow);
                    break;
            }

            switch (shapeNum)
            {
                case 1:
                    g.fillRect (x, y, size, size);
                    g.setFont (textFont);
                    g.drawString ("Shape: Square", x, 800);
                    g.drawString ("Size: " + size, x, 840);
                    break;

                case 2:
                    g.fillRoundRect (x, y, size, size, 30, 30);
                    g.setFont (textFont);
                    g.drawString ("Shape: Round Square", x, 800);
                    g.drawString ("Size: " + size, x, 840);
                    break;

                case 3:
                    g.fillOval (x, y, size, size);
                    g.setFont (textFont);
                    g.drawString ("Shape: Circle", x, 800);
                    g.drawString ("Size: " + size, x, 840);
                    break;

                case 4:
                    g.fillArc (x, y, size, size, 360, 180);
                    g.setFont (textFont);
                    g.drawString ("Shape: Semi Circle", x, 800);
                    g.drawString ("Size: " + size, x, 840);
                    break;
            }

            x = x + 5;
            time = time - 1;

            try
            {
                Thread.sleep (100);
            }
            catch (InterruptedException ex)
            {
            }

            repaint ();
        }
        animate = false;
    }

行动执行方法代码:

else if (evt.getSource () == bttn2)
    {
        if (draw == false)
        {
            JOptionPane.showMessageDialog (null, "Invalid order of user-input has occured\n     DRAW BEFORE ANIMATE", "Exception or Error", 0);

        }
        else
        {
            animate = true;
            time = 500;
            x = 0;
            repaint ();
        }
    }

0 个答案:

没有答案