java - 如何为框架设置彩色背景?

标签 java swing

我正在尝试添加背景颜色,例如红色或蓝色等...我几乎尝试了在其他论坛帖子中找到的所有内容,但没有任何效果。

这不是我的代码。我从开源中获取它并正在修改它

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.awt.color.*;


public class main {
    public static void main(String[] args) {
        Program program = new Program();
        program.run();
    }
}

class Program {
    private JFrame mainFrame;
    private DrawPanel drawPanel;
    private java.util.List<Ball> balls;

    private int windowWidth = 640;
    private int windowHeight = 480;
    private String windowLabel = "Bounce Program";

    void run() {

        balls = new ArrayList<>();

        /* Generate balls */
        for (int i = 0; i < 50; i++) {
            Ball ball = new Ball(
                    /* Random positions from 0 to windowWidth or windowHeight */
                    (int) Math.floor(Math.random() * windowWidth),
                    (int) Math.floor(Math.random() * windowHeight),
                    /* Random size from 10 to 30 */
                    (int) Math.floor(Math.random() * 20) + 10,
                    /* Random RGB colors*/
                    new Color(
                            (int) Math.floor((Math.random() * 256)),
                            (int) Math.floor((Math.random() * 256)),
                            (int) Math.floor((Math.random() * 256))
                    ),
                    /* Random velocities from -5 to 5 */
                    (int) Math.floor((Math.random() * 10) - 5),
                    (int) Math.floor((Math.random() * 10) - 5)
            );

            balls.add(ball);
        }

        /* Initialize program */
        mainFrame = new JFrame();
        drawPanel = new DrawPanel();
        mainFrame.getContentPane().add(drawPanel);
        mainFrame.setTitle(windowLabel);
        mainFrame.setSize(windowWidth, windowHeight);
        mainFrame.setVisible(true);
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



        while (true) {
            for (Ball b: balls) {
                b.update();
            }

            /* Give Swing 10 milliseconds to see the update! */
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            mainFrame.repaint();
        }
    }

    class DrawPanel extends JPanel {
        @Override
        public void paintComponent(Graphics graphics) {
            super.paintComponent(graphics);

            for (Ball b: balls) {
                b.draw(graphics);
            }

        }
    }

    class Ball {
        private int posX, posY, size;
        private Color color;

        private int vx = 5;
        private int vy = 5;

        public Ball(int posX, int posY, int size, Color color, int vx, int vy) {
            this.posX = posX;
            this.posY = posY;
            this.size = size;
            this.color = color;
            this.vx = vx;
            this.vy = vy;
        }

        void update() {

            if (posX > mainFrame.getWidth() || posX < 0) {
                vx *= -1;
            }

            if (posY > mainFrame.getHeight() || posY < 0) {
                vy *= -1;
            }

            if (posX > mainFrame.getWidth()) {
                posX = mainFrame.getWidth();
            }

            if (posX < 0) {
                posX = 0;
            }

            if (posY > mainFrame.getHeight()) {
                posY = mainFrame.getHeight();
            }

            if (posY < 0) {
                posY = 0;
            }

            this.posX += vx;
            this.posY += vy;

        }

        void draw(Graphics g) {
            g.setColor(color);
            g.fillOval(posX, posY, size, size);
        }
    }
}

最佳答案

drawPanel = new DrawPanel();

您向框架添加了一个面板,因此您需要设置该面板的背景:

drawPanel = new DrawPanel();
drawPanel.setBackground( Color.RED );

关于java - 如何为框架设置彩色背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53440416/

相关文章:

java - 当 JLabel 包含 HTML 时,BoxLayout 不遵守胶水

java - admob 和 startActivity 崩溃

java - 如何在 JLabel 或 JEditorPane 中指定选择

java - Minfiying CSS 破坏了嵌入的 EL 表达式

Java Hibernate - 无法连接到 H2

java - jTextField 仅接受字母和空格

Java2D 在移动图像后删除旧像素?

java - 如何从 Swing 的 JTextField 中获取整数值?

java - 如何将列表映射的值转换为单个列表

java - Android AlarmManager 暂停/继续