用 Java 创建动画 4x4 网格 [英] Creating an animated 4x4 grid in Java
问题描述
我需要在 Java 中创建一个 4 x 4 的矩形网格,然后我需要这些矩形按顺序改变颜色.
我以前从未做过任何图形工作,只在控制台中做过.
我首先做了一些研究并创建了一个 650 x 650 JFrame
来放置矩形.之后,我使用了 GridLayout
并设法使用 window.JButton
从按钮中创建了一个 4 x 4 网格,这是不正确的.
我将如何创建矩形?使用带有 ++ 的 for 循环来为动画计时是否正确?
在 stackoverflow 和 google 上搜索时,我找不到任何适合我需要的东西.对不起,如果这是一个愚蠢的问题.我是新来的,我正在做实习.
这是我想要的样子,每个矩形在一个时间间隔内改变颜色
来自@Eng.Fouad
import java.awt.BorderLayout;导入 java.awt.event.ActionEvent;导入 java.awt.event.ActionListener;导入 java.awt.Color;导入 javax.swing.BorderFactory;导入 javax.swing.JFrame;导入 javax.swing.JLabel;导入 javax.swing.JPanel;导入 javax.swing.SwingUtilities;导入 javax.swing.Timer;公共类 SimpleTimer 扩展 JFrame{私人 JLabel 标签;私人定时器定时器;私有整数计数器 = 3;//持续时间私人整数延迟 = 1000;//每 1 秒private static final long serialVersionUID = 1L;私有颜色 c = Color.RED;私人布尔红色 = 真;私人布尔停止=假;int i = 计数器;公共 SimpleTimer(){super("简单定时器");setDefaultCloseOperation(EXIT_ON_CLOSE);label = new JLabel("等待" + counter + " sec", JLabel.CENTER);JPanel contentPane = (JPanel) getContentPane();contentPane.add(label, BorderLayout.CENTER);contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));盒();定时器 = 新定时器(延迟,动作);timer.setInitialDelay(0);定时器开始();设置可见(真);}ActionListener action = new ActionListener(){@覆盖public void actionPerformed(ActionEvent 事件){如果(我 == 0){定时器停止();停止 = 真;我 = 计数器;定时器 = 新定时器(延迟,动作);timer.setInitialDelay(0);定时器开始();}别的{c = 红色?颜色.绿色:颜色.红色;红色 = !红色;label.setBackground(c);label.setOpaque(true);label.setText("等待" + i + " 秒");一世 - ;}}};public static void main(String[] args){SwingUtilities.invokeLater(new Runnable(){@覆盖公共无效运行(){新的简单定时器();}});}}
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屋!