Creating an animated 4x4 grid in Java

后端 未结 2 1583
挽巷
挽巷 2020-11-28 00:11

I need to create a 4 x 4 grid of rectangles in Java, I then need these rectangles to change color in a sequence.

I\'ve never done any graphical work before, just thi

相关标签:
2条回答
  • 2020-11-28 00:40

    You could use jLabel, and set background color to it. How you do it, you can read here: How do I set a JLabel's background color?

    Then just use for loop and Thred.sleep to change there color in animation.

    0 讨论(0)
  • 2020-11-28 00:42

    From @Eng.Fouad answer (so give him credit and upvote his answer too), I made some changes, this example shows how to use a Swing Timer which changes color every second from green to red. I'm using a simple JLabel for demonstration purposes, take this logic into the GridLayout you have:

    Here are some screen shots on how it looks:

    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.Color;
    
    import javax.swing.BorderFactory;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    import javax.swing.Timer;
    
    public class SimpleTimer extends JFrame
    {
        private JLabel label;
        private Timer timer;
        private int counter = 3; // the duration
        private int delay = 1000; // every 1 second
        private static final long serialVersionUID = 1L;
        private Color c = Color.RED;
        private boolean red = true;
        private boolean stop = false;
        int i = counter;
    
        public SimpleTimer()
        {
            super("Simple Timer");
            setDefaultCloseOperation(EXIT_ON_CLOSE);
    
            label = new JLabel("Wait for " + counter + " sec", JLabel.CENTER);
            JPanel contentPane = (JPanel) getContentPane();
            contentPane.add(label, BorderLayout.CENTER);
            contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
            pack();
    
            timer = new Timer(delay, action);
            timer.setInitialDelay(0);
            timer.start();
            setVisible(true);
        }
    
        ActionListener action = new ActionListener()
        {   
            @Override
            public void actionPerformed(ActionEvent event)
            {
                if(i == 0)
                {
                    timer.stop();
                    stop = true;
                    i = counter;
                    timer = new Timer(delay, action);
                    timer.setInitialDelay(0);
                    timer.start();
                }
                else
                {
                    c = red ? Color.GREEN : Color.RED;
                    red = !red;
                    label.setBackground(c);
                    label.setOpaque(true);
                    label.setText("Wait for " + i + " sec");
                    i--;
                }
            }
        };
    
        public static void main(String[] args)
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                @Override
                public void run()
                {
                    new SimpleTimer();
                }
            });
        }
    }
    
    0 讨论(0)
提交回复
热议问题