简体   繁体   中英

Updating Applet with random color

I am trying to make a JFrame with a background that changes slowly over time. 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
  2. DON'T create a Timer in paint , paint is called EVERY TIME the component needs to be painted and often in quick succession. See Painting in AWT and Swing nd Performing Custom Painting for more details about how painting works in 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
  4. JFrame != Applet
  5. Math.random returns a value between 0 and 1 , Color expects a values between 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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