如何使动画可见?

当我单击开始按钮时,所有内容都消失了... 此外,如果我使用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,102));
        setforeground(new Color(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));
        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",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",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",13));
        btnNewButton_1.setbounds(363,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",13));
        btnNewButton_3.setbounds(611,43);
        contentPane.add(btnNewButton_3);

        JLabel lblNewLabel = new JLabel("Bubble Sort Algorithm");
        lblNewLabel.setfont(new Font("HanziPen TC",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,50);
            g.setColor(Color.BLACK);
            g.drawString(Integer.toString(arr[1]),y2,280);
            g.setColor(Color.PINK);
            g.fillOval(x3,50);
            g.setColor(Color.BLACK);
            g.drawString(Integer.toString(arr[2]),y3,280);
            g.setColor(Color.PINK);
            g.fillOval(x4,50);
            g.setColor(Color.BLACK);
            g.drawString(Integer.toString(arr[3]),y4,280);
            g.setColor(Color.PINK);
            g.fillOval(x5,50);
            g.setColor(Color.BLACK);
            g.drawString(Integer.toString(arr[4]),y5,280);
            g.setColor(Color.PINK);
            g.fillOval(x6,50);
            g.setColor(Color.BLACK);
            g.drawString(Integer.toString(arr[5]),y6,280);
            g.setColor(Color.PINK);
            g.fillOval(x7,50);
            g.setColor(Color.BLACK);
            g.drawString(Integer.toString(arr[6]),y7,280);
            g.setColor(Color.PINK);
            g.fillOval(x8,50);
            g.setColor(Color.BLACK);
            g.drawString(Integer.toString(arr[7]),y8,280);
            g.setColor(Color.PINK);
            g.fillOval(x9,50);
            g.setColor(Color.BLACK);
            g.drawString(Integer.toString(arr[8]),y9,280);
            g.setColor(Color.PINK);
            g.fillOval(x10,50);
            g.setColor(Color.BLACK);
            g.drawString(Integer.toString(arr[9]),y10,280);
            g.setColor(Color.PINK);
            g.fillOval(x11,50);
            g.setColor(Color.BLACK);
            g.drawString(Integer.toString(arr[10]),y11,280);
            g.setColor(Color.PINK);
            g.fillOval(x12,50);
            g.setColor(Color.BLACK);
            g.drawString(Integer.toString(arr[11]),y12,280);
            g.setColor(Color.PINK);
            g.fillOval(x13,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; 
        }


        }

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

huanglianxing 回答:如何使动画可见?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3169508.html

大家都在问