迭代列表并绘制彩色方块

时间:2015-07-18 00:00:24

标签: java arrays list colors awt

我有一个彩色的盒子,我希望它每1/2秒更换一次颜色,但是我希望我的代码能够运行。

我使用Java AWT的Graphic Api使用g.fillRect绘制(568,383,48,48);其中g被包裹到' Graphics。'

所以你认为这很简单吗?

Color[] colors

colors = new Color[4];

colors[0] = new Color(Color.red);
colors[1] = new Color(Color.blue);
colors[2] = new Color(Color.green);
colors[3] = new Color(Color.yellow);

for(int i = 0; i < colors.length; i++){
    g.setColor(colors[i]);
    g.fillRect(568, 383, 48, 48);
}

这很酷但问题是当这个for循环正在运行时我的程序都没有运行......

我想我可以制作游戏&#39; Multi-Threaded&#39;这意味着它一次可以做多件事但我不知道如何做到这一点听起来很难,所有人都很感激!

2 个答案:

答案 0 :(得分:2)

大多数UI框架都不是线程安全的,因此您需要注意这一点。例如,在Swing中,您可以使用Swing Timer作为伪循环。因为Timer在事件调度线程的上下文中通知ActionListener,所以可以安全地从内部更新UI或UI的状态,而不会有线程竞争条件的风险

请查看Concurrency in SwingHow to use Swing Timers了解详情

Colors

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class JavaApplication430 {

    public static void main(String[] args) {
        new JavaApplication430();
    }

    public JavaApplication430() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private Color[] colors;
        private int whichColor = 0;

        public TestPane() {

            colors = new Color[4];

            colors[0] = Color.red;
            colors[1] = Color.blue;
            colors[2] = Color.green;
            colors[3] = Color.yellow;

            Timer timer = new Timer(500, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    whichColor++;
                    repaint();
                    if (whichColor >= colors.length) {
                        whichColor = colors.length - 1;
                        ((Timer)(e.getSource())).stop();
                    }
                }
            });
            timer.start();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setColor(colors[whichColor]);
            g2d.fillRect(0, 0, getWidth(), getHeight());
            g2d.dispose();
        }

    }

}

答案 1 :(得分:1)

如果您的代码是单线程,我无法想象如何创建交互式游戏。要定期更改盒子颜色,你必须让你的线程进入睡眠状态。如果您的游戏不是多线程,那么这将冻结您的应用程序,阻止用户交互。您将在Java中找到许多有关使用线程编程的有趣材料:

http://docs.oracle.com/javase/tutorial/essential/concurrency/

http://www.ibm.com/developerworks/library/j-thread/

http://moderntone.blogspot.com.br/2013/02/a-simple-java-multithreading-example.html

只是谷歌吧!