-1

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 things in the console.

I started by doing some research and created a 650 x 650 JFrame to put the rectangles in. After that I used GridLayout and managed to create a 4 x 4 grid out of buttons using window.JButton which wasn't right.

How would I create rectangles instead? And would it be right to use for loops with ++ to time the animation?

I couldn't find anything that worked for my needs when searching on stackoverflow and google. Sorry if this is a stupid question. I'm new to this and I'm doing for an apprecticeship.

Here's how I would like it to look like, with each rectangle changing color in a time interval

enter image description here

Frakcool
  • 10,915
  • 9
  • 50
  • 89
wundersoy
  • 11
  • 4
  • you could use a normal `JPanel` wih different background colours – SomeJavaGuy Jan 12 '16 at 14:56
  • Welcome to Stack Overflow, this isn't a site where you ask for code. Provide the code you have tried as a [Runnable Example](http://stackoverflow.com/help/mcve) after you have read [how to ask](http://stackoverflow.com/help/how-to-ask) a good question and taken the [tour](http://stackoverflow.com/tour). Please, we can see your username on the right, so, there's no need for "Thanks, wundersoy" to be on the post. Also please post an image of what you want it to look like. – Frakcool Jan 12 '16 at 14:59
  • Okay, so I could create 15 JPanels and arrange them in a grid? Then just change the color properties when I need to? – wundersoy Jan 12 '16 at 15:00
  • *using window.JButton which wasn't right.* Why wasn't it right? *And would it be right to use for loops with ++ to time the animation?* Why don't you try with a [Swing Timer](https://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html)? *Sorry if this is a stupid question.* No, it's not, it's just you haven't provided any code you have tried to solve this problem by yourself. Check the links I provided and edit your post accordingly, then your question will be better suited for Stack Overflow, and it may get some upvotes and more and better answers. – Frakcool Jan 12 '16 at 15:01
  • I understand I that, I'm not asking for code I'm just at work right now so I can't post what I have. I will check those links now :) – wundersoy Jan 12 '16 at 15:02
  • Here is what it needs to look like http://imgur.com/uq1MdBG But the boxes will change color Sorry I couldn't edit, didn't expect enter to post it straight away – wundersoy Jan 12 '16 at 15:08
  • Urgh, I just realised I have to put this into a webpage. – wundersoy Jan 12 '16 at 15:27
  • Post an [MCVE](http://stackoverflow.com/help/mcve). – user1803551 Jan 12 '16 at 15:35
  • @wundersoy what do you mean with *I just realised I have to put this into a webpage* – Frakcool Jan 12 '16 at 15:47
  • You mean something like [this](http://stackoverflow.com/questions/33727830/java-button-pausing-graphical-updates/33727882#33727882)? – MadProgrammer Jan 12 '16 at 20:04

2 Answers2

2

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:

enter image description here enter image description here enter image description here enter image description here

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();
            }
        });
    }
}
Community
  • 1
  • 1
Frakcool
  • 10,915
  • 9
  • 50
  • 89
0

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.

Community
  • 1
  • 1
Jure
  • 799
  • 6
  • 25
  • Thread.sleep will make the GUI to freeze, it's better to use a [Swing Timer](https://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html) – Frakcool Jan 12 '16 at 15:06