简体   繁体   English

用随机颜色更新Applet

[英]Updating Applet with random color

I am trying to make a JFrame with a background that changes slowly over time. 我正在尝试制作一个JFrame,其背景随着时间的推移会缓慢变化。 here's my code: 这是我的代码:

public class RainbowWindow extends Applet implements ActionListener {

    private static final long serialVersionUID = 1L;

    public void paint(Graphics g) {
        Timer timer = new Timer(1000, this);
        timer.start();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Color color = new Color(((int) Math.random()), ((int) Math.random()), ((int) Math.random()), 255);
        setBackground(color);

    }
}

but all i get is just a black screen 但我所得到的只是一个黑屏

  1. Java Plugin support deprecated and Moving to a Plugin-Free Web Java插件支持已弃用,移至无插件Web
  2. DON'T create a Timer in paint , paint is called EVERY TIME the component needs to be painted and often in quick succession. 不要在paint创建Timer ,油漆被称为EVERY TIME组件需要进行油漆并且通常要快速连续进行。 See Painting in AWT and Swing nd Performing Custom Painting for more details about how painting works in Swing 有关绘画如何在Swing中工作的更多详细信息,请参见AWT中的绘画和Swing执行自定义绘画
  3. You sould avoid overriding paint of top level containers like JFrame and Applet , they aren't double buffered and it tends to cause no end of issues, better to start with some kind of component and add it to whatever container you want 您可以避免覆盖诸如JFrameApplet类的顶级容器的paint ,它们不是双重缓冲的,它往往不会导致问题的结束,最好从某种组件开始,然后将其添加到所需的任何容器中
  4. JFrame != Applet JFrame != Applet
  5. Math.random returns a value between 0 and 1 , Color expects a values between 0 - 255 Math.random返回之间的值01Color期望之间的值0 - 255

For example 例如

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
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 Test {

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

    public Test() {
        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 {

        public TestPane() {
            Timer timer = new Timer(1000, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    int red = (int)(Math.random() * 255);
                    int green = (int)(Math.random() * 255);
                    int blue = (int)(Math.random() * 255);
                    Color color = new Color(red, green, blue, 255);
                    setBackground(color);
                }
            });
            timer.start();
        }

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

    }
}

You should be extending JApplet (not Applet ), and adding a component to set the color on, and you shouldn't be starting a Timer in your paint method, and you casting a floating point value to an int isn't going to give you any values in your random color generation. 您应该扩展JApplet (而不是Applet ),并添加一个用于设置颜色的组件,并且不应该在paint方法中启动Timer ,并且将浮点值强制转换为int不会给您在随机颜色生成中的任何值。 Something like, 就像是,

public class RainbowWindow extends JApplet implements ActionListener {

    private static final long serialVersionUID = 1L;

    private JPanel panel = new JPanel();

    @Override
    public void actionPerformed(ActionEvent e) {
        Color color = new Color(((int) (Math.random() * 255)),
                ((int) (Math.random() * 255)),
                ((int) (Math.random() * 255)), 255);
        panel.setBackground(color);
    }

    public RainbowWindow() {
        Timer timer = new Timer(1000, this);
        timer.start();
        add(panel);
        setVisible(true);
    }
}

Which I tested, and it changes the background color to a new random color once a second. 我进行了测试,并且每秒将背景颜色更改为一种新的随机颜色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM