用Java创建动画4x4网格 [英] Creating an animated 4x4 grid in Java

查看:99
本文介绍了用Java创建动画4x4网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Java中创建一个4 x 4的矩形网格,然后我需要这些矩形来改变序列中的颜色。



我从来没有做过任何事情之前的图形工作,只是控制台中的东西。



我开始做一些研究并创建了650 x 650 JFrame 将矩形放入。
之后我使用 GridLayout 并设法使用窗口从按钮创建一个4 x 4网格。 JButton 这是不对的。



我将如何创建矩形?使用带有++的循环为动画计时是否正确?



在stackoverflow和google上搜索时,我找不到任何符合我需求的东西。对不起,如果这是一个愚蠢的问题。我是新来的,我正在做一个正面的。



这是我希望它看起来像,每个矩形在一个时间间隔内改变颜色



解决方案

来自@ Eng.Fouad

  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;

公共类SimpleTimer扩展JFrame
{
私有JLabel标签;
私人定时器计时器;
private int counter = 3; //持续时间
private int delay = 1000; //每1秒
私有静态最终长串行版本号= 1L;
private颜色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(延迟,动作);
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(延迟,动作);
timer.setInitialDelay(0);
timer.start();
}
其他
{
c =红色? Color.GREEN:Color.RED;
red =!red;
label.setBackground(c);
label.setOpaque(true);
label.setText(等待+ i +秒);
i--;
}
}
};

public static void main(String [] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
new SimpleTimer();
}
});
}
}


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

解决方案

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();
            }
        });
    }
}

这篇关于用Java创建动画4x4网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆