在applet上显示旋转风扇

时间:2013-04-19 02:12:50

标签: java multithreading swing animation paintcomponent

作为编程任务的一部分,我必须在小程序中显示旋转风扇。

这是我的代码(显示粉丝的类):

import javax.swing.*;
import java.awt.*;

public class Fan extends JPanel
{
    private int angle1 = -15;
    private int angle2 = 75;
    private int angle3 = 165;
    private int angle4 = 255;

    public Fan()
    {
        this.setSize(600, 400);
        Runnable spinner = new SpinFan();

        Thread thread1 = new Thread(spinner);
        thread1.start();
    }


    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);

        g.drawOval(200, 150, 150, 150);
        g.fillArc(210, 160, 130, 130, angle1, 30);
        g.fillArc(210, 160, 130, 130, angle2, 30);
        g.fillArc(210, 160, 130, 130, angle3, 30);
        g.fillArc(210, 160, 130, 130, angle4, 30);
    }
}

class SpinFan implements Runnable
{
    @Override
    public void run() 
    {
        try
        {
            while(true)
            {
                angle1 = (angle1 + 1) % 360;
                angle2 = (angle2 + 1) % 360;
                angle3 = (angle3 + 1) % 360;
                angle4 = (angle4 + 1) % 360;

                System.out.println(angle1 + " " + angle2 + " " + angle3 + " " + angle4);

                repaint();

                Thread.sleep(10);
            }
        }
        catch(InterruptedException ex)
        {
            System.out.println("Problem while putting thread to sleep.");
        }
    }
}

进一步处理的类(现在只有Fan的实例):

import java.awt.*;
import javax.swing.*;

public class FanControl extends JPanel
{
    public FanControl()
    {
        add(new Fan());
    }
}

最后,这是Applet类:

import java.awt.*;
import javax.swing.*;

public class FanApplet extends JApplet
{   
    public FanApplet()
    {
        add(new FanControl());
    }
}

现在我已经尝试了很长时间的各种各样的东西,所以请不要介意额外注释掉的代码。 Fan.jav一个类正常工作(如果我将它作为一个应用程序运行,我可以看到它旋转到一个框架中)。但我只是无法让风扇在Applet中旋转。但是,如果我从Fan.java类向Applet添加类似 JButton 的东西,它就可以工作。

我错过了什么?使用线程和applet,或者我似乎不知道的paintComponent()和applet时是否存在一些复杂情况。

当我将代码作为应用程序运行时,它运行正常。我可以看到旋转风扇。这是代码:

import javax.swing.*;
import java.awt.*;

public class Fan extends JPanel
{
private int angle1 = -15;
private int angle2 = 75;
private int angle3 = 165;
private int angle4 = 255;

public Fan()
{
    Runnable spinner = new SpinFan();

    Thread thread1 = new Thread(spinner);
    thread1.start();
}


public static void main(String[] args)
{   
    JFrame frame = new JFrame();
    frame.add(new Fan());
    frame.setSize(600, 400);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}


@Override
protected void paintComponent(Graphics g)
{
    super.paintComponent(g);

    g.drawOval(200, 150, 150, 150);
    g.fillArc(210, 160, 130, 130, angle1, 30);
    g.fillArc(210, 160, 130, 130, angle2, 30);
    g.fillArc(210, 160, 130, 130, angle3, 30);
    g.fillArc(210, 160, 130, 130, angle4, 30);

}

class SpinFan implements Runnable
{
    @Override
    public void run() 
    {
        try
        {
            while(true)
            {
                angle1 = (angle1 - 1) % 360;
                angle2 = (angle2 - 1) % 360;
                angle3 = (angle3 - 1) % 360;
                angle4 = (angle4 - 1) % 360;

                System.out.println(angle1 + " " + angle2 + " " + angle3 + " " + angle4);

                repaint();

                Thread.sleep(10);
            }
        }

        catch(InterruptedException ex)
        {
            System.out.println("Problem while putting thread to sleep.");
        }
    }

}
}

1 个答案:

答案 0 :(得分:2)

while(true) .. repaint(); .. Thread.sleep(10);

这完全是错误的动画方式。

不要阻止EDT(事件调度线程) - 当发生这种情况时,GUI将“冻结”。而不是调用Thread.sleep(n)为重复任务实现Swing Timer或为长时间运行的任务实现SwingWorker。有关详细信息,请参阅Concurrency in Swing

以下源代码使用代码中定义的Thread,但将GUI更新放回到EDT上。

但这里真正的问题是动画组件被添加到大小为0x0的applet中。通过将父容器的布局更改为BorderLayout,我们可以将其拉伸以适应可用空间。

E.G。

import java.awt.*;
import javax.swing.*;

public class FanApplet extends JApplet
{
    private int angle1 = -15;
    private int angle2 = 75;
    private int angle3 = 165;
    private int angle4 = 255;

    public FanApplet()
    {
        add(new FanControl());
    }

    class FanControl extends JPanel
    {
        public FanControl()
        {
            // by setting a BorderLayout and adding a component to the CENTER
            // (default if no constraint specified) the child component will
            // be stretched to fill the available space.
            setLayout(new BorderLayout());
            add(new Fan());
        }
    }

    class Fan extends JPanel
    {
        public Fan()
        {
            //this.setSize(600, 400);
            Runnable spinner = new SpinFan();

            Thread thread1 = new Thread(spinner);
            thread1.start();
        }


        @Override
        protected void paintComponent(Graphics g)
        {
            super.paintComponent(g);

            g.drawOval(200, 150, 150, 150);
            g.fillArc(210, 160, 130, 130, angle1, 30);
            g.fillArc(210, 160, 130, 130, angle2, 30);
            g.fillArc(210, 160, 130, 130, angle3, 30);
            g.fillArc(210, 160, 130, 130, angle4, 30);
        }
    }

    class SpinFan implements Runnable
    {
        @Override
        public void run()
        {
            try
            {
                while(true)
                {
                    angle1 = (angle1 + 1) % 360;
                    angle2 = (angle2 + 1) % 360;
                    angle3 = (angle3 + 1) % 360;
                    angle4 = (angle4 + 1) % 360;

                    System.out.println(angle1 + " " + angle2 + " " + angle3 + " " + angle4);

                    // This ensures that repaint() is called on the EDT.
                    Runnable r = new Runnable() {
                        public void run() {
                            repaint();
                        }
                    };
                    SwingUtilities.invokeLater(r);

                    Thread.sleep(10);
                }
            }
            catch(InterruptedException ex)
            {
                System.out.println("Problem while putting thread to sleep.");
            }
        }
    }
}