0

我对 java 还很陌生,所以我认为这不是很接近正确,但我似乎可以找到任何其他帮助。基本上,我正在尝试为 jPanel 的背景颜色设置动画,以使其色调(我使用的是 hsb 颜色模型)发生变化。有点像这样:https ://kahoot.it/#/ 注意颜色是如何从一个浮动到另一个的。这是我到目前为止的代码:

public void animate(){


    for(float i=.001f;i<1f;i+=.001f){


            jPanel1.setBackground(Color.getHSBColor(i, .53f, .97f));
            try{
                Thread.sleep(5L);
            }catch(InterruptedException ex){

            }
        System.out.println(i);

    }

}

现在我知道这可能不对,但是循环工作正常,唯一的问题是 jPanel 在循环完成之前不会“更新”。对不起大家对这样的事情是个大菜鸟,感谢您的任何回复

4

2 回答 2

4

问题是您阻塞了事件调度线程,因此无法进行绘图。使用摇摆计时器而不是睡觉。更改 HSB 颜色的运行示例:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class ColorCycle {
    private static class ColorPanel extends JPanel {
        private final float stepSize;
        private final Timer timer;
        private int index;

        ColorPanel(final int steps, int fps) {
            stepSize = 1f / steps;
            timer = new Timer(1000 / fps, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    index++;
                    if (index > steps) {
                        index = 0;
                    }
                    repaint();
                }
            });
        }

        void start() {
            timer.start();
        }

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

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.getHSBColor(index * stepSize, 1f,  1f));
            g.fillRect(0, 0, getWidth(), getHeight());
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame("Colors");
                ColorPanel panel = new ColorPanel(300, 20);
                frame.getContentPane().add(panel);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLocationByPlatform(true);
                frame.pack();
                frame.setVisible(true);
                panel.start();  
            }
        });
    }
}
于 2014-03-08T20:23:29.413 回答
-2

为我工作...

import java.awt.Color;
import java.lang.reflect.InvocationTargetException;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

public class HsbBackground {

    public static void main(String[] args) throws Exception {
        new HsbBackground();
    }

    private JPanel jPanel1 = new JPanel();

    public HsbBackground() throws InvocationTargetException, InterruptedException {

        SwingUtilities.invokeAndWait(new Runnable() {

            @Override
            public void run() {
                JFrame jFrame = new JFrame();
                jFrame.setContentPane(jPanel1);
                jFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
                jFrame.setSize(400, 300);
                jFrame.setVisible(true);
            }
        });

        animate();

    }

    public void animate() {

        for (float i = .001f; i < 1f; i += .001f) {

            final float j = i;

            SwingUtilities.invokeLater(new Runnable() {

                @Override
                public void run() {
                    jPanel1.setBackground(Color.getHSBColor(j, .53f, .97f));
                }
            });

            try {
                Thread.sleep(5L);
            } catch (InterruptedException ex) {

            }
            System.out.println(i);

        }

    }

}

确保setVisible在开始循环之前调用框架。

PS:我更新了代码,所以 GUI 的变化都是从事件调度线程进行的。代码仍然可以正常工作。

于 2014-03-08T20:00:21.827 回答