0

如何在 Java 中创建基本动画?

目前我正在尝试在 Java 中使用 swing 来实现一个基本的动画程序。但我不知道我的程序逻辑是否正确。我的程序实现RunnableActionListener接口和扩展JApplet。我想知道,JApplet和Runnable的启动方法有必要区分吗?

如果是,那么为什么..?

我的程序是基本的气球程序,当我单击开始按钮时,气球开始向上移动并再次到达地板。这将一直持续到我按下停止按钮。

这是我的代码。

public class Balls extends JApplet implements Runnable{

    private static final long serialVersionUID = 1L;
    JPanel btnPanel=new JPanel();
    static boolean flag1=true;
    static boolean flag2=true;
    static boolean flag3=false;
    static int h;
    static int temp=10;
    Thread t=new Thread(this);

    JButton start;
    JButton stop;


    public void init()
    {
         try
         {
             SwingUtilities.invokeAndWait(

                 new Runnable()
                 {
                     public void run()
                     {
                         makeGUI();
                     }


                 });                         

         }
         catch(Exception e)
         {
             JOptionPane.showMessageDialog(null,"Can't create GUI because of exception"); 
             System.exit(0);
         }

    }


    private void makeGUI() {

        start=new JButton("start");
        stop=new JButton("stop");
        btnPanel.add(start);
        btnPanel.add(stop);
        add(btnPanel,BorderLayout.NORTH);
    }

    public void run()
    {           
        while(true)
        {
            if(flag1)
            {
                repaint();
                flag1=false;
            }
            else
            {
                try
                {
                    wait();
                    flag3=true;
                }
                catch(InterruptedException e)
                {
                    JOptionPane.showMessageDialog(null,"Error ocuured !!\n Exiting..");
                    System.exit(0);
                }
            }   
        }   

    }

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        int h=Integer.parseInt(getParameter("height"));
        if (flag1)
        {
            g.setColor(Color.RED);
            g.fillOval(10,h,50,50);
            g.setColor(Color.YELLOW);
            g.fillOval(50,h,20,20);
            g.setColor(Color.CYAN);
            g.fillOval(70,h,80,30);
            g.setColor(Color.BLUE);
            g.fillOval(120,h,50,60);
            g.setColor(Color.GRAY);
            g.fillOval(160,h,70,50);
            g.setColor(Color.GREEN);
            g.fillOval(200,h,80,80);
            g.setColor(Color.MAGENTA);
            g.fillOval(260,h,80,30);
            g.setColor(Color.DARK_GRAY);
            g.fillOval(320,h,60,40);
            g.setColor(Color.pink);
            g.fillOval(370,h,65,45);
            flag1=false;
        }
        else
        {
            g.setColor(Color.RED);
            g.fillOval(10,h-temp,50,50);
            g.setColor(Color.YELLOW);
            g.fillOval(50,h-temp,20,20);
            g.setColor(Color.CYAN);
            g.fillOval(70,h-temp,80,30);
            g.setColor(Color.BLUE);
            g.fillOval(120,355,50,60);
            g.setColor(Color.GRAY);
            g.fillOval(160,h-temp,70,50);
            g.setColor(Color.GREEN);
            g.fillOval(200,h-temp,80,80);
            g.setColor(Color.MAGENTA);
            g.fillOval(260,h-temp,80,30);
            g.setColor(Color.DARK_GRAY);
            g.fillOval(320,h-temp,60,40);
            g.setColor(Color.pink);
            g.fillOval(370,h-temp,65,45);
            if(flag2 && temp<=400)
            {
                temp+=10;
                if(temp==400)
                {
                    flag2=false;
                }
            }
            else if(!flag2)
            {
                temp-=10;
                if(temp==10)
                {
                    flag2=true;
                }
            }
            else
            {

            }
        }
    }

    public void start()
    {
        start.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                t.start();
                if(flag3)
                {
                    notify();
                }

            }
        });
        stop.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                flag1=false;
                try
                {
                    Thread.sleep(5000);
                }
                catch(InterruptedException e)
                {
                    repaint();
                    t=null;
                }
            }
        });

    }       
}
4

1 回答 1

1

1)SwingTimer按照建议使用,而不是您的Runnable实施。

2)阅读自定义绘画在这里

3)画在JPanel而不是JFrame

我已经更改了您的代码,请检查它。我认为,它做你想要的。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class Balls extends JApplet {

    private static final long serialVersionUID = 1L;
    JPanel btnPanel = new JPanel();
    static boolean flag1 = true;
    static boolean flag2 = true;
    static boolean flag3 = false;
    static int h;
    static int temp = 10;
    JButton start;
    JButton stop;
    private Timer timer;

    public void init() {
            SwingUtilities.invokeLater(

            new Runnable() {
                public void run() {
                    makeGUI();
                }

            });
    }

    private void makeGUI() {
        timer = new Timer(10, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                repaint();
            }
        });
        start = new JButton("start");
        stop = new JButton("stop");
        start.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                timer.start();
            }
        });
        stop.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                timer.stop();
            }
        });
        btnPanel.add(start);
        btnPanel.add(stop);
        add(new MyPanel(), BorderLayout.CENTER);
        add(btnPanel, BorderLayout.NORTH);
    }

    class MyPanel extends JPanel{

        public void paintComponent(Graphics g) {
             super.paintComponent(g);
            int h = Integer.parseInt(getParameter("height"));
            if (flag1) {
                g.setColor(Color.RED);
                g.fillOval(10, h, 50, 50);
                g.setColor(Color.YELLOW);
                g.fillOval(50, h, 20, 20);
                g.setColor(Color.CYAN);
                g.fillOval(70, h, 80, 30);
                g.setColor(Color.BLUE);
                g.fillOval(120, h, 50, 60);
                g.setColor(Color.GRAY);
                g.fillOval(160, h, 70, 50);
                g.setColor(Color.GREEN);
                g.fillOval(200, h, 80, 80);
                g.setColor(Color.MAGENTA);
                g.fillOval(260, h, 80, 30);
                g.setColor(Color.DARK_GRAY);
                g.fillOval(320, h, 60, 40);
                g.setColor(Color.pink);
                g.fillOval(370, h, 65, 45);
                flag1 = false;
            } else {
                g.setColor(Color.RED);
                g.fillOval(10, h - temp, 50, 50);
                g.setColor(Color.YELLOW);
                g.fillOval(50, h - temp, 20, 20);
                g.setColor(Color.CYAN);
                g.fillOval(70, h - temp, 80, 30);
                g.setColor(Color.BLUE);
                g.fillOval(120, 355, 50, 60);
                g.setColor(Color.GRAY);
                g.fillOval(160, h - temp, 70, 50);
                g.setColor(Color.GREEN);
                g.fillOval(200, h - temp, 80, 80);
                g.setColor(Color.MAGENTA);
                g.fillOval(260, h - temp, 80, 30);
                g.setColor(Color.DARK_GRAY);
                g.fillOval(320, h - temp, 60, 40);
                g.setColor(Color.pink);
                g.fillOval(370, h - temp, 65, 45);
                if (flag2 && temp <= 400) {
                    temp += 10;
                    if (temp == 400) {
                        flag2 = false;
                    }
                } else if (!flag2) {
                    temp -= 10;
                    if (temp == 10) {
                        flag2 = true;
                    }
                } else {

                }
            }
        }
    }
}
于 2013-11-14T12:24:23.177 回答