如何使动画可见?

时间:2019-11-04 01:25:44

标签: java swing animation jframe

当我单击开始按钮时,所有内容都消失了... 此外,如果我使用for循环编写x1,x2,x3 ...,也会显示错误。 我想使代码更短...

public class BubbleSort extends JFrame implements ActionListener {

    Timer tm = new Timer(5, this);

    static int[] arr = new int[13];

    int i = 0;
    int j = 0;

    int x1 = 20 + 60 * 0;
    int x2 = 20 + 60 * 1;
    int x3 = 20 + 60 * 2;
    int x4 = 20 + 60 * 3;
    int x5 = 20 + 60 * 4;
    int x6 = 20 + 60 * 5;
    int x7 = 20 + 60 * 6;
    int x8 = 20 + 60 * 7;
    int x9 = 20 + 60 * 8;
    int x10 = 20 + 60 * 9;
    int x11 = 20 + 60 * 10;
    int x12 = 20 + 60 * 11;
    int x13 = 20 + 60 * 12;

    int y1 = 35 + 60 * 0;
    int y2 = 35 + 60 * 1;
    int y3 = 35 + 60 * 2;
    int y4 = 35 + 60 * 3;
    int y5 = 35 + 60 * 4;
    int y6 = 35 + 60 * 5;
    int y7 = 35 + 60 * 6;
    int y8 = 35 + 60 * 7;
    int y9 = 35 + 60 * 8;
    int y10 = 35 + 60 * 9;
    int y11 = 35 + 60 * 10;
    int y12 = 35 + 60 * 11;
    int y13 = 35 + 60 * 12;

    int vel = 1;

    private JPanel contentPane;

    public void actionPerformed(ActionEvent e) {

        x1 = x1 + vel;
        y1 = y1 + vel;
        repaint();

    }

    /**
     * Launch the application.
     */

    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    InsertionSort frame = new InsertionSort();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }


    /**
     * Create the frame.
     */
    public BubbleSort() {


        Random r = new Random();

        for (int i = 0; i < arr.length; i ++ ) {
            arr[i] = r.nextInt(100);
        }

        setBackground(new Color(0, 0, 102));
        setForeground(new Color(0, 0, 153));
        setFont(new Font("American Typewriter", Font.BOLD | Font.ITALIC, 15));
        setTitle("Insertion Sort Algorithm");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 818, 481);
        contentPane = new JPanel();
        contentPane.setBackground(new Color(204, 255, 204));
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JButton btnGoBack = new JButton("Go Back");
        btnGoBack.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                contentPane.setVisible(false);
                Animation2 b = new Animation2();
                b.setVisible(true);
            }
        });
        btnGoBack.setFont(new Font("American Typewriter", Font.BOLD | Font.ITALIC, 13));
        btnGoBack.setBounds(17, 16, 97, 36);
        contentPane.add(btnGoBack);

        JButton btnNewButton = new JButton("START");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                tm.start();

            }
        });
        btnNewButton.setFont(new Font("American Typewriter", Font.BOLD | Font.ITALIC, 13));
        btnNewButton.setBounds(101, 376, 117, 43);
        contentPane.add(btnNewButton);

        JButton btnNewButton_1 = new JButton("PAUSE");
        btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                tm.stop();

            }
        });
        btnNewButton_1.setFont(new Font("American Typewriter", Font.BOLD | Font.ITALIC, 13));
        btnNewButton_1.setBounds(363, 376, 117, 43);
        contentPane.add(btnNewButton_1);

        JButton btnNewButton_3 = new JButton("CONTINUE");
        btnNewButton_3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                tm.restart();
            }
        });
        btnNewButton_3.setFont(new Font("American Typewriter", Font.BOLD | Font.ITALIC, 13));
        btnNewButton_3.setBounds(611, 376, 117, 43);
        contentPane.add(btnNewButton_3);

        JLabel lblNewLabel = new JLabel("Bubble Sort Algorithm");
        lblNewLabel.setFont(new Font("HanziPen TC", Font.BOLD | Font.ITALIC, 33));
        lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
        lblNewLabel.setBounds(148, 37, 511, 74);
        contentPane.add(lblNewLabel);

    }


        public void paint(Graphics g){


            super.paint(g);

            g.setColor(Color.PINK);
            g.fillOval(x1, 250, 50, 50);
            g.setColor(Color.BLACK);
            g.drawString(Integer.toString(arr[0]), y1, 280);
            g.setColor(Color.PINK);
            g.fillOval(x2, 250, 50, 50);
            g.setColor(Color.BLACK);
            g.drawString(Integer.toString(arr[1]), y2, 280);
            g.setColor(Color.PINK);
            g.fillOval(x3, 250, 50, 50);
            g.setColor(Color.BLACK);
            g.drawString(Integer.toString(arr[2]), y3, 280);
            g.setColor(Color.PINK);
            g.fillOval(x4, 250, 50, 50);
            g.setColor(Color.BLACK);
            g.drawString(Integer.toString(arr[3]), y4, 280);
            g.setColor(Color.PINK);
            g.fillOval(x5, 250, 50, 50);
            g.setColor(Color.BLACK);
            g.drawString(Integer.toString(arr[4]), y5, 280);
            g.setColor(Color.PINK);
            g.fillOval(x6, 250, 50, 50);
            g.setColor(Color.BLACK);
            g.drawString(Integer.toString(arr[5]), y6, 280);
            g.setColor(Color.PINK);
            g.fillOval(x7, 250, 50, 50);
            g.setColor(Color.BLACK);
            g.drawString(Integer.toString(arr[6]), y7, 280);
            g.setColor(Color.PINK);
            g.fillOval(x8, 250, 50, 50);
            g.setColor(Color.BLACK);
            g.drawString(Integer.toString(arr[7]), y8, 280);
            g.setColor(Color.PINK);
            g.fillOval(x9, 250, 50, 50);
            g.setColor(Color.BLACK);
            g.drawString(Integer.toString(arr[8]), y9, 280);
            g.setColor(Color.PINK);
            g.fillOval(x10, 250, 50, 50);
            g.setColor(Color.BLACK);
            g.drawString(Integer.toString(arr[9]), y10, 280);
            g.setColor(Color.PINK);
            g.fillOval(x11, 250, 50, 50);
            g.setColor(Color.BLACK);
            g.drawString(Integer.toString(arr[10]), y11, 280);
            g.setColor(Color.PINK);
            g.fillOval(x12, 250, 50, 50);
            g.setColor(Color.BLACK);
            g.drawString(Integer.toString(arr[11]), y12, 280);
            g.setColor(Color.PINK);
            g.fillOval(x13, 250, 50, 50);
            g.setColor(Color.BLACK);
            g.drawString(Integer.toString(arr[12]), y13, 280);


                }

        public void swap(int[] arr, int A, int B) {
            int temp = arr[A];
            arr[A] = arr[B];
            arr[B] = temp; 
        }


        }

我想看动画的过程并看圆的平滑移动。

0 个答案:

没有答案