在预定时间更改对象颜色

时间:2013-12-17 10:50:48

标签: java swing graphics jpanel

我正在尝试模拟一个十字路口,到目前为止我已经画了我的路口。现在我想让交通灯亮起来。问题是我无法让我的圆圈每隔X秒就将颜色从绿色变为红色。 请帮帮我。

import java.awt.Color;

import javax.jws.Oneway;
import javax.swing.JFrame;


public class Main {

    public static void main(String[] args) {
        MyMap map = new MyMap();
        JFrame f = new JFrame("Broadway Intersection");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        map.setBackground(Color.white);
        f.add(map);
        f.setSize(1366,738);
        f.setVisible(true);
    }

}


My Map class

import java.awt.Color;
import java.awt.Graphics;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JPanel;

public class MyMap extends JPanel {
    public void paintComponent(final Graphics g) {
        super.paintComponent(g);

        g.setColor(Color.GREEN);
        g.fillRect(0, 0, 500, 250); // stanga sus
        g.fillRect(900, 0, 500, 250); // dreapta sus
        g.fillRect(0, 500, 500, 250);// stanga jos
        g.fillRect(900, 500, 500, 250); // dreapta jos

        g.setColor(Color.GRAY);
        g.fillRect(500, 0, 400, 900);
        g.fillRect(0, 250, 500, 250);
        g.fillRect(900, 250, 500, 250);

        g.setColor(Color.WHITE);
        g.fillRect(695, 0, 5, 100);// linii verticale
        g.fillRect(695, 150, 5, 100);
        g.fillRect(695, 500, 5, 100);
        g.fillRect(695, 650, 5, 50);

        g.fillRect(0, 370, 50, 5);
        g.fillRect(100, 370, 100, 5); // linii orizontale
        g.fillRect(250, 370, 100, 5);
        g.fillRect(400, 370, 100, 5);
        g.fillRect(900, 370, 100, 5);
        g.fillRect(1050, 370, 100, 5);
        g.fillRect(1200, 370, 100, 5);

        Timer timer = new Timer();
        boolean semaphore =true;
            timer.schedule(new TimerTask() {
                @Override
                public void run() {

                    g.setColor(Color.RED);
                    g.fillOval(700, 400, 20, 20);
                    System.out.println("tic tac");
                }
            }, 0, 5000);
            g.setColor(Color.GREEN);
            g.fillOval(700, 400, 20, 20);
    }

}

我在中心有一个圆圈,我想改变它的颜色,而不是我开始绘制我的红绿灯:D感谢所有帮助的人。

1 个答案:

答案 0 :(得分:2)

尝试将Timer放在paintComponent()之外,并将Color个对象作为类成员,以便可以在TimerpaintComponent中访问它。当计时器到达它的标记时,它会改变颜色,然后调用repaint。尝试这样的事情(未经测试)

import java.awt.Color;
import java.awt.Graphics;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JPanel;

public class MyMap extends JPanel {

    Color color = Color.GREEN;     <-- class member

    public MyMap(){
        Timer timer = new Timer();
        boolean semaphore =true;    <-- not sure what this is for so I left it
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                color = Color.RED;      <-- change color

                repaint();              <-- repaint();

                System.out.println("tic tac");
            }
        }, 0, 5000);
    }

    public void paintComponent(final Graphics g) {
        super.paintComponent(g);

        g.setColor(Color.GREEN);
        g.fillRect(0, 0, 500, 250); // stanga sus
        g.fillRect(900, 0, 500, 250); // dreapta sus
        g.fillRect(0, 500, 500, 250);// stanga jos
        g.fillRect(900, 500, 500, 250); // dreapta jos

        g.setColor(Color.GRAY);
        g.fillRect(500, 0, 400, 900);
        g.fillRect(0, 250, 500, 250);
        g.fillRect(900, 250, 500, 250);

        g.setColor(Color.WHITE);
        g.fillRect(695, 0, 5, 100);// linii verticale
        g.fillRect(695, 150, 5, 100);
        g.fillRect(695, 500, 5, 100);
        g.fillRect(695, 650, 5, 50);

        g.fillRect(0, 370, 50, 5);
        g.fillRect(100, 370, 100, 5); // linii orizontale
        g.fillRect(250, 370, 100, 5);
        g.fillRect(400, 370, 100, 5);
        g.fillRect(900, 370, 100, 5);
        g.fillRect(1050, 370, 100, 5);
        g.fillRect(1200, 370, 100, 5);


        g.setColor(color);             <--- just use your color variable
        g.fillOval(700, 400, 20, 20);

    }
}

编辑:尝试使用Swing Timer而不是java.util.Timer

Timer timer = new Timer(5000, new ActionListener(){
    public void actionPerformed(ActionEvent e){
        color = Color.GREEN;
        repaint();
    }
});
timer.setRepeats(false);
timer.start();

编辑2:保持交替

Timer timer = new Timer(5000, new ActionListener(){
    public void actionPerformed(ActionEvent e){
        if (color.equals(Color.GREEN){
            color = Color.RED;
        } else {
            color = Color.GREEN;
        }
        repaint();
    }
});
timer.start();

编辑3:不同的时间延迟

Color color = Color.GREEN;
Timer timer;
public MyMap() {

    timer = null;
    timer = new Timer(5000, new ActionListener(){
        public void actionPerformed(ActionEvent e) {
            if (color.equals(Color.GREEN)) {
                color = Color.RED;
                timer.setDelay(2000);
            } else {
                color = Color.GREEN;
                timer.setDelay(8000);
            }
            repaint();
        }
    });
    timer.start();
}