javax.swing.Timer slowdown in Java7u40

后端 未结 2 1408
盖世英雄少女心
盖世英雄少女心 2021-01-14 15:06

Invoke javax.swing.Timer#start() same time,

7u25 is not problem.

\"enter

2条回答
  •  自闭症患者
    2021-01-14 16:01

    Several issues arise in your example:

    • Swing GUI objects should be constructed and manipulated only on the event dispatch thread.

    • All Swing Timer instances share a common thread, which is being saturated.

    Depending on the goal, some alternatives are possible:

    • Use a single Timer instance, and select some fraction for update at a proportionally higher rate. The example below randomly selects N of the components and updates them every 100 ms.

    • Use TexturePaint, as shown here.

    image

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.Graphics;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    import java.util.Random;
    import javax.swing.*;
    
    /** @see https://stackoverflow.com/a/18936444/230513 */
    public class BlinkenLights {
    
        private static final int S = 24;
        private static final int N = 10;
        private static final Random r = new Random();
        private static final List list = new ArrayList();
    
        private static final class MyComponent extends JComponent {
    
            public MyComponent() {
                this.setOpaque(true);
            }
    
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(S, S);
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                g.setColor(Color.getHSBColor(r.nextFloat() / 6, 1, 1));
                g.fillRect(0, 0, getWidth(), getHeight());
            }
        }
    
        private static JPanel createPanel() {
            final JPanel p = new JPanel();
            p.setLayout(new GridLayout(N, N));
            for (int i = 0; i < N * N; i++) {
                MyComponent c = new MyComponent();
                p.add(c);
                list.add(c);
            }
            Timer t = new Timer(1000 / N, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Collections.shuffle(list, r);
                    for (int i = 0; i < N; i++) {
                        list.get(i).repaint();
                    }
                }
            });
            t.start();
            return p;
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    JFrame f = new JFrame();
                    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    f.add(createPanel());
                    f.pack();
                    f.setLocationRelativeTo(null);
                    f.setVisible(true);
                }
            });
        }
    }
    

提交回复
热议问题