【问题标题】:Random Color Background随机颜色背景
【发布时间】:2013-10-14 17:58:21
【问题描述】:

当我按下“r”时,我试图让这段代码将背景颜色更改为随机颜色。到目前为止,除了将背景颜色更改为随机颜色外,一切正常。这个程序是一个屏幕保护程序,我必须用随机颜色在随机位置生成随机形状。

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

public class ScreenSaver1 extends JPanel {
    private JFrame frame = new JFrame("FullSize");
    private Rectangle rectangle;
    boolean full;

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        setBackground(Color.BLACK);
    }

    ScreenSaver1() {
        // Remove the title bar, min, max, close stuff
        frame.setUndecorated(true);
        // Add a Key Listener to the frame
        frame.addKeyListener(new KeyHandler());
        // Add this panel object to the frame
        frame.add(this);
        // Get the dimensions of the screen
        rectangle = GraphicsEnvironment.getLocalGraphicsEnvironment()
        .getDefaultScreenDevice().getDefaultConfiguration().getBounds();
        // Set the size of the frame to the size of the screen
        frame.setSize(rectangle.width, rectangle.height);
        frame.setVisible(true);
        // Remember that we are currently at full size
        full = true;
    }

// This method will run when any key is pressed in the window
class KeyHandler extends KeyAdapter {
    public void keyPressed(KeyEvent e) {
        // Terminate the program.
        if (e.getKeyChar() == 'x') {
            System.out.println("Exiting");
            System.exit(0);
        }
        else if (e.getKeyChar() == 'r') {
            System.out.println("Change background color");
            setBackground(new Color((int)Math.random() * 256, (int)Math.random() * 256, (int)Math.random() * 256));
        }
        else if (e.getKeyChar() == 'z') {
            System.out.println("Resizing");
            frame.setSize((int)rectangle.getWidth() / 2, (int)rectangle.getHeight());
        }
    }

}

public static void main(String[] args) {
        ScreenSaver1 obj = new ScreenSaver1();
    }
}

【问题讨论】:

    标签: java swing graphics keylistener paintcomponent


    【解决方案1】:

    我将首先从您的 paintComponent 方法中删除 setBackground(Color.BLACK);

    您遇到的另一个问题是您计算随机值的方式...

    (int)Math.random() * 256
    

    这基本上是将Math.random() 的结果转换为int,这通常会导致它变为0,然后再乘以256,即0...

    相反,尝试使用类似的东西

    (int)(Math.random() * 256)
    

    在将结果转换为int之前会执行Math.random() * 256的计算

    您可能还想看看Frame#getExtendedStateFrame#setExtendedState...它会让您的生活变得更加轻松...

    【讨论】:

    • 我忘记了 repaint()。我只是使用 setBackground(Color.BLACK) 作为测试。同样,当我这样做时,它只会从白色变为黑色。
    • 这也是有道理的。这就是它变黑的原因。
    • @Ryel,不需要 repaint()。 Swing 足够聪明,可以在组件属性更改时执行 repaint()。 -1 表示 repaint() 建议。 +1 其他两个建议。
    • @camickr 你赢了一些,你输了一些
    • 还要考虑Color.getHSBColor(r.nextFloat(), 1, 1),其中rRandom 的一个实例。
    【解决方案2】:

    试试这个:

    (int)(Math.random() * 256)
    

    或者这个:

    Random gen= new Random();
    getContentPane().setBackground(Color.Black);
    

    要获得随机颜色,试试这个:

    .setBackground(Color.(gen.nextInt(256), gen.nextInt(256),
                    gen.nextInt(256));
    

    【讨论】:

    • 请考虑格式化你的答案,它对我来说并不完全可读。
    猜你喜欢
    • 1970-01-01
    • 2018-11-25
    • 1970-01-01
    • 1970-01-01
    • 2018-04-15
    • 2017-06-24
    • 1970-01-01
    • 2015-04-26
    • 2021-10-31
    相关资源
    最近更新 更多