初学者 - 动画跳到最后

时间:2014-01-02 00:16:23

标签: java swing animation

大家好,我是新手程序员,如果我想尝试一些愚蠢的事,请原谅。

我今天开始学习GUI应用程序,我想做一个练习来检查我是否正确学习它。当我运行程序时,屏幕上有一个点,我希望它在我点击开始按钮时向右移动。我完成了这个,但我希望它是一个动画,我希望这个点看起来好像它正在缓慢移动。但是当我点击按钮时,它只是传送。

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

public class Guila extends JFrame {
    private JPanel panel;
    private JButton myButton;
    private final int WINDOW_WIDTH = 300;
    private final int WINDOW_HEIGHT = 600;
    private JPanel[] array = new JPanel[900];
    private int i = 0;
    int j = 0;
    int m = 0;
    int k = 0;

    public Guila() {
        setTitle("Simple Animation");
        setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        panel = new JPanel();
        panel.setLayout(new GridLayout(30, 30));

        while(i < 900) {
            array[i] = new JPanel();
            if(i == 460) {
                array[i].setBackground(Color.BLACK);
            } 
            else {
                array[i].setBackground(Color.WHITE);
            }
        panel.add(array[i]);
        i++;
        }

      JPanel panel2 = new JPanel();
      myButton = new JButton("Start");
      myButton.addActionListener(new myActionListener());
      setLayout(new GridLayout(2,1));
      add(panel);
      panel2.add(myButton);
      add(panel2);
      setVisible(true);

    }



    private class myActionListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {

            array[460+j].setBackground(Color.WHITE);
            array[461+j].setBackground(Color.BLACK);

            repeat();
        }
    }
    public void repeat() {
        if (j<11) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException ex) {
                Thread.currentThread().interrupt();
            }
            j++;
            myButton.doClick();
        }
    }


    public static void main(String[] args) {
        new Guila();
    }
}

2 个答案:

答案 0 :(得分:4)

不要在Event Dispatch Thread上执行的代码上使用Thread.sleep()。

使用Swing Timer进行动画制作。每次Timer触发时,您都会更新要更改的组件的属性,然后在组件上调用repaint()。

答案 1 :(得分:0)

我同意将Swing Timer用于动画。虽然创建所需的慢动作效果可能是一个挑战。

如果需要,请使用以下代码帮助您入门:

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

public class Bullet extends JPanel implements ActionListener
{
    private static final long serialVersionUID = 1L;    
    private static final int SCREEN_WIDTH = 500;
    private static final int increments = 5;

    int[] xPoints = new int[5];

    public void paintComponent(Graphics g)
    {
        g.setColor(Color.WHITE);
        g.fillRect(0,0,800,800);


        g.setColor(Color.BLACK);
        for (int i = 0; i < xPoints.length; i++)
        {
            g.fillRect(xPoints[i]+150, 210, 20, 20);
        }

        Timer timer = new Timer(100, new ActionListener()
        {   
            public void actionPerformed(ActionEvent e) 
            {
                for (int i = 0; i <= 1; i++) 
                {
                    if (xPoints[i] + increments < SCREEN_WIDTH) 
                    {
                        xPoints[i] += increments;

                    } else {

                        xPoints[i] = 0; 
                    }          
                }
                repaint();
            }
        }); 

        timer.start();  
    }

    public static void main(String[] args)
    {
        JFrame frame = new JFrame();
        frame.setSize(500,500);
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(new Bullet());
        frame.setVisible(true);
    }

}