Java动画,使对象每隔'x'秒改变一次颜色 [英] java animation, making an object change color every 'x' seconds

查看:76
本文介绍了Java动画,使对象每隔'x'秒改变一次颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道是否有人可以帮助我或给我一些定时器的建议,我遇到的问题是我需要一个物体,每单击按钮闪光灯"每隔x秒就换一次颜色并保持到目前为止,单击按钮稳定"后只能使用单一颜色.到目前为止,我已经可以使用按钮了,但似乎无法使对象闪烁"(自行更改颜色).我的代码在下面,可以正常工作.

hi there i was just wondering if someone could help me or give me some advice with timers, the problem i have got is that i need an object to change color every x seconds on click of a button 'flash' and stay a single color on click of a button 'steady' so far i have got my buttons to work i just cant seem to get the object to 'flash'(change color on its own). my code is below and works without problems.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


/**
 * Created by joe on 26/03/15.
 */

    class Beacon extends JPanel {
    private boolean lightOn = true;

    private int x = 150;
    private int y = 90;
    private int ballSize = 55;

        public void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2 = (Graphics2D) g;
                if (lightOn){
                    g2.setColor(Color.BLACK);
                    g2.fillRect(172, 140, 12, 30);
                    g2.drawRect(172, 170, 11, 30);
                    g2.fillRect(172, 200, 12, 30);
                    g2.drawRect(172, 230, 11, 30);
                    g2.fillRect(172, 260, 12, 30);
                    g2.drawRect(172, 290, 11, 30);
                    g2.fillRect(172, 320, 12, 30);
                    g2.drawRect(172, 350, 11, 30);
                    g2.setColor(Color.ORANGE);
                    g2.fillOval(x, y, ballSize, ballSize);
                }
            else{
        g2.setColor(Color.BLACK);
        g2.fillRect(172, 140, 12, 30);
        g2.drawRect(172, 170, 11, 30);
        g2.fillRect(172, 200, 12, 30);
        g2.drawRect(172, 230, 11, 30);
        g2.fillRect(172, 260, 12, 30);
        g2.drawRect(172, 290, 11, 30);
        g2.fillRect(172, 320, 12, 30);
        g2.drawRect(172, 350, 11, 30);
        g2.setColor(Color.GRAY);
        g2.fillOval(x, y, ballSize, ballSize);
    }   }
    public void lightOn() { lightOn = true; }
    public void lightOff() { lightOn = false; }
}

    public class BeaconViewer extends JFrame
    {
        JButton Flash = new JButton("Flash");
        JButton Steady = new JButton("Steady");
        JPanel bPanel = new JPanel();
        Beacon bbPanel = new Beacon();



    public BeaconViewer()
    {
        bPanel.add(Flash);
        this.add(bPanel, BorderLayout.SOUTH);
        bPanel.add(Steady);
        this.add(bbPanel, BorderLayout.CENTER);
        Flash.addActionListener(new FlashListener());
        Steady.addActionListener(new SteadyListener());
    }

        class FlashListener implements ActionListener {

            public void actionPerformed(ActionEvent e) {
                bbPanel.lightOff();
                repaint();

            }

        }

        class SteadyListener implements ActionListener {

            public void actionPerformed(ActionEvent a) {
                bbPanel.lightOn();
                repaint();
            }

        }

        public static void main(String[] args) {
        JFrame scFrame = new BeaconViewer();
        scFrame.setTitle("Belish Beacon");
        scFrame.setSize(300, 500);
        scFrame.setDefaultCloseOperation((JFrame.EXIT_ON_CLOSE));
        scFrame.setVisible(true); }}

推荐答案

您想要做的是使用一个摆动计时器并将其注册为 AbstractAction ActionListener 它将根据您传递的延迟参数触发.

What you'd like to do is use a swing timer and register it an AbstractAction or ActionListener which it will fire based on a delay parameter that you pass it.

例如,我可能有...

For example, I may have...

...
Timer timer = new Timer(FRAME_DELAY, myActionListener);
timer.start();
...

其中 FRAME_DELAY 是事件触发之间的毫秒数,并且 myActionListener 接收执行的操作的通知.

where FRAME_DELAY is the number of milliseconds between event firings and myActionListener receives the notification of the action performed.

一旦有了计时器,您就可以调整延迟,停止和重新启动.有关更多信息,请参见计时器.

Once you have the timer you can adjust the delay, stop, and restart among other things. See Timer for better information.

这篇关于Java动画,使对象每隔'x'秒改变一次颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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